phil campbell london south bank university java 1 first steps
TRANSCRIPT
![Page 1: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/1.jpg)
Phil Campbell London South Bank University
Java 1
First Steps
![Page 2: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/2.jpg)
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
![Page 3: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/3.jpg)
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
![Page 4: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/4.jpg)
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
![Page 5: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/5.jpg)
Phil Campbell London South Bank University
A Class
![Page 6: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/6.jpg)
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{
![Page 7: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/7.jpg)
Phil Campbell London South Bank University
public class SimpleCounter extends Object{ private int value;
![Page 8: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/8.jpg)
Phil Campbell London South Bank University
// the constructor public SimpleCounter (int startValue){ value = startValue; } // End SimpleCounter()
![Page 9: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/9.jpg)
Phil Campbell London South Bank University
The constructor
...places a newly created instance into a well defined initial state.
![Page 10: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/10.jpg)
Phil Campbell London South Bank University
// a simple method public void click( ){ value++; } // End click()
![Page 11: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/11.jpg)
Phil Campbell London South Bank University
public int getValue(){ return value; } // End getValue()
return type
![Page 12: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/12.jpg)
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
![Page 13: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/13.jpg)
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 variableobject :an instance of the class SimpleCounter
count: SimpleCounter
value = 5
![Page 14: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/14.jpg)
Phil Campbell London South Bank University
An object is an instance of a class that has state
and behaviour.
![Page 15: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/15.jpg)
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
![Page 16: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/16.jpg)
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
![Page 17: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/17.jpg)
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(); count.click(); System.out.print( "Third value : "); System.out.println( count.getValue()); } // End of main } // End of SimpleCounterDemonstration
count
5
output
First value : 5Second value: 6Third value : 8
678
![Page 18: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/18.jpg)
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.
![Page 19: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/19.jpg)
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 : 6Second value: 12
67
count2
101112
![Page 20: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/20.jpg)
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
![Page 21: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/21.jpg)
Phil Campbell London South Bank University
Words used so farclass
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
![Page 22: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/22.jpg)
Phil Campbell London South Bank University
Words used so farclass
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
![Page 23: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/23.jpg)
Phil Campbell London South Bank University
Words used so farclass
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
![Page 24: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/24.jpg)
Phil Campbell London South Bank University
Words used so farclass
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
![Page 25: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/25.jpg)
Phil Campbell London South Bank University
Words used so farclass
reference variable
method
state
constructor
inquiry method
object
instance
Places a newly created instance into a well defined initial stateconstructor
![Page 26: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/26.jpg)
Phil Campbell London South Bank University
Words used so farclass
reference variable
method
state
constructor
query method
object
instance
A method that returns a value indicating some aspect of the state of an instanceinquiry method
![Page 27: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/27.jpg)
Phil Campbell London South Bank University
Words used so farclass
reference variable
method
state
constructor
inquiry method
object
instance
An object is an instance of a class that has state, behaviour and identity
object
![Page 28: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/28.jpg)
Phil Campbell London South Bank University
Words used so farclass
reference variable
method
state
constructor
inquiry method
object
instance
Instantiation is the process of creating a physical example of a class... an object.
instance
![Page 29: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/29.jpg)
Phil Campbell London South Bank University
The IDE Integrated Development Environment
Editor pane
File Chooser
Interaction pane
![Page 30: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/30.jpg)
Phil Campbell London South Bank University
The IDE
![Page 31: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/31.jpg)
Phil Campbell London South Bank University
![Page 32: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/32.jpg)
Phil Campbell London South Bank University
Exercise 1
Colour
hue : String
first : Colour
hue : "red"
first second third
"red"
"blue"
"green"
getHue():StringsetHue(String theHue)
Write the class Colour in Java
![Page 33: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/33.jpg)
Phil Campbell London South Bank University
public class Colour extends Object{
} // End class Colour
Colour
hue : String
getHue():StringsetHue(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()
![Page 34: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/34.jpg)
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():StringsetHue(String theHue)
Object
Write a demonstration program for the Colour Class
AttributeConstructorModifier MethodInquiry Method
![Page 35: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/35.jpg)
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"
![Page 36: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/36.jpg)
Phil Campbell London South Bank University
Exercise 2Rectangle
height : doublewidth : doubleRectangle (h : double, w : double)area(): doubleperimeter(): doublegetHeight(): doublegetWidth(): doublesetHeight( aHeight: double)setWidth( aWidth: double)
Write the Rectangle class in Java
![Page 37: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/37.jpg)
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()
![Page 38: Phil Campbell London South Bank University Java 1 First Steps](https://reader036.vdocuments.mx/reader036/viewer/2022062223/551ae4c755034606048b5b11/html5/thumbnails/38.jpg)
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