180 likes | 290 Views
Using Java (2). Temperature. Temperature. Temperature. An object is. an instance of a class that has state, behaviour and identity. // Filename Temperature.java // // Class to represent a temperature in degrees // centigrade, accurate to 1/10 of a degree.
E N D
Using Java (2) Phil Campbell London South Bank University
Temperature Phil Campbell London South Bank University
Temperature Phil Campbell London South Bank University
Temperature Phil Campbell London South Bank University
An object is an instance of a class that has state, behaviour and identity. Phil Campbell London South Bank University
// Filename Temperature.java // // Class to represent a temperature in degrees // centigrade, accurate to 1/10 of a degree. // Fintan Culwin, v0.2, Sept 03. public classTemperatureextendsObject { Phil Campbell London South Bank University
Temperature - theTemp : double + Temperature( ) + Temperature( setTo: double) + getTemp(): double # setTemp(setTo: double) + toString(): String attribute Call constructor in super class (Object) constructor constructor privatedoubletheTemp = 0.0; publicTemperature() { this( 0.0); } // End Temperature no-args constructor. publicTemperature( doublesetTo) { super(); theTemp = setTo; } // End Temperature constructor. Phil Campbell London South Bank University
A constructor Places a newly created object into a well defined initial state Phil Campbell London South Bank University
inquiry methods publicdoublegetTemp() { returntheTemp; } // End The getTemp. publicStringtoString() { returntheTemp + " degrees centigrade"; } // End toString. Phil Campbell London South Bank University
Note: mutator method protectedvoidsetTemp( doublesetTo) { theTemp = setTo; } // End The setTemp. Phil Campbell London South Bank University
Thingy class DiagramExercise Write what you would expect to see in Java Object Thingy - thing : int + Thingy( ) # Thingy( toThis : int) + setThing( toThis :int) + getThing( ) : int + toString( ) : String Phil Campbell London South Bank University
Object Thingy public classThingyextendsObject { } Phil Campbell London South Bank University
Object Thingy - thing : int public classThingyextendsObject { } private int thing; Phil Campbell London South Bank University
concatenation` Object publicThingy( ){ this( 0); } Thingy -thing : int protectedThingy( int toThis){ super( ); thing = toThis; } + toString( ) : String + Thingy( ) # Thingy( toThis : int) + setThing( toThis :int) + getThing( ) : int public void setThing( int toThis){ thing = toThis; } public int getThing( ){ returnthing; } public String toString( ){ return "Value held is " + thing; } Phil Campbell London South Bank University
Object IsA Temperature instance of instance of instance of bodyTemp boiling freezing creates and shows creates and shows creates and shows TemperatureDemonstration Phil Campbell London South Bank University
0001 // Filename TemperatureDemonstration.java 0002 // 0003 // Demonstration client for the Temperature 0004 // class. 0005 // 0006 // Fintan Culwin, v0.1, sept 99. 0007 0008 public classTemperatureDemonstrationextendsObject { 0009public static voidmain( String[] args) { 0010 0011Temperature freezing = null; 0012Temperature boiling = null; 0013Temperature bodyHeat = null; 0014 0015 System.out.println( "\n\nTemperature Demonstration\n\n"); 0016 0017System.out.println( "This demonstration will construct and display"); 0018System.out.println( "Three different instances of the Temp~ class."); 0019 0020System.out.println( "\nConstrucing ... \n"); Phil Campbell London South Bank University
0025 freezing = newTemperature(); 0026 boiling = newTemperature( 100.0); 0027 bodyHeat = newTemperature( 37.8); 0029 System.out.println( "\nConstructed, showing ... \n");0030 0031 System.out.println( "Freezing is " + freezing.toString() );0032 System.out.println( "Boiling is " + boiling.toString());0033 System.out.println( "Body Heat is " + bodyHeat);0034 } // End main. 0035 0036 } // End TemperatureDemonstration. Constructed, showing ... Freezing is 0.0 degrees centigrade Boiling is 100.0 degrees centigrade Body Heat is 37.8 degrees centigrade Phil Campbell London South Bank University
Object Thingy - thing : int + Thingy( ) # Thingy( toThis : int) + setThing( toThis :int) + getThing( ) : int + toString( ) : String class 10 fred : Thingy thing = 5 instance Make a demo class for a Thingy (first line) public classThingyDemoextendsObject { public static void main( String[] args) { Write the start of the main method Create a Thingy reference with the name fred Thingy fred = null; Make fred refer to a new Thingy with initial value 5 fred = new Thingy(5); fred.setThing( 10); Set the value in the new Thingy to 10 Create an int called result and give it the number from the Thingy int result = fred.getThing(); System.out.println( fred.toString()); Display the Thingy to the screen }// end main() 10 }// end ThingyDemo Phil Campbell London South Bank University