Phil Campbell London South Bank University 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 class Temperature extends Object {
Phil Campbell London South Bank University private double theTemp = 0.0; public Temperature() { this( 0.0); } // End Temperature no-args constructor. public Temperature( double setTo) { super(); theTemp = setTo; } // End Temperature constructor. Temperature - theTemp : double + Temperature( ) + Temperature( setTo: double) + getTemp(): double # setTemp(setTo: double) + toString(): String attribute constructor Call constructor in super class (Object)
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 public double getTemp() { return theTemp; } // End The getTemp. public String toString() { return theTemp + " degrees centigrade"; } // End toString. inquiry methods
Phil Campbell London South Bank University protected void setTemp( double setTo) { theTemp = setTo; } // End The setTemp. mutator method Note:
Phil Campbell London South Bank University Thingy - thing : int + Thingy( ) # Thingy( toThis : int) + setThing( toThis :int) + getThing( ) : int + toString( ) : String Object Thingy class Diagram Exercise Write what you would expect to see in Java
Phil Campbell London South Bank University Thingy Object public class Thingy extends Object { }
Phil Campbell London South Bank University Thingy Object public class Thingy extends Object { } - thing : int private int thing;
Phil Campbell London South Bank University Thingy - thing : int + Thingy( ) Object # Thingy( toThis : int) + setThing( toThis :int) + getThing( ) : int + toString( ) : String public Thingy( ){ this( 0); } protected Thingy( int toThis){ super( ); thing = toThis; } public void setThing( int toThis){ thing = toThis; } public int getThing( ){ return thing; } public String toString( ){ return "Value held is " + thing; } concatenation`
Phil Campbell London South Bank University TemperatureDemonstration bodyTempboilingfreezing Temperature creates and shows Object instance of IsA
Phil Campbell London South Bank University 0001 // Filename TemperatureDemonstration.java 0002 // 0003 // Demonstration client for the Temperature 0004 // class // 0006 // Fintan Culwin, v0.1, sept public class TemperatureDemonstration extends Object { 0009 public static void main( String[] args) { Temperature freezing = null; 0012 Temperature boiling = null; 0013 Temperature bodyHeat = null; System.out.println( "\n\nTemperature Demonstration\n\n"); System.out.println ( "This demonstration will construct and display"); 0018 System.out.println ( "Three different instances of the Temp~ class."); System.out.println ( "\nConstrucing... \n ");
Phil Campbell London South Bank University 0025 freezing = new Temperature(); 0026 boiling = new Temperature( 100.0); 0027 bodyHeat = new Temperature( 37.8); 0029 System.out.println( "\nConstructed, showing... \n"); 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 } // End TemperatureDemonstration. Constructed, showing... Freezing is 0.0 degrees centigrade Boiling is degrees centigrade Body Heat is 37.8 degrees centigrade
Phil Campbell London South Bank University Thingy - thing : int + Thingy( ) # Thingy( toThis : int) + setThing( toThis :int) + getThing( ) : int + toString( ) : String Object fred : Thingy thing = 5 class instance Make a demo class for a Thingy (first line) Write the start of the main method Create a Thingy reference with the name fred public class ThingyDemo extends Object { public static void main( String[] args) { Thingy fred = null; Make fred refer to a new Thingy with initial value 5 fred = new Thingy(5); Set the value in the new Thingy to 10 fred.setThing( 10); Create an int called result and give it the number from the Thingy int result = fred.getThing(); Display the Thingy to the screenSystem.out.println( fred.toString()); }// end main() }// end ThingyDemo 10