|
CS 596 Java Programming Fall Semester, 1998 Classes (1): members, this, constructors, packages |
|
|---|---|---|
|
© 1998, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 19-Sep-98 |
| Java | field | method |
| C++ | data member | member function |
| Smalltalk | instance variable | method |
| C | ??? | function |
public class BankAccount
{
public float balance = 0.0F; // field
public void deposit( float amount ) // method
{
balance += amount ;
}
}
public class BankAccount
{
public float balance = 0.0F;
}
public class RunBank
{
public static void main( String args[] )
{
System.out.println( "Start main " );
BankAccount richStudent = new BankAccount( );
richStudent.balance = (float) 100000;
BankAccount poorInstructor = new BankAccount( );
poorInstructor.balance = 5.10F;
System.out.println( "Student Balance: " +
richStudent.balance );
System.out.println( "Prof Balance: " + poorInstructor.balance );
}
}
Start main Student Balance: 100000 Prof Balance: 5.1
rohan 21-> ls BankAccount.java RunBank.java rohan 22-> javac RunBank.java rohan 23-> ls BankAccount.class RunBank.class BankAccount.java RunBank.java rohan 24-> java RunBank Start main Student Balance: 100000.0 Prof Balance: 5.1
public class BankAccount
{
public float balance = 0.0F;
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
public class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( );
BankAccount poorInstructor;
poorInstructor = new BankAccount( );
richStudent.deposit( 10000F);
poorInstructor.deposit( 5.10F );
System.out.println( "Student Balance: " +
richStudent.balance );
System.out.println( "Prof: " + poorInstructor.toString() );
}
}
Student Balance: 10000 Prof: Account Balance: 5.1
public class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( );
BankAccount poorInstructor = new BankAccount( );
richStudent.deposit( 10000F);
poorInstructor.deposit( 5.10F );
String profBalance = "Prof: " + poorInstructor;
System.out.println( profBalance );
System.out.println( "Prof: " + poorInstructor );
System.out.println( richStudent );
}
}
Prof: Account Balance: 5.1 Prof: Account Balance: 5.1 Account Balance: 10000
public class BankAccount
{
public float balance = 0.0F;
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
public class TaxAccount
{
public double balance;
{ //Instance Initialization block
float baseTaxRate = .33F;
float companySize = 1.24F;
balance = baseTaxRate * companySize -
Math.sin( baseTaxRate ) + .013f;
}
public static void main( String[] args ) //Test method
{
TaxAccount x = new TaxAccount();
System.out.println( x.balance);
}
}
0.09815697215174707
public class BankAccount
{
public float balance;
public BankAccount( float initialBalance ) //Constructor
{
balance = initialBalance;
}
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
public class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( 10000F );
BankAccount poorInstructor = new BankAccount( 5.10F );
System.out.println( "Prof: " + poorInstructor );
System.out.println( "Student: " + richStudent );
}
}
Prof: Account Balance: 5.1 Student: Account Balance: 10000.0
public class ConstructorExample {
public ConstructorExample( ) {
System.out.println( "In constructor - no argument" );
}; // the ; is legal but does nothing
public ConstructorExample( int size) {
System.out.println( "In constructor - one argument" );
}
public void ConstructorExample( ) {
System.out.println( "return type means it is ");
System.out.println( "not a constructor " );
}
}
public class TestConstructor {
public static void main( String args[] ) {
System.out.println( "Start main " );
ConstructorExample test = new ConstructorExample( );
ConstructorExample x = new ConstructorExample(5);
System.out.println( "Done with Constructors " );
test.ConstructorExample ();
}
}
Start main In constructor - no argument In constructor - one argument Done with Constructors return type means it is not a constructor
public class ImplicitConstructorOnly {
int size = 5;
}
public class OneConstructor {
OneConstructor( String message ) {
System.out.println( message );
}
}
public class TwoConstructors {
TwoConstructors ( String message ) {
System.out.println( message );
}
TwoConstructors ( ) {
System.out.println( "No argument Constructor" );
}
}
public class Constructors {
public static void main( String args[] ) {
ImplicitConstructorOnly ok = new ImplicitConstructorOnly();
TwoConstructors alsoOk = new TwoConstructors();
OneConstructor compileError = new OneConstructor();
}
}
public class OrderExample
{
int aField = 1;
{
System.out.println( "First block: " + aField );
aField++;
}
public OrderExample()
{
System.out.println( "Start Constructor: " + aField );
aField++;
System.out.println( "End Constructor: " + aField );
}
{
System.out.println( "Second block: " + aField );
aField++;
}
public static void main( String args[] )
{
OrderExample test = new OrderExample();
}
}
First block: 1 Second block: 2 Start Constructor: 3 End Constructor: 4
public class OrderExample
{
public OrderExample()
{
a = 8; // OK
}
{
a = 4; // Compile Error
}
int a = 1;
public int getC()
{
return c;
}
int c = 3;
{
a = 4;
}
}
public class OverLoad
{
public void same() {
System.out.println( "No arguments" );
}
public void same( int firstArgument ) {
System.out.println( "One int arguments" );
}
public void same( char firstArgument ) {
System.out.println( "One char arguments" );
}
public int same( int firstArgument ) { // Compile Error
System.out.println( "One char arguments" );
return 5;
}
public void same( char firstArgument, int secondArgument) {
System.out.println( "char + int arguments" );
}
public void same( int firstArgument, char secondArgument ) {
System.out.println( "int + char arguments" );
}
}
public class BankAccount
{
public float balance;
public BankAccount( float initialBalance )
{
this.balance = initialBalance;
}
public BankAccount( int balance )
{
this.balance = balance;
}
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
public class BankAccount
{
public float balance;
public BankAccount( float initialBalance )
{
this.balance = initialBalance;
}
public BankAccount deposit( float amount )
{
balance += amount ;
return this;
}
}
public class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( 10000F );
richStudent.deposit( 100F ).deposit( 200F ).deposit( 300F );
System.out.println( "Student: " + richStudent.balance );
}
}
Student: 10600.0
public class CustomerList {
public BankAccount[] list = new BankAccount[ 100 ];
public int nextFreeSlot = 0;
public void add( BankAccount newItem ){
list[ nextFreeSlot++ ] = newItem;
}
}
public class BankAccount {
public float balance;
public BankAccount( float initialBalance ) {
this.balance = initialBalance;
}
public void badBalanceCheck( CustomerList badAccounts ) {
if ( balance <= 0F ) badAccounts.add( this );
}
}
public class RunBank {
public static void main( String args[] ) {
BankAccount richStudent = new BankAccount( 10000F );
CustomerList customersToDrop = new CustomerList();
richStudent.badBalanceCheck( customersToDrop );
}
}
public class ThisAndConstructors
{
public ThisAndConstructors()
{
this( 5 );
System.out.println( "No argument");
}
public ThisAndConstructors( int a)
{
this( a, 10 );
System.out.println( "One argument");
}
public ThisAndConstructors( int a, int b)
{
System.out.println( "Two arguments");
}
public static void main( String args[] )
{
new ThisAndConstructors();
}
}
Two arguments One argument No argument
public class MethodCallInConstructor
{
private int value;
public MethodCallInConstructor( int first, int second, int third )
{
this( nonstaticAdd( first, second, third ) ); // compiler error
}
public MethodCallInConstructor( int first, int second )
{
this( add( first, second ) ); // OK, can call static method
System.out.println( "2 arguments: Value = " + value );
}
public MethodCallInConstructor( int first )
{
value = first;
System.out.println( "1 argument: Value = " + value );
}
public int nonstaticAdd( int a, int b, int c )
{
return a + b + c;
}
public static int add( int a, int b )
{
return a + b;
}
}
public class Death
{
int myId;
public Death ( int sequenceNumber)
{
myId = sequenceNumber;
}
public void finalize( )
{
System.out.println( myId );
}
}
public class Finalization
{
public static void main( String args[] )
{
Death sampleObject;
for ( int k = 0; k < 5; k++ )
sampleObject = new Death( k );
}
}
public class FinalizationOnExit
{
public static void main( String args[] )
{
System.runFinalizersOnExit(true);
Death sampleObject;
for ( int k = 0; k < 5; k++ )
sampleObject = new Death( k );
}
}
0 1 2 3 4
public class FinalizationForced
{
public static void main( String args[] )
{
Death sampleObject;
for ( int k = 0; k < 5; k++ )
sampleObject = new Death( k );
System.gc();
System.runFinalization();
}
}
0 1 2 3
S-sept14 2mins, Q-sept17 11mins Doc 4, Classes (1): members, this, constructors, packages Slide # 29
public class AccessLevels
{
public int publicObjectVariable ;
protected float protectedObjectVariable = 10;
private int[] privateObjectVariable;
int packageAcceess = publicObjectVariable;
public AccessLevels ( int startValue )
{
System.out.println( " Start Constructor" );
privateObjectVariable = new int[ startValue ];
}
public void sampleMethod( int value )
{
System.out.println( " In method" );
privateObjectVariable[ 1 ] = value;
}
}
public class TestAccessLevels
{
public static void main( String args[] )
{
AccessLevels test = new AccessLevels ( 11 );
test.publicObjectVariable = 100; // Ok
test.protectedObjectVariable= 100; // Ok
test.privateObjectVariable[ 1 ] = 100; // Compile Error
test.packageAcceess = 100; // Ok
}
}
public class Hide
{
public void publicAccess()
{
System.out.println( "Start public access " );
internalWorker();
realPrivateWorker();
}
protected void internalWorker()
{
System.out.println( "In internal worker " );
}
private void realPrivateWorker()
{
System.out.println( "In Private worker " );
}
public static void main( String[] args )
{
Hide me = new Hide();
me.publicAccess();
}
}
Start public access In internal worker In Private worker
public class BankAccount
{
private float balance;
public BankAccount( float initialBalance )
{
balance = initialBalance;
}
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
|
Decomposability
Composability
Protection
|
Continuity
Understandability
|
|
Decomposability
Composability
|
Continuity
Understandability
|
public class BankAccount {
private float balance;
public float getBalance() {
return balance;
}
private void setbalance( float newBalance) {
balance = newBalance;
}
public BankAccount( float initialBalance ) {
setbalance( initialBalance );
}
public void deposit( float amount ) {
setbalance( getBalance() + amount );
}
public String toString() {
return "Account Balance: " + getBalance();
}
}
package packageName;
package EDU.sdsu.roger;
public class Sample {
public void hello() {
System.out.println( "Hello for package sdsu.roger" );
}
}
package EDU.sdsu.roger;
public class Sample {
public void hello() {
System.out.println( "Hello for package sdsu.roger" );
}
}
setenv CLASSPATH '.:/home/ma/whitney/java'
import EDU.sdsu.roger.Sample;
public class TestPackage {
public static void main( String args[] ) {
Sample me = new Sample();
me.hello();
}
}
import EDU.sdsu.roger.Sample;
public class TestPackage {
public static void main( String args[] ) {
Sample me = new Sample();
me.hello();
}
}
public class TestPackage {
public static void main( String args[] ) {
EDU.sdsu.roger.Sample me =
new EDU.sdsu.roger.Sample();
me.hello();
}
}
import EDU.sdsu.roger.*;
package SearchTree;
public class Leaf
{
public Leaf()
{
System.out.println( "Leaf in a binary search tree" );
}
}
package Botany;
public class Leaf
{
public Leaf()
{
System.out.println( "Leaf in a real tree" );
}
}
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
SearchTree.Leaf node = new SearchTree.Leaf();
}
}
import SearchTree.Leaf;
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
Leaf node = new Leaf();
}
}
import SearchTree.Leaf;
import Botany.Leaf; // Compile error
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
Leaf node = new Leaf();
}
}
import SearchTree.Leaf;
import Botany.*;
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
Leaf node = new Leaf();
}
}
package Botany;
public class Leaf
{
public Leaf()
{
System.out.println( "Leaf in a real tree" );
}
}
package Botany;
class BotanyHelper
{
// Only code in package Botany can use this class
}