Presentation is loading. Please wait.

Presentation is loading. Please wait.

Phil Campbell London South Bank University Java 1 First Steps.

Similar presentations


Presentation on theme: "Phil Campbell London South Bank University Java 1 First Steps."— Presentation transcript:

1 Phil Campbell London South Bank University Java 1 First Steps

2 Phil Campbell London South Bank University 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

3 Phil Campbell London South Bank University 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

4 Phil Campbell London South Bank University 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

5 Phil Campbell London South Bank University A Class

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

7 Phil Campbell London South Bank University public class SimpleCounter extends Object{ private int value;

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

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

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

11 Phil Campbell London South Bank University public int getValue(){ return value; } // End getValue() return type

12 Phil Campbell London South Bank University public class SimpleCounter extends Object{ private int value; // the constructor public SimpleCounter (int startValue){ value = startValue; } // End SimpleCounter() // a simple action public void click (){ value++; } // End click() // a 'getter' public int getValue(){ return value; } // End getValue() } // End class SimpleCounter

13 Phil Campbell London South Bank University Demonstrating SimpleCounter count = new SimpleCounter( 5); count 5 public class SimpleCounterDemonstration extends Object{ public static void main( String[] args){ SimpleCounter count = null; reference variable object : an instance of the class SimpleCounter count: SimpleCounter value = 5

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

15 Phil Campbell London South Bank University Demonstrating SimpleCounter count 5 public class SimpleCounterDemonstration extends Object{ public static void main( String[] args){ SimpleCounter count = null; count = new SimpleCounter( 5); count.click( ); public void click ( ){ value++; } 6

16 Phil Campbell London South Bank University Demonstrating SimpleCounter count 5 public static void main( String[] args){ SimpleCounter count = null; count = new SimpleCounter( 5); count.click( ); public void click ( ){ value++; } 6 System.out.println( count.getValue( )) public int getValue(){ return value; } 6

17 Phil Campbell London South Bank University public class SimpleCounterDemonstration extends Object{ public static void main (String[] args){ SimpleCounter count = null; count = new SimpleCounter(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(); System.out.print( "Third value : "); System.out.println( count.getValue()); } // End of main } // End of SimpleCounterDemonstration count 5 output First value : 5 Second value: 6 Third value : 8 678

18 Phil Campbell London South Bank University 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.

19 Phil Campbell London South Bank University public class SimpleCounterDemonstration extends Object{ public static void main (String[] args){ SimpleCounter count = null; count = new SimpleCounter(5); SimpleCounter count2 = null; count2 = new SimpleCounter(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 count 5 output First value : 6 Second value: 12 67 count2 10 1112

20 Phil Campbell London South Bank University public class SimpleCounterDemonstration extends Object{ public static void main (String[] args){ SimpleCounter count = null; count = new SimpleCounter(5); SimpleCounter count2 = null; count = new SimpleCounter(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 count 5 output First value : 11 count2 10 <----ERROR 11

21 Phil Campbell London South Bank University Words used so far 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 class

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

23 Phil Campbell London South Bank University 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

24 Phil Campbell London South Bank University 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 state set of all values in

25 Phil Campbell London South Bank University 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

26 Phil Campbell London South Bank University 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

27 Phil Campbell London South Bank University 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

28 Phil Campbell London South Bank University 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

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

30 Phil Campbell London South Bank University The IDE

31 Phil Campbell London South Bank University

32 Exercise 1 Colour hue : String first : Colour hue : "red" firstsecondthird "red" "blue" "green" getHue():String setHue(String theHue) Write the class Colour in Java

33 Phil Campbell London South Bank University public class Colour extends Object{ } // End class Colour Colour hue : String getHue():String setHue(String theHue) Object public String getHue(){ return hue; } // end getHue() private String hue; public Colour (String theHue){ hue = theHue; } // End constructor() public void setHue( String theHue){ hue = theHue; } // End setHue()

34 Phil Campbell London South Bank University public class Colour extends Object{ private String hue; public Colour (String theHue){ hue = theHue; } // End constructor() public String getHue(){ return hue; } // end getHue() public void setHue( String theHue){ hue = theHue; } // End setHue() } // End class Colour Colour hue : String getHue():String setHue(String theHue) Object Write a demonstration program for the Colour Class AttributeConstructorModifier Method Inquiry Method

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

36 Phil Campbell London South Bank University 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

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

38 Phil Campbell London South Bank University public void setWidth ( double arg){ width = arg; } // End getWidth() public double area(){ return height * 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


Download ppt "Phil Campbell London South Bank University Java 1 First Steps."

Similar presentations


Ads by Google