lecture08 writing classes
lecture08 writing classes
31/05/2022 / Slide
1
Lecture
• objectives
We've been using predefined classes. Now we will learn to
write our own classes to define new objects
31/05/2022 / Slide
2
Two types of
• Class
Those that contain a main method
– Called Driver programs
– Not used to define objects
31/05/2022 / Slide
3
Objects and
• methods
Eg String class: create an object then use methods on it
– A student object
• name, studentNumber, course, address etc
– A rectangle object
• width, length
• Define a constructor
– This initialises the instance variables
31/05/2022 / Slide
7
Circle
• class
What if we wanted to create an application to work with
circles eg. calculate area, circumference ..
• What data fields might we require for a circle?
– radius
• Define the constructor
• What methods might we need to write?
– calculateArea etc
– Method to provide a value for radius ie setRadius
– Methods to enquire as to the value of radius eg getRadius
31/05/2022 / Slide
8
Building a Circle
• A Circle class
object will have the following field:
– radius. That will hold the circle’s radius.
public
Circle()
{
radius
= 0;
}
31/05/2022 / Slide
10
public class Circle
{
private double
radius;
public Circle()
{
radius = 0;
}
}
Setting the
• variables
So when we create a Circle object the variable radius is set
to 0
• To make the class useful we need to provide a method to
set it to other values
31/05/2022 / Slide
12
Header for the setRadius
Method
Return Notice the word static
Type does not appear in the
Access Method method header designed to
specifier Name work on an instance of a
class (instance method).
31/05/2022 / Slide
13
public class Circle
{
private double radius;
public Circle()
{
radius = 0;
}
public void
setRadius(double rad)
{
radius = rad;
}
}
Getting the
• variables
To retrieve the value of the data field we need to provide a
method
31/05/2022 / Slide
15
public class Circle
{
private double radius;
public Circle()
{
radius = 0;
}
public void
setRadius(double rad)
{
radius = rad;
}
public double
getRadius()
{
return radius;
}
}
Other useful
• methods
We can provide any number of useful methods that apply
to circles eg calculate area
31/05/2022 / Slide
17
public class Circle
{
private double radius;
public Circle()
{
radius = 0;
}
31/05/2022 / Slide
19
// Driver class
public class TestCircle Circle class
{
public static void main Call the constructor Circle()
(String[] args) to create an object of the
{ Circle class called circ
Circle circ = new Circle();
circ.setRadius(2.
0); Invokes the setRadius
method of the Circle
double area = class on the object
circ.calculateArea();
System.out.println (“Area of Invokes the
circle radius “ + calculateArea
circ.getRadius() + “ = “ + method on the object
} area);
} Invokes the getRadius
method of the Circle
class on the object
public class Circle
{
public class private double
TestCircle radius;
{ public
public static void main Circle()
(String [ ] args) {
{ radius
Circle circ = new Circle(); = 0;
circ.setRadius(2. }
public void setRadius(double
0); rad)
double area = {
circ.calculateArea(); radius = rad;
System.out.println (“Area of }
circle radius + public double getRadius()
circ.getRadius() + “ = “ + {
} area); return radius;
} }
public double
calculateArea()
{
} return Math.PI *
Math.pow(radius, 2);
Practic
• e
Write an instantiable class called Counter that has
– An integer instance variable called numClicks
– A constructor that sets numClicks to 0
– A method called click() that adds 1 to numClicks
– A method called getClicks() that returns numClicks
31/05/2022 / Slide
22
public class TestCounter public class Counter
{ {
public static void main (String private int numClicks;
[] args)
{
public Counter()
//create Counter object
Counter count = new {
Counter(); numClicks = 0;
}
// call click
method twice public void click()
count.click(); {
count.click(); numClicks = numClicks + 1;
}
//get number of
clicks
public int getClicks()
int num =
count.getClicks(); {
return numClicks;
// print number }
of clicks }
System.out.print(nu
Constructo
• rs kind of method that contains
A constructor is a special
instructions to set up a newly created object
• When writing a constructor, remember that:
– it has the same name as the class
– it is syntactically similar to a method
– it has no return type, not even void
– it often sets the initial values of instance variables
– it is invoked by the keyword new
31/05/2022 / Slide
24
Another instantiable
• Class
Suppose we wanted to write a program that simulates the
flipping of a coin
• Think of a coin object as an actual coin
• We could write a Coin class to define coin objects
31/05/2022 / Slide
30
// CountFlips.java Driver // Coin.java Represents a coin
public
program class
counts heads with two sides
CountFlips
{ public static void main public class Coin
(String[] args) {
{ private int face;
int result, myScore = 0, public //
yourScore = 0; Coin () Constructor:
Coin myCoin = new Coin(); {
Coin yourCoin
for (int i = 1; i =
<=new
10;Coin();
i face =
++) 1;
{ public
} void flip () // Flips the
myCoin.flip(); coin
result = {
myCoin.getFace(); } face = (int) (Math.random()
if (result == 0) * 2);
yourCoin.flip();
myScore = public int
result
myScore
= + 1; getFace()
yourCoin.getFace(); {
if (result == 0) return
} yourScore = face;
yourScore + 1;
System.out.print(“me “ + myScore } }
+ “you “ +
} yourScore
Instance
CountFlips.java
Variables
class Coin myCoin
yourCoin
face 1
31/05/2022 / Slide
32
Exercis
• e
Change the driver program to
– Create a second counter object
– Click it once
– Get and display its value
31/05/2022 / Slide
34
public class TestCounter public class Counter
{ {
public static void main (String private int numClicks;
[] args)
{ public Counter()
Counter count = new {
Counter(); numClicks = 0;
count.click(); }
count.click();
public void click()
int num = {
count.getClicks(); numClicks = numClicks + 1;
System.out.print(num); }
31/05/2022 / Slide
36
Adding a setter to
• Say we haveCounter
a requirement to be able to re-set out
numClicks variable
– Eg back to zero
– Or to any number
• We need to write a setter in the Counter class
• Creating objects
create circ as new Circle()
create coin1 as new Coin()
• Using methods
area circ.calculateArea()
circ.setRadius(num)
31/05/2022 / Slide
39
Pseudocod
• e
For instantiable classes we do not use pseudocode for
design
• We use UML – covered next week
31/05/2022 / Slide
40
Lecture
Outcomes
Today we have covered:
– writing your own instantiable classes
– creating objects from them
– we have moved into true object-oriented
programming
• Questions?
31/05/2022 / Slide
41