1 / 18

Using Java (2)

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.

galia
Download Presentation

Using Java (2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Temperature Phil Campbell London South Bank University

  3. Temperature Phil Campbell London South Bank University

  4. Temperature Phil Campbell London South Bank University

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

  6. // 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

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

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

  9. inquiry methods publicdoublegetTemp() { returntheTemp; } // End The getTemp. publicStringtoString() { returntheTemp + " degrees centigrade"; } // End toString. Phil Campbell London South Bank University

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

  11. 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

  12. Object Thingy public classThingyextendsObject { } Phil Campbell London South Bank University

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

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

  15. 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

  16. 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

  17. 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

  18. 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

More Related