Presentation is loading. Please wait.

Presentation is loading. Please wait.

Phil Campbell London South Bank University Using Java (2)

Similar presentations


Presentation on theme: "Phil Campbell London South Bank University Using Java (2)"— Presentation transcript:

1 Phil Campbell London South Bank University Using Java (2)

2 Phil Campbell London South Bank University Temperature

3 Phil Campbell London South Bank University Temperature

4 Phil Campbell London South Bank University Temperature

5 Phil Campbell London South Bank University An object is an instance of a class that has state, behaviour and identity.

6 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 {

7 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)

8 Phil Campbell London South Bank University A constructor Places a newly created object into a well defined initial state

9 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

10 Phil Campbell London South Bank University protected void setTemp( double setTo) { theTemp = setTo; } // End The setTemp. mutator method Note:

11 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

12 Phil Campbell London South Bank University Thingy Object public class Thingy extends Object { }

13 Phil Campbell London South Bank University Thingy Object public class Thingy extends Object { } - thing : int private int thing;

14 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`

15 Phil Campbell London South Bank University TemperatureDemonstration bodyTempboilingfreezing Temperature creates and shows Object instance of IsA

16 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 class TemperatureDemonstration extends Object { 0009 public static void main( String[] args) { 0010 0011 Temperature freezing = null; 0012 Temperature boiling = null; 0013 Temperature bodyHeat = null; 0014 0015 System.out.println( "\n\nTemperature Demonstration\n\n"); 0016 0017 System.out.println ( "This demonstration will construct and display"); 0018 System.out.println ( "Three different instances of the Temp~ class."); 0019 0020 System.out.println ( "\nConstrucing... \n ");

17 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"); 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

18 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


Download ppt "Phil Campbell London South Bank University Using Java (2)"

Similar presentations


Ads by Google