Phil Campbell London South Bank University Java 1 First Steps

Preview:

Citation preview

Phil Campbell London South Bank University

Java 1

First Steps

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

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

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

Phil Campbell London South Bank University

A Class

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{

Phil Campbell London South Bank University

public class SimpleCounter extends Object{ private int value;

Phil Campbell London South Bank University

// the constructor public SimpleCounter (int startValue){ value = startValue; } // End SimpleCounter()

Phil Campbell London South Bank University

The constructor

...places a newly created instance into a well defined initial state.

Phil Campbell London South Bank University

// a simple method public void click( ){ value++; } // End click()

Phil Campbell London South Bank University

public int getValue(){ return value; } // End getValue()

return type

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

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

Phil Campbell London South Bank University

An object is an instance of a class that has state

and behaviour.

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

Phil Campbell London South Bank University

The IDE Integrated Development Environment

Editor pane

File Chooser

Interaction pane

Phil Campbell London South Bank University

The IDE

Phil Campbell London South Bank University

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

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

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

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"

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

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

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

Recommended