1 / 30

Classes in Java

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?

deon
Download Presentation

Classes in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes in Java

  2. Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given dimensions length width

  3. 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

  4. 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> }

  5. Class Rectangle 1 • The first step is to choose a name for the class: public class Rectangle { <list of instance variables> <list of methods> }

  6. 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

  7. Defining an instance variable in Java • The syntax of a Java instance variable declaration is as follows: private <type> <instance variable name>;

  8. 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

  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”); • The corresponding headings are publicint readInt(String prompt) publicvoid println(String message)

  10. 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>

  11. 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>; }

  12. 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

  13. 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); } }

  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 • 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

  15. 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

  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. 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

  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. 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

  20. 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

  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. 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”

  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 public Square(int l) { return this.length = l; } 10 “Square”

  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); } 20 10 20 public Square(int l) { return this.length = l; }

  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); } 10 20

  26. 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

  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); } 10 20

  28. 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; }

  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); } 10 20

  30. 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

More Related