Download presentation
Presentation is loading. Please wait.
Published byReese Nelson Modified over 9 years ago
1
© Vinny Cahill 1 Classes in Java
2
© Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given dimensions length width
3
© Vinny Cahill 3 Identifying the classes The first step is to identify what classes will be required l What kinds of entity does our program deal with? l Put another way, what types of object will the program use? l Or, what classes will we need? We will need a class whose instances represent rectangles
4
© Vinny Cahill 4 Defining a class in Java l The syntax of a Java class declaration is as follows: public class { } attributes are usually called instance variables in Java
5
© Vinny Cahill 5 Class Rectangle 1 l The first step is to choose a name for the class: public class Rectangle { }
6
© Vinny Cahill 6 Class Rectangle 2 We want two instance variables called length and width l Both will be integer numbers an integer is a positive or negative whole number public class Rectangle { private int length; private int width; } here we define two instance variables of type int we give the name of the type and then the name of the instance variable
7
© Vinny Cahill 7 Defining an instance variable in Java l The syntax of a Java instance variable declaration is as follows: private ;
8
© Vinny Cahill 8 Defining a method in Java l Typically, a method performs a calculation and returns a result l We need to describe both: The type of result returned The calculation to be performed l So, a method definition has two parts: A heading that describes how to use the method A body that describes how the method works
9
© Vinny Cahill 9 Defining a method’s heading 1/2 Consider some of the methods in the Terminal class number = terminal.readInt(“Enter number:”); terminal.println(“Hello World”); l The corresponding headings are public int readInt(String prompt) public void println(String message)
10
© Vinny Cahill 10 Defining a method’s heading 2/2 l The syntax of a Java method heading is as follows: l The syntax of each Java parameter definition is: The result type may be void if no result is produced public ( )
11
© Vinny Cahill 11 Defining a method’s body The body of the method is just a list of variable declarations and statements just as in main l The syntax of a Java method declaration is as follows: public ( ) { ;... ; }
12
© Vinny Cahill 12 Class Rectangle 3 public int calculateArea() { return this.length * this.width; } public int calculatePerimeter( ) { return (2 * this.length) + (2 * this.width); } both methods return an int as their result calculating the result involves multiplying the length by the width We want two methods called calculateArea and calculatePerimeter
13
© Vinny Cahill 13 Class Rectangle 4 class Rectangle { private int length; private int width; public int calculateArea() { return this.length * this.width; } public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); }
14
© Vinny Cahill 14 Defining a constructor is Java We also need a method to initialise the length and width instance variables of each new object that we create l Methods that initialise new objects are called constructors l The syntax of a Java constructor declaration is as follows: public ( ) { ;... ; } Look, no result type! The name of the method is the same as the name of the class
15
© Vinny Cahill 15 In our constructor we need to initialise length and width l Our constructor needs to have two parameters giving these values These two parameters are also of type int Class Rectangle 5 public Rectangle(int l, int w) { this.length = l; this.width = w; } storing a value into a variable uses assignment “ this.width = w ” means store (assign) the value of w in(to) variable width
16
© Vinny Cahill 16 Class Rectangle 6 /* A class whose instances represent rectangles */ public class Rectangle { private int length; // used to store the length of the rectangle private int width; // used to store the width of the rectangle /* declare a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) { this.length = l; // store the value of l into length this.width = w; // store the value of w into width } /* declare a method to calculate the area of a rectangle */ public int calculateArea() { return this.length * this.width; } /* declare a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); }
17
© Vinny Cahill 17 Functions and Parameters l Given f(x) = x 2 + 4x + 13 l What is f(10)? - 153 l What is f(20)? - 493 l What is f(30)? - 1033 x is the “formal parameter” of function f 10 is an “actual parameter” of function f
18
© Vinny Cahill 18 Class Rectangle /* A class whose instances represent rectangles */ public class Rectangle { private int length; // used to store the length of the rectangle private int width; // used to store the width of the rectangle /* declare a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) { this.length = l; // store the value of l into length this.width = w; // store the value of w into width } /* declare a method to calculate the area of a rectangle */ public int calculateArea() { return this.length * this.width; } /* declare a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); } “public” parts of Rectangle
19
© Vinny Cahill 19 /* A class whose instances represent rectangles */ public class Rectangle { /* a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) /* a method to calculate the area of a rectangle */ public int calculateArea() /* a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() } Class Rectangle We refer to the collection of method headings as the interface of the class
20
© Vinny Cahill 20 Class Rectangle “public” parts of Rectangle public class Rectangle { private int length; // used to store the length of the rectangle private int width; // used to store the width of the rectangle private int area; // used to store the area of the rectangle private int perimeter; // used to store the perimeter of the rectangle /* declare a constructor to initialise new instances of class Rectangle */ public Rectangle(int l, int w) { this.length = l; // store the value of l into length this.width = w; // store the value of w into width this.area = this.length * this.width; // calculate the area this.perimeter = (2 * this.length) + (2 * this.width); // calculate the perimeter } /* declare a method to calculate the area of a rectangle */ public int calculateArea() { return this.area; } /* declare a method to calculate the perimeter of a rectangle */ public int calculatePerimeter() { return this.perimeter; }
21
© Vinny Cahill 21 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); }
22
© Vinny Cahill 22 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } “Square” On the screen
23
© Vinny Cahill 23 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 10 “Square” 10 public Square(int l) { return this.length = l; }
24
© Vinny Cahill 24 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 10 20 public Square(int l) { return this.length = l; } 20
25
© Vinny Cahill 25 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 1020
26
© Vinny Cahill 26 2010 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } calculateArea! public int calculateArea() { return this.length * this.length; } 100
27
© Vinny Cahill 27 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 1020
28
© Vinny Cahill 28 2010 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } calculateArea! public int calculateArea() { return this.length * this.length; } 400
29
© Vinny Cahill 29 Transfer of control public static void main(String[] args) { Terminal window; Square shape1, shape2; int area; window = new Terminal(“Square”); shape1 = new Square(10); shape2 = new Square(20); area = shape1.calculateArea(); window.println(“Area is: ” + area); area = shape2.calculateArea(); window.println(“Area is: ” + area); } 1020
30
© Vinny Cahill 30 Circles Want to write a Java class describing objects that represent circles radius Might want to ask a circle for: its area the length of its circumference
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.