1 / 38

Java 1

Java 1. First Steps. Java. Is an Object Oriented Language which supports the creation of Object models discussed in the UML section. The next few slides show how to define a class in Java. We then discover how to use that class in a simple Java program. Java is an Object Oriented Language

lynton
Télécharger la présentation

Java 1

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. Java 1 First Steps Phil Campbell London South Bank University

  2. Java Is an Object Oriented Language which supports the creation of Object models discussed in the UML section. The next few slides show how to define a class in Java. We then discover how to use that class in a simple Java program Phil Campbell London South Bank University

  3. Java is an Object Oriented Language • Java is a compiled language • Java is portable (Multi-platform) • but . . . . You are not here to learn Java . . . . You are here to learn Software Development Phil Campbell London South Bank University

  4. SimpleCounter The class called SimpleCounter holds a single value in an attribute called value When a new instance of the class is created, a value has to be supplied to give the counter an initial value The only methods available to the user of the class are: click() - which adds one to the value in the object and getValue() - which returns the number in the value attribute Phil Campbell London South Bank University

  5. A Class Phil Campbell London South Bank University

  6. // filename : SimpleCounter.class // // Demonstration of a **very** simple and not very // useful class // Phil Campbell ver v0.1 September 03 public classSimpleCounterextendsObject{ Phil Campbell London South Bank University

  7. public classSimpleCounterextendsObject{ private intvalue; Phil Campbell London South Bank University

  8. // the constructor public SimpleCounter (intstartValue){ value = startValue; } // End SimpleCounter() Phil Campbell London South Bank University

  9. The constructor ...places a newly created instance into a well defined initial state. Phil Campbell London South Bank University

  10. // a simple method public voidclick( ){ value++; }// End click() Phil Campbell London South Bank University

  11. return type public intgetValue(){ returnvalue; } // End getValue() Phil Campbell London South Bank University

  12. public classSimpleCounterextendsObject{ private intvalue; // the constructor publicSimpleCounter(intstartValue){ value = startValue; }// End SimpleCounter() // a simple action public voidclick(){ value++; }// End click() // a 'getter' public intgetValue(){ returnvalue; }// End getValue() }// End class SimpleCounter Phil Campbell London South Bank University

  13. 5 count: SimpleCounter value = 5 Demonstrating SimpleCounter object : an instance of the class SimpleCounter reference variable count public classSimpleCounterDemonstrationextendsObject{ public static voidmain( String[] args){ SimpleCounter count = null; count = newSimpleCounter( 5); Phil Campbell London South Bank University

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

  15. 5 public voidclick( ){ value++; } Demonstrating SimpleCounter 6 count public classSimpleCounterDemonstrationextendsObject{ public static voidmain( String[] args){ SimpleCounter count = null; count = newSimpleCounter( 5); count.click( ); Phil Campbell London South Bank University

  16. 5 public intgetValue(){ returnvalue; } 6 Demonstrating SimpleCounter public voidclick( ){ value++; } 6 count public static voidmain( String[] args){ SimpleCounter count = null; count = newSimpleCounter( 5); count.click( ); System.out.println( count.getValue( )) Phil Campbell London South Bank University

  17. 5 public classSimpleCounterDemonstrationextendsObject{ public static voidmain (String[] args){ SimpleCounter count = null; count = newSimpleCounter(5); System.out.print( "First value : "); System.out.println( count.getValue()); count.click(); System.out.print( "Second value : "); System.out.println( count.getValue()); count.click(); count.click(); System.out.print( "Third value : "); System.out.println( count.getValue()); }// End of main }// End of SimpleCounterDemonstration 6 7 8 count output First value : 5 Second value: 6 8 Third value : Phil Campbell London South Bank University

  18. The state of an object is the aggregate value of its attributes. Each time the value of an attribute is changed in an object, the object changes state. Phil Campbell London South Bank University

  19. 5 10 public classSimpleCounterDemonstrationextendsObject{ public static voidmain (String[] args){ SimpleCounter count = null; count = newSimpleCounter(5); SimpleCountercount2 = null; count2 = newSimpleCounter(10); count.click(); System.out.print( "First value : "); System.out.println( count.getValue()); count2.click(); count.click(); count2.click(); System.out.print( "Second value : "); System.out.println( count2.getValue()); }// End of main }// End of SimpleCounterDemonstration 6 7 count 11 12 count2 output First value : 6 Second value: 12 Phil Campbell London South Bank University

  20. 5 10 public classSimpleCounterDemonstrationextendsObject{ public static voidmain (String[] args){ SimpleCounter count = null; count = newSimpleCounter(5); SimpleCountercount2 = null; count = newSimpleCounter(10); count.click(); System.out.print( "First value : "); System.out.println( count.getValue()); count2.click(); count.click(); count2.click(); System.out.print( "Second value : "); System.out.println( count2.getValue()); }// End of main }// End of SimpleCounterDemonstration 11 count count2 <----ERROR output First value : 11 Phil Campbell London South Bank University

  21. Words used so far class class reference variable method state constructor inquiry method object instance Describes the contents and behaviour of a set of instances (objects) that share the same attributes, methods and relationships Phil Campbell London South Bank University

  22. Words used so far class reference variable method state constructor inquiry method object instance reference variable Used as the name of an instance. Can only refer to instances of a certain kind. Is not the instance itself. Phil Campbell London South Bank University

  23. Words used so far class reference variable method state constructor inquiry method object instance Describes the behaviour of an object. Provides a way to interact with the data held in the object. method Phil Campbell London South Bank University

  24. Words used so far class reference variable method state constructor inquiry method object instance The state of an object is the aggregate value of all of its attributes set of all values in state Phil Campbell London South Bank University

  25. Words used so far class reference variable method state constructor inquiry method object instance Places a newly created instance into a well defined initial state constructor Phil Campbell London South Bank University

  26. Words used so far class reference variable method state constructor query method object instance A method that returns a value indicating some aspect of the state of an instance inquiry method Phil Campbell London South Bank University

  27. Words used so far class reference variable method state constructor inquiry method object instance An object is an instance of a class that has state, behaviour and identity object Phil Campbell London South Bank University

  28. Words used so far class reference variable method state constructor inquiry method object instance Instantiation is the process of creating a physical example of a class... an object. instance Phil Campbell London South Bank University

  29. The IDE Integrated Development Environment Editor pane File Chooser Interaction pane Phil Campbell London South Bank University

  30. The IDE Phil Campbell London South Bank University

  31. Phil Campbell London South Bank University

  32. first second third Exercise 1 "green" "red" "blue" Colour hue : String getHue():String setHue(String theHue) first : Colour hue : "red" Write the class Colour in Java Phil Campbell London South Bank University

  33. Object Colour hue : String getHue():String setHue(String theHue) public classColourextendsObject{ } // End class Colour private String hue; publicColour (String theHue){ hue = theHue; } // End constructor() public String getHue(){ returnhue; } // end getHue() public voidsetHue( String theHue){ hue = theHue; } // End setHue() Phil Campbell London South Bank University

  34. Object Colour hue : String getHue():String setHue(String theHue) Modifier Method Constructor Attribute Inquiry Method public classColourextendsObject{ private String hue; publicColour (String theHue){ hue = theHue; } // End constructor() public String getHue(){ returnhue; } // end getHue() public voidsetHue( String theHue){ hue = theHue; } // End setHue() } // End class Colour Write a demonstration program for the Colour Class Phil Campbell London South Bank University

  35. "Red" first public classColourDemoextendsObject{ "Blue" public static void main ( String[] argv){ Colour first = null; first = newColour("Red"); System.out.println("First has colour " + first.getHue()); System.out.println("Changing the colour "); first.setHue( "Blue" ); System.out.println(" First now has " + first.getHue()); } // end main() } // end class ColourDemo Phil Campbell London South Bank University

  36. Exercise 2 Rectangle height : double width : double Rectangle (h : double, w : double) area(): double perimeter(): double getHeight(): double getWidth(): double setHeight( aHeight: double) setWidth( aWidth: double) Write the Rectangle class in Java Phil Campbell London South Bank University

  37. public classRectangleextends Object{ private double height; private double width; // the constructor publicRectangle (double height, double width){ super(); this.height = height; this.width = width; } // End Rectangle() public double getHeight (){ returnheight; } // End getHeight() public double getWidth (){ returnwidth; } // End getWidth() public void setHeight ( double arg){ height = arg; } // End setHeight() Phil Campbell London South Bank University

  38. public voidsetWidth ( double arg){ width = arg; } // End getWidth() public double area(){ returnheight * width; } // End area() public double perimeter(){ return 2 * ( height + width); } public String toString(){ return "(" + height +"," + width + ") area= " + area() + " perimeter= " + perimeter(); } // End toString() } // End class Rectangle Phil Campbell London South Bank University

More Related