310 likes | 571 Vues
Classes in Java. Writing a Java class. Recall the program to calculate the area and perimeter of a rectangle of given dimensions. length. width. Identifying the classes. The first step is to identify what classes will be required What kinds of entity does our program deal with?
E N D
Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given dimensions length width
Identifying the classes The first step is to identify what classes will be required • What kinds of entity does our program deal with? • Put another way, what types of object will the program use? • Or, what classes will we need? We will need a class whose instances represent rectangles
attributes are usually called instance variables in Java Defining a class in Java • The syntax of a Java class declaration is as follows: public class <class name> { <list of instance variables> <list of methods> }
Class Rectangle 1 • The first step is to choose a name for the class: public class Rectangle { <list of instance variables> <list of methods> }
Class Rectangle 2 • We want two instance variables called length and width • Both will be integer numbers an integer is a positive or negative whole number public class Rectangle { privateint length; privateint width; <list of methods> } here we define two instance variables of type int we give the name of the type and then the name of the instance variable
Defining an instance variable in Java • The syntax of a Java instance variable declaration is as follows: private <type> <instance variable name>;
Defining a method in Java • Typically, a method performs a calculation and returns a result • We need to describe both: The type of result returned The calculation to be performed • So, a method definition has two parts: A heading that describes how to use the method A body that describes how the method works
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”); • The corresponding headings are publicint readInt(String prompt) publicvoid println(String message)
Defining a method’s heading 2/2 • The syntax of a Java method heading is as follows: • The syntax of each Java parameter definition is: • The result type may be void if no result is produced public <result type> <method name>(<parameters>) <type> <parameter name>
Defining a method’s body • The body of the method is just a list of variable declarations and statements just as in main • The syntax of a Java method declaration is as follows: public <result type> <method name>(<parameters>) { <list of local variables> <statement 1>; <statement 2>; . . . <statement n>; }
Class Rectangle 3 • We want two methods called calculateArea and calculatePerimeter calculating the result involves multiplying the length by the width public int calculateArea() { returnthis.length * this.width; } public int calculatePerimeter( ) { return (2 * this.length) + (2 * this.width); } both methods return an int as their result
Class Rectangle 4 class Rectangle { privateint length; privateint width; public int calculateArea() { returnthis.length * this.width; } public int calculatePerimeter() { return (2 * this.length) + (2 * this.width); } }
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 • Methods that initialise new objects are called constructors • The syntax of a Java constructor declaration is as follows: public <class name>(<parameters>) { <list of local variables> <statement 1>; <statement 2>; . . . <statement n>; } Look, no result type! The name of the method is the same as the name of the class
Class Rectangle 5 • In our constructor we need to initialise length and width • Our constructor needs to have two parameters giving these values • These two parameters are also of type int 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
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); } }
Functions and Parameters • Given f(x) = x2 + 4x + 13 • What is f(10)? - 153 • What is f(20)? - 493 • What is f(30)? - 1033 x is the “formal parameter” of function f 10 is an “actual parameter” of function f
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
Class Rectangle /* 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() } We refer to the collection of method headings as the interface of the class
Class 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; } } “public” parts of Rectangle
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); }
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); } On the screen “Square”
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 public Square(int l) { return this.length = l; } 10 “Square”
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); } 20 10 20 public Square(int l) { return this.length = l; }
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
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! 100 public int calculateArea() { return this.length * this.length; } 20 10
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
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! 400 20 10 public int calculateArea() { return this.length * this.length; }
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
Circles Want to write a Java class describing objects that represent circles radius its area Might want to ask a circle for: the length of its circumference