Java Assignments For Students Pdf Printer

Exercises on Classes

Ex: The Circle Class (An Introduction to Classes and Instances)

This first exercise shall lead you through all the basic concepts in OOP.

A class called is designed as shown in the following class diagram. It contains:

  • Two instance variables: (of the type ) and (of the type ), with default value of and "", respectively.
  • Two overloaded constructors - a default constructor with no argument, and a constructor which takes a double argument for radius.
  • Two methods: and , which return the radius and area of this instance, respectively.

The source codes for is as follows:

public class Circle { private double radius; private String color; public Circle() { radius = 1.0; color = "red"; } public Circle(double r) { radius = r; color = "red"; } public double getRadius() { return radius; } public double getArea() { return radius*radius*Math.PI; } }

Compile "". Can you run the class? Why?

This class does not have a method. Hence, it cannot be run directly. This class is a “building block” and is meant to be used in another program.

Let us write a test program called (in another source file called ) which uses the class, as follows:

public class TestCircle { public static void main(String[] args) { Circle c1 = new Circle(); System.out.println("The circle has radius of " + c1.getRadius() + " and area of " + c1.getArea()); Circle c2 = new Circle(2.0); System.out.println("The circle has radius of " + c2.getRadius() + " and area of " + c2.getArea()); } }

Now, run the and study the results.

More Basic OOP Concepts
  1. Constructor: Modify the class to include a third constructor for constructing a instance with two arguments - a for and a for . public Circle (double r, String c) { ...... } Modify the test program to construct an instance of using this constructor.
  2. Getter: Add a getter for variable for retrieving the of this instance. public String getColor() { ...... } Modify the test program to test this method.
  3. public vs. private: In , can you access the instance variable directly (e.g., ); or assign a new value to (e.g., )? Try it out and explain the error messages.
  4. Setter: Is there a need to change the values of and of a instance after it is constructed? If so, add two methods called setters for changing the and of a instance as follows: public void setRadius(double newRadius) { radius = newRadius; } public void setColor(String newColor) { ...... } Modify the to test these methods, e.g., Circle c4 = new Circle(); c4.setRadius(5.0); System.out.println("radius is: " + c4.getRadius()); c4.setColor(......); System.out.println("color is: " + c4.getColor()); System.out.println(c4.setRadius(4.0));
  5. Keyword "this": Instead of using variable names such as (for ) and (for ) in the methods' arguments, it is better to use variable names (for ) and (for ) and use the special keyword "" to resolve the conflict between instance variables and methods' arguments. For example, private double radius; public Circle(double radius) { this.radius = radius; color = ....... } public void setRadius(double radius) { this.radius = radius; } Modify ALL the constructors and setters in the class to use the keyword "".
  6. Method toString(): Every well-designed Java class should contain a method called that returns a short description of the instance (in a return type of ). The method can be called explicitly (via ) just like any other method; or implicitly through . If an instance is passed to the method, the method of that instance will be invoked implicitly. For example, include the following methods to the class: public String toString() { return "Circle[radius=" + radius + " color=" + color + "]"; } Try calling method explicitly, just like any other method: Circle c1 = new Circle(5.0); System.out.println(c1.toString()); is called implicitly when an instance is passed to method, for example, Circle c2 = new Circle(1.2); System.out.println(c2.toString()); System.out.println(c2); System.out.println("Operator '+' invokes toString() too: " + c2);

The final class diagram for the class is as follows:

Ex: A Simplified Circle Class

Ex: The Rectangle Class

Ex: The Employee Class

Ex: The InvoiceItem Class

Ex: The Account Class

Ex: The Date Class

Ex: The Time Class

Exercises on Composition

Ex: The Author and Book Classes (An Introduction to OOP Composition)

This first exercise shall lead you through all the concepts involved in OOP Composition.

A class called (as shown in the class diagram) is designed to model a book's author. It contains:

  • Three instance variables: (), (), and ( of either or );
  • One constructor to initialize the , and with the given values; public Author (String name, String email, char gender) {......} (There is no default constructor for Author, as there are no defaults for name, email and gender.)
  • getters/setters: , , , and ;
    (There are no setters for and , as these attributes cannot be changed.)
  • A method that returns "", e.g., "".

Write the class. Also write a test driver called to test all the methods, e.g.,

Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm'); System.out.println(ahTeck); ahTeck.setEmail("paulTan@nowhere.com"); System.out.println("name is: " + ahTeck.getName()); System.out.println("eamil is: " + ahTeck.getEmail()); System.out.println("gender is: " + ahTeck.getGender());

A class called is designed (as shown in the class diagram) to model a book written by one author. It contains:

  • Four instance variables: (), (of the class you have just created, assume that a book has one and only one author), (), and ();
  • Two constructors: public Book (String name, Author author, double price) { ...... } public Book (String name, Author author, double price, int qty) { ...... }
  • public methods , , , , , .
  • A that returns "".  You should reuse ’s .

Write the class (which uses the class written earlier). Also write a test driver called to test all the methods in the class . Take Note that you have to construct an instance of before you can construct an instance of . E.g.,

Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm'); System.out.println(ahTeck); Book dummyBook = new Book("Java for dummy", ahTeck, 19.95, 99); System.out.println(dummyBook); dummyBook.setPrice(29.95); dummyBook.setQty(28); System.out.println("name is: " + dummyBook.getName()); System.out.println("price is: " + dummyBook.getPrice()); System.out.println("qty is: " + dummyBook.getQty()); System.out.println("Author is: " + dummyBook.getAuthor()); System.out.println("Author's name is: " + dummyBook.getAuthor().getName()); System.out.println("Author's email is: " + dummyBook.getAuthor().getEmail()); Book anotherBook = new Book("more Java", new Author("Paul Tan", "paul@somewhere.com", 'm'), 29.95); System.out.println(anotherBook);

Take note that both and classes have a variable called . However, it can be differentiated via the referencing instance. For a instance says , refers to the of the book; whereas for an 's instance say , refers to the of the author. There is no need (and not recommended) to call the variables and .

TRY:

  1. Printing the and of the author from a instance. (Hint: , ).
  2. Introduce new methods called , , in the class to return the , and of the author of the book. For example, public String getAuthorName() { return author.getName(); }

Exercise (Advanced): Book and Author Classes Again - An Array of Objects as an Instance Variable

In the earlier exercise, a book is written by one and only one author. In reality, a book can be written by one or more author. Modify the class to support one or more authors by changing the instance variable to an array.

Notes:

  • The constructors take an array of (i.e., ), instead of an instance. In this design, once a instance is constructor, you cannot add or remove author.
  • The method shall return "".

You are required to:

  1. Write the code for the class. You shall re-use the class written earlier.
  2. Write a test driver (called ) to test the class.

Hints:

Author[] authors = new Author[2]; authors[0] = new Author("Tan Ah Teck", "AhTeck@somewhere.com", 'm'); authors[1] = new Author("Paul Tan", "Paul@nowhere.com", 'm'); Book javaDummy = new Book("Java for Dummy", authors, 19.99, 99); System.out.println(javaDummy);

Ex: The MyPoint Class

A class called , which models a 2D point with and coordinates, is designed as shown in the class diagram. It contains:

  • Two instance variables () and ().
  • A default (or "no-argument" or "no-arg") constructor that construct a point at the default location of .
  • A overloaded constructor that constructs a point with the given and coordinates.
  • Getter and setter for the instance variables and .
  • A method to set both and .
  • A method which returns the x and y in a 2-element array.
  • A method that returns a string description of the instance in the format "".
  • A method called that returns the distance from this point to another point at the given coordinates, e.g., MyPoint p1 = new MyPoint(3, 4); System.out.println(p1.distance(5, 6));
  • An overloaded that returns the distance from this point to the given instance (called ), e.g., MyPoint p1 = new MyPoint(3, 4); MyPoint p2 = new MyPoint(5, 6); System.out.println(p1.distance(p2));
  • Another overloaded method that returns the distance from point to the origin , e.g., MyPoint p1 = new MyPoint(3, 4); System.out.println(p1.distance());

You are required to:

  1. Write the code for the class . Also write a test program (called ) to test all the methods defined in the class.
    Hints: public double distance(int x, int y) { int xDiff = this.x – x; int yDiff = ...... return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } public double distance(MyPoint another) { int xDiff = this.x – another.x; ....... } MyPoint p1 = new MyPoint(); System.out.println(p1); p1.setX(8); p1.setY(6); System.out.println("x is: " + p1.getX()); System.out.println("y is: " + p1.getY()); p1.setXY(3, 0); System.out.println(p1.getXY()[0]); System.out.println(p1.getXY()[1]); System.out.println(p1); MyPoint p2 = new MyPoint(0, 4); System.out.println(p2); System.out.println(p1.distance(p2)); System.out.println(p2.distance(p1)); System.out.println(p1.distance(5, 6)); System.out.println(p1.distance());
  2. Write a program that allocates points in an array of , and initializes to , , ... .
    Hints: You need to allocate the array, as well as each of the 10 instances.  In other words, you need to issue 11 , 1 for the array and 10 for the instances. MyPoint[] points = new MyPoint[10]; for (int i = 0; i < points.length; i++) { points[i] = new MyPoint(...); }

Notes: Point is such a common entity that JDK certainly provided for in all flavors.

Ex: The MyCircle and MyPoint Classes

A class called , which models a circle with a and a , is designed as shown in the class diagram. The class uses an instance of class (created in the previous exercise) as its .

The class contains:

  • Two instance variables: (an instance of ) and ().
  • A constructor that constructs a circle with the given center's (, ) and .
  • An overloaded constructor that constructs a given a instance as , and .
  • A default constructor that construct a circle with center at and radius of .
  • Various getters and setters.
  • A method that returns a string description of this instance in the format "". You shall reuse the of .
  • and methods that return the area and circumference of circle in .
  • A method that returns the distance of the centers from instance and the given instance.  You should use ’s method to compute this distance.

Write the class. Also write a test driver (called ) to test all the public methods defined in the class.

Hints:

public MyCircle(int x, int y, int radius) { center = new MyPoint(x, y); this.radius = radius; } public MyCircle(MyPoint center, int radius) { this.center = center; ...... } public MyCircle() { center = new MyPoint(.....); this.radius = ...... } public int getCenterX() { return center.getX(); } public double distance(MyCircle another) { return center.distance(another.center); }

Ex: The MyTriangle and MyPoint Classes

A class called , which models a triangle with 3 vertices, is designed as shown. The class uses three instances (created in the earlier exercise) as its three vertices.

It contains:

  • Three instance variables , , (instances of ), for the three vertices.
  • A constructor that constructs a with three set of coordinates, , , .
  • An overloaded constructor that constructs a given three instances of .
  • A method that returns a string description of the instance in the format "".
  • A method that returns the length of the perimeter in double. You should use the method of to compute the perimeter.
  • A method , which prints "" if all the three sides are equal, "" if any two of the three sides are equal, or "" if the three sides are different.

Write the class. Also write a test driver (called ) to test all the methods defined in the class.

Ex: The MyRectangle and MyPoint Classes

Design a class which is composed of two instances as its top-left and bottom-right corners. Draw the class diagrams, write the codes, and write the test drivers.

Ex: The Customer and Invoice classes

The class models a customer is design as shown in the class diagram. Write the codes for the class and a test driver to test all the methods.

The class, design as shown in the class diagram, composes a instance (written earlier) as its member. Write the codes for the class and a test driver to test all the methods.

Ex: The Customer and Account classes

The class models a customer is design as shown in the class diagram. Write the codes for the class and a test driver to test all the methods.

The class models a bank account, design as shown in the class diagram, composes a instance (written earlier) as its member. Write the codes for the class and a test driver to test all the methods.

More Exercises on Classes

Ex: The MyComplex class

A class called , which models complex numbers , is designed as shown in the class diagram. It contains:

  • Two instance variable named () and () which stores the real and imaginary parts of the complex number, respectively.
  • A constructor that creates a instance with the given real and imaginary values.
  • A default constructor that create a MyComplex at .
  • Getters and setters for instance variables and .
  • A method to set the value of the complex number.
  • A that returns "" where and are the real and imaginary parts, respectively.
  • Methods and that returns if this complex number is real or imaginary, respectively.
    Hints: return (imag == 0);
  • A method that returns if complex number is equal to the given complex number (real, imag).
    Hints: return (this.real == real && this.imag == imag);
  • An overloaded that returns if this complex number is equal to the given instance .
    Hints: return (this.real == another.real && this.imag == another.imag);
  • A method that returns the magnitude of this complex number. magnitude(x+yi) = Math.sqrt(x*x + y*y)
  • Methods that returns the argument of this complex number in radians (). arg(x+yi) = Math.atan2(y, x) (in radians) Note: The library has two arc-tangent methods, and . We commonly use the instead of to avoid division by zero. Read the documentation of class in package .
  • Methods and that adds and subtract the given instance (called ), into/from instance and returns instance. (a + bi) + (c + di) = (a+c) + (b+d)i (a + bi) - (c + di) = (a-c) + (b-d)i Hints: return this;
  • Methods and that adds and subtract instance with the given instance called , and returns a new instance containing the result.
    Hint: return new MyComplex(..., ...);
  • Methods and that multiplies and divides instance with the given instance , and keeps the result in instance, and returns this instance. (a + bi) * (c + di) = (ac - bd) + (ad + bc)i (a + bi) / (c + di) = [(a + bi) * (c – di)] / (c*c + d*d)
  • A method that operates on instance and returns instance containing the complex conjugate. conjugate(x+yi) = x - yi

You are required to:

  1. Write the class.
  2. Write a test driver to test all the methods defined in the class.
  3. Write an application called that uses the class. The application shall prompt the user for two complex numbers, print their values, check for real, imaginary and equality, and carry out all the arithmetic operations. Enter complex number 1 (real and imaginary part): 1.1 2.2 Enter complex number 2 (real and imaginary part): 3.3 4.4 Number 1 is: (1.1 + 2.2i) (1.1 + 2.2i) is NOT a pure real number (1.1 + 2.2i) is NOT a pure imaginary number Number 2 is: (3.3 + 4.4i) (3.3 + 4.4i) is NOT a pure real number (3.3 + 4.4i) is NOT a pure imaginary number (1.1 + 2.2i) is NOT equal to (3.3 + 4.4i) (1.1 + 2.2i) + (3.3 + 4.4i) = (4.4 + 6.6000000000000005i) (1.1 + 2.2i) - (3.3 + 4.4i) = (-2.1999999999999997 + -2.2i)

Take note that there are a few flaws in the design of this class, which was introduced solely for teaching purpose:

  • Comparing s in using "" may produce unexpected outcome. For example, returns . It is common to define a small threshold called (set to about ) for comparing floating point numbers.
  • The method , produce new instances, whereas , , , and modify instance. There is inconsistency in the design (introduced for teaching purpose).

Also take note that methods such as returns an instance of . Hence, you can place the result inside a (which implicitly invoke the ). You can also chain the operations, e.g., (same as , or .

Ex: The MyPolynomial Class

A class called , which models polynomials of degree- (see equation), is designed as shown in the class diagram.

It contains:

  • An instance variable named , which stores the coefficients of the n-degree polynomial in a array of size , where c0 is kept at index 0.
  • A constructor that takes a variable number of doubles to initialize the coeffs array, where the first argument corresponds to c0.
    The three dots is known as varargs (variable number of arguments), which is a new feature introduced in JDK 1.5. It accepts an array or a sequence of comma-separated arguments. The compiler automatically packs the comma-separated arguments in an array. The three dots can only be used for the last argument of the method.
    Hints: public class MyPolynomial { private double[] coeffs; public MyPolynomial(double... coeffs) { this.coeffs = coeffs; } ...... } MyPolynomial p1 = new MyPolynomial(1.1, 2.2, 3.3); MyPolynomial p1 = new MyPolynomial(1.1, 2.2, 3.3, 4.4, 5.5); Double coeffs = {1.2, 3.4, 5.6, 7.8} MyPolynomial p2 = new MyPolynomial(coeffs);
  • A method that returns the degree of this polynomial.
  • A method that returns "cnx^n+cn-1x^(n-1)+...+c1x+c0".
  • A method that evaluate the polynomial for the given , by substituting the given into the polynomial expression.
  • Methods and that adds and multiplies this polynomial with the given instance , and returns instance that contains the result.

Write the class. Also write a test driver (called ) to test all the methods defined in the class.

Question: Do you need to keep the degree of the polynomial as an instance variable in the class in Java? How about C/C++? Why?

Ex: Using JDK's BigInteger Class

Recall that primitive integer type , , and represent 8-, 16-, 32-, and 64-bit signed integers, respectively. You cannot use them for integers bigger than 64 bits. Java API provides a class called in a package called . Study the API of the class (Java API ⇒ From "Packages", choose "java.math" " From "classes", choose "BigInteger" " Study the constructors (choose "CONSTR") on how to construct a instance, and the public methods available (choose "METHOD"). Look for methods for adding and multiplying two .

Write a program called that:

  1. adds "11111111111111111111111111111111111111111111111111111111111111" to "22222222222222222222222222222222222222222222222222" and prints the result.
  2. multiplies the above two number and prints the result.

Hints:

import java.math.BigInteger public class TestBigInteger { public static void main(String[] args) { BigInteger i1 = new BigInteger(...); BigInteger i2 = new BigInteger(...); System.out.println(i1.add(i2)); ....... } }

Ex: The MyTime Class

A class called , which models a time instance, is designed as shown in the class diagram.

It contains the following instance variables:

  • : between 0 to 23.
  • : between 0 to 59.
  • : between 0 to 59.

You are required to perform input validation.

It contains the following methods:

  • : It shall check if the given , and are valid before setting the instance variables.
    (Advanced: Otherwise, it shall throw an with the message "Invalid hour, minute, or second!".)
  • Setters , , : It shall check if the parameters are valid, similar to the above.
  • Getters , , .
  • : returns "".
  • : Update this instance to the next second and return this instance. Take note that the of is .
  • , , , , : similar to the above.

Write the code for the class. Also write a test driver (called ) to test all the methods defined in the class.

Ex: The MyDate Class

A class called , which models a date instance, is defined as shown in the class diagram.

The class contains the following instance variables:

  • (): Between to .
  • (): Between (Jan) to (Dec).
  • (): Between to , where the last day depends on the month and whether it is a leap year for Feb ().

It also contains the following variables (drawn with underlined in the class diagram):

  • (), (), and (): variables, initialized as shown, which are used in the methods.

The class has the following methods (drawn with underlined in the class diagram):

  • : returns if the given is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400.
  • : returns if the given , , and constitute a valid date. Assume that is between and , is between (Jan) to (Dec) and shall be between and depending on the and whether it is a leap year on Feb.
  • : returns the day of the week, where for Sun, for Mon, ..., for Sat, for the given date. Assume that the date is valid. Read the earlier exercise on how to determine the day of the week (or Wiki "Determination of the day of the week").

The class has one constructor, which takes 3 parameters: , and . It shall invoke method (to be described later) to set the instance variables.

The class has the following methods:

  • : It shall invoke the method to verify that the given , and constitute a valid date.
    (Advanced: Otherwise, it shall throw an with the message "Invalid year, month, or day!".)
  • : It shall verify that the given is between and .
    (Advanced: Otherwise, it shall throw an with the message "Invalid year!".)
  • : It shall verify that the given is between and .
    (Advanced: Otherwise, it shall throw an with the message "Invalid month!".)
  • : It shall verify that the given is between and , where depends on the and whether it is a leap year for Feb.
    (Advanced: Otherwise, it shall throw an with the message "Invalid month!".)
  • , , : return the value for the , and , respectively.
  • : returns a date string in the format "", e.g., "Tuesday 14 Feb 2012".
  • : update instance to the next day and return instance. Take note that for shall be .
  • : update instance to the next month and return instance. Take note that for shall be .
  • : update instance to the next year and return instance. Take note that for shall be .
    (Advanced: throw an with the message "Year out of range!" if year > 9999.)
  • , , : similar to the above.

Write the code for the class.

Use the following test statements to test the class:

MyDate d1 = new MyDate(2012, 2, 28); System.out.println(d1); System.out.println(d1.nextDay()); System.out.println(d1.nextDay()); System.out.println(d1.nextMonth()); System.out.println(d1.nextYear()); MyDate d2 = new MyDate(2012, 1, 2); System.out.println(d2); System.out.println(d2.previousDay()); System.out.println(d2.previousDay()); System.out.println(d2.previousMonth()); System.out.println(d2.previousYear()); MyDate d3 = new MyDate(2012, 2, 29); System.out.println(d3.previousYear()); // MyDate d4 = new MyDate(2099, 11, 31); // MyDate d5 = new MyDate(2011, 2, 29);

Write a test program that tests the in a loop, by printing the dates from to .

Ex: Bouncing Balls - Ball and Container Classes

A class called is designed as shown in the class diagram.

The class contains the following instance variables:

  • , and , which represent the ball's center co-ordinates and the radius, respectively.
  • () and (), which represent the displacement (movement) per step, in the and direction respectively.

The class contains the following methods:

  • A constructor which accepts , , , , and as arguments. For user friendliness, user specifies (in pixels per step) and (in degrees in the range of ). For the internal operations, the and are to be converted to in the internal representation. Note that the y-axis of the Java graphics coordinate system is inverted, i.e., the origin is located at the top-left corner.
    Δx = d × cos(θ) Δy = -d × sin(θ)
  • Getter and setter for all the instance variables.
  • A method which move the ball by one step. x += Δx y += Δy
  • which reflects the ball horizontally (i.e., hitting a vertical wall) Δx = -Δx Δy no changes
  • (the ball hits a horizontal wall). Δx no changes Δy = -Δy
  • which prints the message "".

Write the class. Also write a test program to test all the methods defined in the class.


A class called , which represents the enclosing box for the ball, is designed as shown in the class diagram. It contains:

  • Instance variables and which denote the top-left and bottom-right corners of the rectangular box.
  • A constructor which accepts of the top-left corner, and as argument, and converts them into the internal representation (i.e., ). and is used in the argument for safer operation (there is no need to check the validity of etc.).
  • A method that returns "".
  • A method called , which check if the given is outside the bounds of the container box. If so, it invokes the 's and/or to change the movement direction of the ball, and returns . public boolean collidesWith(Ball ball) { if (ball.getX() - ball.getRadius() <= this.x1 || ball.getX() - ball.getRadius() >= this.x2) { ball.reflectHorizontal(); return true; } ...... }

Use the following statements to test your program:

Ball ball = new Ball(50, 50, 5, 10, 30); Container box = new Container(0, 0, 100, 100); for (int step = 0; step < 100; ++step) { ball.move(); box.collidesWith(ball); System.out.println(ball); }

Ex: The Ball and Player Classes

The class, which models the ball in a soccer game, is designed as shown in the class diagram. Write the codes for the class and a test driver to test all the methods.

The class, which models the players in a soccer game, is designed as shown in the class diagram. The Player interacts with the Ball (written earlier). Write the codes for the class and a test driver to test all the methods. Make your assumption for the .

Can you write a very simple soccer game with 2 teams of players and a ball, inside a soccer field?

Exercises on Inheritance

Ex: The Circle and Cylinder Classes

This exercise shall guide you through the important concepts in inheritance.

In this exercise, a subclass called is derived from the superclass as shown in the class diagram (where an an arrow pointing up from the subclass to its superclass). Study how the subclass invokes the superclass' constructors (via and ) and inherits the variables and methods from the superclass .

You can reuse the class that you have created in the previous exercise. Make sure that you keep "" in the same directory.

public class Cylinder extends Circle { private double height; public Cylinder() { super(); height = 1.0; } public Cylinder(double height) { super(); this.height = height; } public Cylinder(double radius, double height) { super(radius); this.height = height; } public double getHeight() { return height; } public double getVolume() { return getArea()*height; } }

Write a test program (says ) to test the class created, as follow:

public class TestCylinder { public static void main (String[] args) { Cylinder c1 = new Cylinder(); System.out.println("Cylinder:" + " radius=" + c1.getRadius() + " height=" + c1.getHeight() + " base area=" + c1.getArea() + " volume=" + c1.getVolume()); Cylinder c2 = new Cylinder(10.0); System.out.println("Cylinder:" + " radius=" + c2.getRadius() + " height=" + c2.getHeight() + " base area=" + c2.getArea() + " volume=" + c2.getVolume()); Cylinder c3 = new Cylinder(2.0, 10.0); System.out.println("Cylinder:" + " radius=" + c3.getRadius() + " height=" + c3.getHeight() + " base area=" + c3.getArea() + " volume=" + c3.getVolume()); } }

Method Overriding and "Super": The subclass inherits method from its superclass Circle. Try overriding the method in the subclass to compute the surface area (=2π×radius×height + 2×base-area) of the cylinder instead of base area. That is, if is called by a instance, it returns the area. If is called by a instance, it returns the surface area of the cylinder.

If you override the in the subclass , the no longer works. This is because the uses the overridden method found in the same class. (Java runtime will search the superclass only if it cannot locate the method in this class). Fix the .

Hints: After overridding the in subclass , you can choose to invoke the of the superclass by calling .

TRY:

Provide a method to the class, which overrides the inherited from the superclass , e.g.,

@Override public String toString() { return "Cylinder: subclass of " + super.toString() + " height=" + height; }

Try out the method in .

Note: is known as annotation (introduced in JDK 1.5), which asks compiler to check whether there is such a method in the superclass to be overridden. This helps greatly if you misspell the name of the . If is not used and is misspelled as , it will be treated as a new method in the subclass, instead of overriding the superclass. If is used, the compiler will signal an error. annotation is optional, but certainly nice to have.

Ex: Superclass Person and its subclasses

Ex: Point2D and Point3D

Ex: Point and MovablePoint

Ex: Superclass Shape and its subclasses Circle, Rectangle and Square

Write a superclass called (as shown in the class diagram), which contains:

  • Two instance variables () and ().
  • Two constructors: a no-arg (no-argument) constructor that initializes the to "green" and to , and a constructor that initializes the and to the given values.
  • Getter and setter for all the instance variables. By convention, the getter for a variable is called (instead of for all the other types).
  • A method that returns "".

Write a test program to test all the methods defined in .

Write two subclasses of called and , as shown in the class diagram.

The class contains:

  • An instance variable ().
  • Three constructors as shown. The no-arg constructor initializes the radius to .
  • Getter and setter for the instance variable .
  • Methods and .
  • Override the method inherited, to return "", where is the output of the method from the superclass.

The class contains:

  • Two instance variables () and ().
  • Three constructors as shown. The no-arg constructor initializes the and to .
  • Getter and setter for all the instance variables.
  • Methods and .
  • Override the method inherited, to return "", where is the output of the method from the superclass.

Write a class called , as a subclass of . Convince yourself that can be modeled as a subclass of . has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.

  • Provide the appropriate constructors (as shown in the class diagram). Hint: public Square(double side) { super(side, side); }
  • Override the method to return "", where is the output of the method from the superclass.
  • Do you need to override the and ? Try them out.
  • Override the and to change both the and , so as to maintain the square geometry.

Exercises on Composition vs Inheritance

They are two ways to reuse a class in your applications: composition and inheritance.

Ex: The Point and Line Classes

Let us begin with composition with the statement "a line composes of two points".

Complete the definition of the following two classes: and . The class composes 2 instances of class , representing the beginning and ending points of the line. Also write test classes for and (says and ).

public class Point { private int x; private int y; public Point (int x, int y) {......} public String toString() { return "Point: (" + x + "," + y + ")"; } public int getX() {......} public int getY() {......} public void setX(int x) {......} public void setY(int y) {......} public void setXY(int x, int y) {......} } public class TestPoint { public static void main(String[] args) { Point p1 = new Point(10, 20); System.out.println(p1); ...... } } public class Line { private Point begin; private Point end; public Line (Point begin, Point end) { this.begin = begin; ...... } public Line (int beginX, int beginY, int endX, int endY) { begin = new Point(beginX, beginY); ...... } public String toString() { ...... } public Point getBegin() { ...... } public Point getEnd() { ...... } public void setBegin(......) { ...... } public void setEnd(......) { ...... } public int getBeginX() { ...... } public int getBeginY() { ...... } public int getEndX() { ...... } public int getEndY() { ...... } public void setBeginX(......) { ...... } public void setBeginY(......) { ...... } public void setBeginXY(......) { ...... } public void setEndX(......) { ...... } public void setEndY(......) { ...... } public void setEndXY(......) { ...... } public int getLength() { ...... } public double getGradient() { ...... } } public class TestLine { public static void main(String[] args) { Line l1 = new Line(0, 0, 3, 4); System.out.println(l1); Point p1 = new Point(...); Point p2 = new Point(...); Line l2 = new Line(p1, p2); System.out.println(l2); ... } }

The class diagram for composition is as follows (where a diamond-hollow-head arrow pointing to its constituents):

Instead of composition, we can design a class using . Instead of "a line composes of two points", we can say that "a line is a point extended by another point", as shown in the following class diagram:

Let's re-design the class (called ) as a subclass of class . inherits the starting point from its superclass , and adds an ending point. Complete the class definition. Write a testing class called to test .

public class LineSub extends Point { Point end; public LineSub (int beginX, int beginY, int endX, int endY) { super(beginX, beginY); this.end = new Point(endX, endY); } public LineSub (Point begin, Point end) { super(begin.getX(), begin.getY()); this.end = end; } public String toString() { ... } public Point getBegin() { ... } public Point getEnd() { ... } public void setBegin(...) { ... } public void setEnd(...) { ... } public int getBeginX() { ... } public int getBeginY() { ... } public int getEndX() { ... } public int getEndY() { ... } public void setBeginX(...) { ... } public void setBeginY(...) { ... } public void setBeginXY(...) { ... } public void setEndX(...) { ... } public void setEndY(...) { ... } public void setEndXY(...) { ... } public int getLength() { ... } public double getGradient() { ... } }

Summary: There are two approaches that you can design a line, or . "A line composes two points" or "A line is a point extended with another point"”. Compare the and designs: uses composition and uses inheritance. Which design is better?

Ex: The Circle and Cylinder Classes Using Composition

Try rewriting the of the previous exercise using composition (as shown in the class diagram) instead of inheritance. That is, "a cylinder is composed of a base circle and a height".

public class Cylinder { private Circle base; private double height; public Cylinder() { base = new Circle(); height = 1.0; } ...... }

Which design (inheritance or composition) is better?

Exercises on Polymorphism, Abstract Classes and Interfaces

Ex: Abstract Superclass Shape and Its Concrete Subclasses

Rewrite the superclass and its subclasses , and , as shown in the class diagram.

In this exercise, shall be defined as an class, which contains:

  • Two instance variables () and (). The variables can be accessed by its subclasses and classes in the same package. They are denoted with a sign in the class diagram.
  • Getter and setter for all the instance variables, and .
  • Two methods and (shown in italics in the class diagram).

The subclasses and shall override the methods and and provide the proper implementation. They also override the .

Write a test class to test these statements involving polymorphism and explain the outputs. Some statements may trigger compilation errors. Explain the errors, if any.

Shape s1 = new Circle(5.5, "RED", false); System.out.println(s1); System.out.println(s1.getArea()); System.out.println(s1.getPerimeter()); System.out.println(s1.getColor()); System.out.println(s1.isFilled()); System.out.println(s1.getRadius()); Circle c1 = (Circle)s1; System.out.println(c1); System.out.println(c1.getArea()); System.out.println(c1.getPerimeter()); System.out.println(c1.getColor()); System.out.println(c1.isFilled()); System.out.println(c1.getRadius()); Shape s2 = new Shape(); Shape s3 = new Rectangle(1.0, 2.0, "RED", false); System.out.println(s3); System.out.println(s3.getArea()); System.out.println(s3.getPerimeter()); System.out.println(s3.getColor()); System.out.println(s3.getLength()); Rectangle r1 = (Rectangle)s3; System.out.println(r1); System.out.println(r1.getArea()); System.out.println(r1.getColor()); System.out.println(r1.getLength()); Shape s4 = new Square(6.6); System.out.println(s4); System.out.println(s4.getArea()); System.out.println(s4.getColor()); System.out.println(s4.getSide()); Rectangle r2 = (Rectangle)s4; System.out.println(r2); System.out.println(r2.getArea()); System.out.println(r2.getColor()); System.out.println(r2.getSide()); System.out.println(r2.getLength()); Square sq1 = (Square)r2; System.out.println(sq1); System.out.println(sq1.getArea()); System.out.println(sq1.getColor()); System.out.println(sq1.getSide()); System.out.println(sq1.getLength());

What is the usage of the method and class?

Ex: Polymorphism

Examine the following codes and draw the class diagram.

abstract public class Animal { abstract public void greeting(); } public class Cat extends Animal { @Override public void greeting() { System.out.println("Meow!"); } } public class Dog extends Animal { @Override public void greeting() { System.out.println("Woof!"); } public void greeting(Dog another) { System.out.println("Woooooooooof!"); } } public class BigDog extends Dog { @Override public void greeting() { System.out.println("Woow!"); } @Override public void greeting(Dog another) { System.out.println("Woooooowwwww!"); } }

Explain the outputs (or error) for the following test program.

public class TestAnimal { public static void main(String[] args) { Cat cat1 = new Cat(); cat1.greeting(); Dog dog1 = new Dog(); dog1.greeting(); BigDog bigDog1 = new BigDog(); bigDog1.greeting(); Animal animal1 = new Cat(); animal1.greeting(); Animal animal2 = new Dog(); animal2.greeting(); Animal animal3 = new BigDog(); animal3.greeting(); Animal animal4 = new Animal(); Dog dog2 = (Dog)animal2; BigDog bigDog2 = (BigDog)animal3; Dog dog3 = (Dog)animal3; Cat cat2 = (Cat)animal2; dog2.greeting(dog3); dog3.greeting(dog2); dog2.greeting(bigDog2); bigDog2.greeting(dog2); bigDog2.greeting(bigDog1); } }

Ex: Interface Movable and its implementations MovablePoint and MovableCircle

Suppose that we have a set of objects with some common behaviors: they could move up, down, left or right. The exact behaviors (such as how to move and how far to move) depend on the objects themselves. One common way to model these common behaviors is to define an interface called , with methods , , and . The classes that implement the interface will provide actual implementation to these methods.

Let's write two concrete classes - and - that implement the Movable interface.

The code for the interface is straight forward.

public interface Movable { public void moveUp(); ...... }

For the class, declare the instance variable , , and with package access as shown with in the class diagram (i.e., classes in the same package can access these variables directly). For the class, use a to represent its center (which contains four variable , , and ). In other words, the composes a , and its .

public class MovablePoint implements Movable { int x, y, xSpeed, ySpeed; public MovablePoint(int x, int y, int xSpeed, int ySpeed) { this.x = x; ...... } ...... @Override public void moveUp() { y -= ySpeed; } ...... } public class MovableCircle implements Movable { private MovablePoint center; private int radius; public MovableCircle(int x, int y, int xSpeed, int ySpeed, int radius) { center = new MovablePoint(x, y, xSpeed, ySpeed); ...... } ...... @Override public void moveUp() { center.y -= center.ySpeed; } ...... }

Write a test program and try out these statements:

Movable m1 = new MovablePoint(5, 6, 10, 15); System.out.println(m1); m1.moveLeft(); System.out.println(m1); Movable m2 = new MovableCircle(1, 2, 3, 4, 20); System.out.println(m2); m2.moveRight(); System.out.println(m2);

Write a new class called , which composes two (representing the top-left and bottom-right corners) and implementing the Interface. Make sure that the two points has the same speed.

What is the difference between an interface and an abstract class?

Ex: Interfaces GeometricObject and Resizable

  1. Write the called , which declares two methods: and , as specified in the class diagram.
    Hints: public interface GeometricObject { public double getPerimeter(); ...... }
  2. Write the implementation class , with a protected variable , which implements the interface .
    Hints: public class Circle implements GeometricObject { ...... ...... @Override public double getPerimeter() { ...... } ...... }
  3. Write a test program called to test the methods defined in .
  4. The class is defined as a subclass of the class , which also implements an interface called , as shown in class diagram. The interface declares an method , which modifies the dimension (such as ) by the given percentage. Write the interface and the class .
    Hints: public interface { public double resize(...); } public class extends Circle implements Resizeable { public ResizableCircle(double radius) { super(...); } @Override public double resize(int percent) { ...... } }
  5. Write a test program called to test the methods defined in .

More Exercises on OOP

Ex: The Discount System

You are asked to write a discount system for a beauty saloon, which provides services and sells beauty products. It offers 3 types of memberships: Premium, Gold and Silver. Premium, gold and silver members receive a discount of 20%, 15%, and 10%, respectively, for all services provided. Customers without membership receive no discount. All members receives a flat 10% discount on products purchased (this might change in future). Your system shall consist of three classes: , and , as shown in the class diagram. It shall compute the total bill if a customer purchases $x of products and $y of services, for a visit. Also write a test program to exercise all the classes.

The class contains only variables and methods (underlined in the class diagram).

Ex: Polyline of Points with ArrayList

A polyline is a line with segments formed by points. Let's use the (dynamically allocated array) to keep the points, but upcast to in the instance variable. (Take note that array is of fixed-length, and you need to set the initial length).

public class Point { private int x; private int y; public Point(int x, int y) { ...... } public int getX() { ...... } public int getY() { ...... } public void setX(int x) { ...... } public void setY(int y) { ...... } public int[] getXY() { ...... } public void setXY(int x, int y) { ...... } public String toString() { ...... } public double distance(Point another) { ...... } } import java.util.*; public class PolyLine { private List<Point> points; public PolyLine() { points = new ArrayList<Point>(); } public PolyLine(List<Point> points) { this.points = points; } public void appendPoint(int x, int y) { Point newPoint = new Point(x, y); points.add(newPoint); } public void appendPoint(Point point) { points.add(point); } } public String toString() { StringBuilder sb = new StringBuilder("{"); for (Point aPoint : points) { sb.append(aPoint.toString()); } sb.append("}"); return sb.toString(); } public double getLength() { ...... } } import java.util.*; public class TestPolyLine { public static void main(String[] args) { PolyLine l1 = new PolyLine(); System.out.println(l1); l1.appendPoint(new Point(1, 2)); l1.appendPoint(3, 4); l1.appendPoint(5, 6); System.out.println(l1); List<Point> points = new ArrayList<Point>(); points.add(new Point(11, 12)); points.add(new Point(13, 14)); PolyLine l2 = new PolyLine(points); System.out.println(l2); } }

Exercises on Data Structures

Ex: MyIntStack

A stack is a first-in-last-out queue. Write a program called , which uses an array to store the contents, restricted to .

Write a test program.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class MyIntStack { private int[] contents; private int tos; public MyIntStack(int capacity) { contents = new int[capacity]; tos = -1; } public void push(int element) { contents[++tos] = element; } public int pop() { return contents[tos--]; } public int peek() { return contents[tos]; } public boolean isEmpty() { return tos < 0; } public boolean isFull() { return tos == contents.length - 1; } }

Try:

  1. Modify the method to throw an if the stack is full.
  2. Modify the to return if the operation is successful, or otherwise.
  3. Modify the to increase the capacity by reallocating another array, if the stack is full.

Exercise (Nodes, Link Lists, Trees, Graphs):

[TODO]

  • Study the existing open source codes, including JDK.
  • Specialized algorithms, such as shortest path.

Exercise (Maps):

[TODO]

  • Representation of map data.
  • Specialized algorithms, such as shortest path.

Exercise (Matrix Operations for 3D Graphics):

[TODO]

  • Study the existing open source codes, including JDK's 2D Graphics and JOGL's 3D Graphics.
  • Efficient and specialized codes for 3D Graphics (4D matrices). Handle various primitive types such as , and efficiently.

Why OOP?

Suppose that you want to assemble your own PC, you go to a hardware store and pick up a motherboard, a processor, some RAMs, a hard disk, a casing, a power supply, and put them together.  You turn on the power, and the PC runs.  You need not worry whether the CPU is 1-core or 6-core; the motherboard is a 4-layer or 6-layer; the hard disk has 4 plates or 6 plates, 3 inches or 5 inches in diameter; the RAM is made in Japan or Korea, and so on. You simply put the hardware components together and expect the machine to run.  Of course, you have to make sure that you have the correct interfaces, i.e., you pick an IDE hard disk rather than a SCSI hard disk, if your motherboard supports only IDE; you have to select RAMs with the correct speed rating, and so on.  Nevertheless, it is not difficult to set up a machine from hardware components.

Similarly, a car is assembled from parts and components, such as chassis, doors, engine, wheels, brake and transmission. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications).

Hardware, such as computers and cars, are assembled from parts, which are reusable hardware components.

How about software?  Can you "assemble" a software application by picking a routine here, a routine there, and expect the program to run?  The answer is obviously NO!  Unlike hardware, it is very difficult to "assemble" an application from software components.  Since the advent of computer 70 years ago, we have written tons and tons of programs and routines.  However, for each new application, we have to re-invent the wheels and write the program from scratch!

Why re-invent the wheels? Why re-writing codes? Can you write better codes than those codes written by the experts?

Traditional Procedural-Oriented languages

Traditional procedural-oriented programming languages (such as C, Fortran, Cobol and Pascal) suffer some notable drawbacks in creating reusable software components:

  1. The procedural-oriented programs are made up of functions. Functions are less reusable. It is very difficult to copy a function from one program and reuse in another program because the function is likely to reference the global variables and other functions. In other words, functions are not well-encapsulated as a self-contained reusable unit.
  2. The procedural languages are not suitable of high-level abstraction for solving real life problems. For example, C programs uses constructs such as if-else, for-loop, array, method, pointer, which are low-level and hard to abstract real problems such as a Customer Relationship Management (CRM) system or a computer soccer game.

The traditional procedural-languages separate the data structures (variables) and algorithms (functions).

Object-Oriented Programming Languages

Object-oriented programming (OOP) languages are designed to overcome these problems.

  1. The basic unit of OOP is a class, which encapsulates both the static properties and dynamic operations within a "box", and specifies the public interface for using these boxes. Since classes are well-encapsulated, it is easier to reuse these classes. In other words, OOP combines the data structures and algorithms of a software entity inside the same box.
  2. OOP languages permit higher level of abstraction for solving real-life problems. The traditional procedural language (such as C and Pascal) forces you to think in terms of the structure of the computer (e.g. memory bits and bytes, array, decision, loop) rather than thinking in terms of the problem you are trying to solve. The OOP languages (such as Java, C++ and C#) let you think in the problem space, and use software objects to represent and abstract entities of the problem space to solve the problem.

As an example, suppose you wish to write a computer soccer games (which I consider as a complex application). It is quite difficult to model the game in procedural-oriented languages. But using OOP languages, you can easily model the program accordingly to the "real things" appear in the soccer games.

  • Player: attributes include name, number, location in the field, and etc; operations include run, jump, kick-the-ball, and etc.
  • Ball:
  • Reference:
  • Field:
  • Audience:
  • Weather:

Most importantly, some of these classes (such as and ) can be reused in another application, e.g., computer basketball game, with little or no modification.

Benefits of OOP

The procedural-oriented languages focus on procedures, with function as the basic unit. You need to first figure out all the functions and then think about how to represent data.

The object-oriented languages focus on components that the user perceives, with objects as the basic unit. You figure out all the objects by putting all the data and operations that describe the user's interaction with the data.

Object-Oriented technology has many benefits:

  • Ease in software design as you could think in the problem space rather than the machine's bits and bytes. You are dealing with high-level concepts and abstractions. Ease in design leads to more productive software development.
  • Ease in software maintenance: object-oriented software are easier to understand, therefore easier to test, debug, and maintain.
  • Reusable software: you don't need to keep re-inventing the wheels and re-write the same functions for different situations. The fastest and safest way of developing a new application is to reuse existing codes - fully tested and proven codes.

OOP in Java

Class & Instances

In Java, a class is a definition of objects of the same kind. In other words, a class is a blueprint, template, or prototype that defines and describes the static attributes and dynamic behaviors common to all objects of the same kind.

An instance is a realization of a particular item of a class. In other words, an instance is an instantiation of a class. All the instances of a class have similar properties, as described in the class definition. For example, you can define a class called "" and create three instances of the class "" for "", "" and "".

The term "object" usually refers to instance. But it is often used loosely, and may refer to a class or an instance.

A Class is a 3-Compartment Box Encapsulating Data and Operations

A class can be visualized as a three-compartment box, as illustrated:

  1. Name (or identity): identifies the class.
  2. Variables (or attribute, state, field): contains the static attributes of the class.
  3. Methods (or behaviors, function, operation): contains the dynamic behaviors of the class.

In other words, a class encapsulates the static attributes (data) and dynamic behaviors (operations that operate on the data) in a box.

The followings figure shows a few examples of classes:

The following figure shows two instances of the class , identified as "" and "".

Unified Modeling Language (UML) Class and Instance Diagrams: The above class diagrams are drawn according to the UML notations. A class is represented as a 3-compartment box, containing name, variables, and methods, respectively. Class name is shown in bold and centralized. An instance is also represented as a 3-compartment box, with instance name shown as and underlined.

Brief Summary
  1. A class is a programmer-defined, abstract, self-contained, reusable software entity that mimics a real-world thing.
  2. A class is a 3-compartment box containing the name, variables and the methods.
  3. A class encapsulates the data structures (in variables) and algorithms (in methods). The values of the variables constitute its state. The methods constitute its behaviors.
  4. An instance is an instantiation (or realization) of a particular item of a class.

Class Definition in Java

In Java, we use the keyword to define a class. For examples:

public classCircle { double radius; String color; double getRadius() { ...... } double getArea() { ...... } } public classSoccerPlayer { int number; String name; int x, y; void run() { ...... } void kickBall() { ...... } }

The syntax for class definition in Java is:

[AccessControlModifier] classClassName{ ...... }

We shall explain the access control modifier, such as and , later.

Class Naming Convention: A class name shall be a noun or a noun phrase made up of several words. All the words shall be initial-capitalized (camel-case). Use a singular noun for class name. Choose a meaningful and self-descriptive classname. For examples, , , , and .

Creating Instances of a Class

To create an instance of a class, you have to:

  1. Declare an instance identifier (instance name) of a particular class.
  2. Construct the instance (i.e., allocate storage for the instance and initialize the instance) using the "" operator.

For examples, suppose that we have a class called , we can create instances of as follows:

Circle c1, c2, c3; c1 = new Circle(); c2 = new Circle(2.0); c3 = new Circle(3.0, "red"); Circle c4 = new Circle();

When an instance is declared but not constructed, it holds a special value called .

Dot (.) Operator

The variables and methods belonging to a class are formally called member variables and member methods. To reference a member variable or method, you must:

  1. First identify the instance you are interested in, and then,
  2. Use the dot operator () to reference the desired member variable or method.

For example, suppose that we have a class called , with two member variables ( and ) and two member methods ( and ). We have created three instances of the class , namely, , and . To invoke the method , you must first identity the instance of interest, says , then use the dot operator, in the form of .

For example,

Circle c1 = new Circle (); Circle c2 = new Circle (); System.out.println(c1.getArea()); System.out.println(c1.getRadius()); c2.radius = 5.0; c2.color = "blue";

Calling without identifying the instance is meaningless, as the radius is unknown (there could be many instances of - each maintaining its own radius). Furthermore, and are likely to produce different results.

In general, suppose there is a class called with a member variable called and a member method called . An instance called is constructed for . You use and .

Member Variables

A member variable has a name (or identifier) and a type; and holds a value of that particular type (as descried in the earlier chapter).

Variable Naming Convention: A variable name shall be a noun or a noun phrase made up of several words. The first word is in lowercase and the rest of the words are initial-capitalized (camel-case), e.g., , , , and .

The formal syntax for variable definition in Java is:

[AccessControlModifier] type variableName [=initialValue]; [AccessControlModifier] type variableName-1 [=initialValue-1] [,type variableName-2 [=initialValue-2]] ... ;

For example,

private double radius; public int length = 1, width = 1;

Member Methods

A method (as described in the earlier chapter):

  1. receives arguments from the caller,
  2. performs the operations defined in the method body, and
  3. returns a piece of result (or ) to the caller.

The syntax for method declaration in Java is as follows:

[AccessControlModifier] returnType methodName([parameterList]) { ...... }

For examples:

public double getArea() { return radius * radius * Math.PI; }

Method Naming Convention: A method name shall be a verb, or a verb phrase made up of several words. The first word is in lowercase and the rest of the words are initial-capitalized (camel-case). For example, , , , .

Variable name vs. Method name vs. Class name: A variable name is a noun, denoting an attribute; while a method name is a verb, denoting an action. They have the same naming convention (the first word in lowercase and the rest are initial-capitalized). Nevertheless, you can easily distinguish them from the context. Methods take arguments in parentheses (possibly zero arguments with empty parentheses), but variables do not. In this writing, methods are denoted with a pair of parentheses, e.g., , for clarity.

On the other hand, class name is a noun beginning with uppercase.

Putting them Together: An OOP Example

A class called is defined as shown in the class diagram. It contains two member variables: (of type ) and (of type ); and three member methods: , , and .

Three instances of s, called , , and , shall be constructed with their respective data members, as shown in the instance diagrams.

The source codes for is as follows:

Circle.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Circle { private double radius; private String color; public Circle() { radius = 1.0; color = "red"; } public Circle(double r) { radius = r; color = "red"; } public Circle(double r, String c) { radius = r; color = c; } public double getRadius() { return radius; } public String getColor() { return color; } public double getArea() { return radius * radius * Math.PI; } }

Compile "" into "".

Notice that the class does not have a method. Hence, it is NOT a standalone program and you cannot run the class by itself. The class is meant to be a building block - to be used in other programs.

TestCircle.java

We shall now write another class called , which uses the class. The class has a method and can be executed.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class TestCircle { public static void main(String[] args) { Circle c1 = new Circle(2.0, "blue"); System.out.println("The radius is: " + c1.getRadius()); System.out.println("The color is: " + c1.getColor()); System.out.printf("The area is: %.2f%n", c1.getArea()); Circle c2 = new Circle(2.0); System.out.println("The radius is: " + c2.getRadius()); System.out.println("The color is: " + c2.getColor()); System.out.printf("The area is: %.2f%n", c2.getArea()); Circle c3 = new Circle(); System.out.println("The radius is: " + c3.getRadius()); System.out.println("The color is: " + c3.getColor()); System.out.printf("The area is: %.2f%n", c3.getArea()); } }

Compile into .

Run the and study the output:

The radius is: 2.0 The color is: blue The area is: 12.57 The radius is: 2.0 The color is: red The area is: 12.57 The radius is: 1.0 The color is: red The area is: 3.14

Constructors

A constructor looks like a special method that has the same method name as the class name. In the above class, we define three overloaded versions of constructor . A constructor is used to construct and initialize all the member variables. To construct a new instance of a class, you need to use a special "" operator followed by a call to one of the constructors. For example,

Circle c1 = new Circle(); Circle c2 = new Circle(2.0); Circle c3 = new Circle(3.0, "red");

A constructor is different from an ordinary method in the following aspects:

  • The name of the constructor method is the same as the class name. By classname's convention, it begins with an uppercase (instead of lowercase for ordinary methods).
  • Constructor has no return type. It implicitly returns . No statement is allowed inside the constructor's body.
  • Constructor can only be invoked via the "" operator. It can only be used once to initialize the instance constructed. Once an instance is constructed, you cannot call the constructor anymore.
  • Constructors are not inherited (to be explained later).

Default Constructor: A constructor with no parameter is called the default constructor. It initializes the member variables to their default value. For example, the in the above example initialize member variables and to their default value.

Method Overloading (Revisit)

Method overloading means that the same method name can have different implementations (versions). However, the different implementations must be distinguishable by their parameter list (either the number of parameters, or the type of parameters, or their order).

Example: The method has 3 versions, with different parameter lists. The caller can invoke the chosen version by supplying the matching arguments.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class TestMethodOverloading { public static int average(int n1, int n2) { System.out.println("Run version A"); return (n1+n2)/2; } public static double average(double n1, double n2) { System.out.println("Run version B"); return (n1+n2)/2; } public static int average(int n1, int n2, int n3) { System.out.println("Run version C"); return (n1+n2+n3)/3; } public static void main(String[] args) { System.out.println(average(1, 2)); System.out.println(average(1.0, 2.0)); System.out.println(average(1, 2, 3)); System.out.println(average(1.0, 2)); - average(1, 2, 3, 4); } }
Overloading Circle Class' Constructor

Constructor, like an ordinary method, can also be overloaded. The above class has three overloaded versions of constructors differentiated by their parameter list, as followed:

Circle() Circle(double r) Circle(double r, String c)

Depending on the actual argument list used when invoking the method, the matching constructor will be invoked. If your argument list does not match any one of the methods, you will get a compilation error.

public vs. private - Access Control Modifiers

An access control modifier can be used to control the visibility of a class, or a member variable or a member method within a class. We begin with the following two access control modifiers:

  1. : The class/variable/method is accessible and available to all the other objects in the system.
  2. : The class/variable/method is accessible and available within this class only.

For example, in the above definition, the member variable is declared . As the result, is accessible inside the class, but NOT in the class. In other words, you cannot use "" to refer to 's in .

  • Try inserting the statement "" in and observe the error message.
  • Try changing to in the class, and re-run the above statement.

On the other hand, the method is declared in the class. Hence, it can be invoked in the class.

UML Notation: In class diagram, members are denoted with a ""; while members with a "".

More access control modifiers will be discussed later.

Information Hiding and Encapsulation

A class encapsulates the name, static attributes and dynamic behaviors into a "3-compartment box". Once a class is defined, you can seal up the "box" and put the "box" on the shelve for others to use and reuse. Anyone can pick up the "box" and use it in their application. This cannot be done in the traditional procedural-oriented language like C, as the static attributes (or variables) are scattered over the entire program and header files. You cannot "cut" out a portion of C program, plug into another program and expect the program to run without extensive changes.

Member variables of a class are typically hidden from the outside word (i.e., the other classes), with access control modifier. Access to the member variables are provided via assessor methods, e.g., and .

This follows the principle of information hiding. That is, objects communicate with each others using well-defined interfaces (public methods). Objects are not allowed to know the implementation details of others. The implementation details are hidden or encapsulated within the class. Information hiding facilitates reuse of the class.

Rule of Thumb: Do not make any variable , unless you have a good reason.

The public Getters and Setters for private Variables

To allow other classes to read the value of a variable says , we provide a get method (or getter or accessor method) called . A get method needs not expose the data in raw format. It can process the data and limit the view of the data others will see. The getters shall not modify the variable.

To allow other classes to modify the value of a variable says , we provide a set method (or setter or mutator method) called . A set method could provide data validation (such as range checking), or transform the raw data into the internal representation.

For example, in our class, the variables and are declared . That is to say, they are only accessible within the class and not visible in any other classes, such as the class. You cannot access the variables and from the class directly - via says or . The class provides two public accessor methods, namely, and . These methods are declared . The class can invoke these public accessor methods to retrieve the and of a object, via says and .

There is no way you can change the or of a object, after it is constructed in the class. You cannot issue statements such as to change the of instance , as is declared as in the class and is not visible to other classes including .

If the designer of the class permits the change the and after a object is constructed, he has to provide the appropriate set methods (or setters or mutator methods), e.g.,

public void setColor(String newColor) { color = newColor; } public void setRadius(double newRadius) { radius = newRadius; }

With proper implementation of information hiding, the designer of a class has full control of what the user of the class can and cannot do.

Keyword "this"

You can use keyword "" to refer to this instance inside a class definition.

One of the main usage of keyword is to resolve ambiguity.

public class Circle { double radius; public Circle(double radius) { this.radius = radius; } ... }

In the above codes, there are two identifiers called - a member variable of the class and the method's argument. This causes naming conflict. To avoid the naming conflict, you could name the method's argument instead of . However, is more approximate and meaningful in this context. Java provides a keyword called to resolve this naming conflict. "" refers to the member variable; while "" resolves to the method's argument.

Using the keyword "", the constructor, getter and setter methods for a variable called of type are as follows:

public class Aaa { private T xxx; public Aaa(T xxx) { this.xxx = xxx; } public T getXxx() { return xxx; } public void setXxx(T xxx) { this.xxx = xxx; } }

For a boolean variable , the getter shall be named or , which is more meaningful than . The setter remains .

private boolean xxx; public boolean isXxx() { return xxx; } public void setXxx(boolean xxx) { this.xxx = xxx; }
More on "this"
  • refers to of this instance; invokes of this instance.
  • In a constructor, we can use to call another constructor of this class.
  • Inside a method, we can use the statement "" to return this instance to the caller.

Method toString()

Every well-designed Java class should have a method called that returns a description of instance. You can invoke the method explicitly by calling , or implicitly via or concatenation operator . That is, running invokes the method of that instance implicitly.

For example, include the following method in our class:

public String toString() { return "Circle[radius=" + radius + ",color=" + color + "]"; }

In your class, you can get a description of a instance via:

Circle c1 = new Circle(); System.out.println(c1.toString()); System.out.println(c1); System.out.println("c1 is: " + c1);

The signature of is:

public String toString() { ...... }

Constants (final)

Constants are variables defined with the modifier . A variable can only be assigned once and its value cannot be modified once assigned. For example,

public final double X_REFERENCE = 1.234; private final int MAX_ID = 9999; MAX_ID = 10000; // error: cannot assign a value to final variable MAX_ID private final int SIZE; // error: variable SIZE might not have been initialized

Constant Naming Convention: A constant name is a noun, or a noun phrase made up of several words. All words are in uppercase separated by underscores '', for examples, , and .

Advanced Notes:

  1. A primitive variable cannot be re-assigned a new value.
  2. A instance cannot be re-assigned a new object.
  3. A class cannot be sub-classed (or extended).
  4. A method cannot be overridden.

Putting Them Together in the Revised Circle Class

We shall include constructors, getters, setters, , and use the keyword "". The class diagram for the final class is as follows:

Circle.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public class Circle { public static final double DEFAULT_RADIUS = 8.8; public static final String DEFAULT_COLOR = "red"; private double radius; private String color; public Circle() { this.radius = DEFAULT_RADIUS; this.color = DEFAULT_COLOR; } public Circle(double radius) { this.radius = radius; this.color = DEFAULT_COLOR; } public Circle(double radius, String color) { this.radius = radius; this.color = color; } public double getRadius() { return this.radius; } public void setRadius(double radius) { this.radius = radius; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } public String toString() { return "Circle[radius=" + radius + ", color=" + color + "]"; } public double getArea() { return radius * radius * Math.PI; } public double getCircumference() { return 2.0 * radius * Math.PI; } }
A Test Driver for the Circle Class
public class TestCircle { public static void main(String[] args) { Circle c1 = new Circle(1.1, "blue"); System.out.println(c1); Circle c2 = new Circle(2.2); System.out.println(c2); Circle c3 = new Circle(); System.out.println(c3); c1.setRadius(2.2); c1.setColor("green"); System.out.println(c1); System.out.println("The radius is: " + c1.getRadius()); System.out.println("The color is: " + c1.getColor()); System.out.printf("The area is: %.2f%n", c1.getArea()); System.out.printf("The circumference is: %.2f%n", c1.getCircumference()); } }

The expected outputs are:

Circle[radius=1.1, color=blue] Circle[radius=2.2, color=red] Circle[radius=8.8, color=red] Circle[radius=2.2, color=green] Radius is: 2.2 Color is: green Area is: 15.21 Circumference is: 13.82

More Examples on Classes

EG. 1: The Account Class

A class called , which models a bank account, is designed as shown in the class diagram. It contains the following members:

  • Two instance variables: (), and () which maintains the current account balance.
  • Constructors (overloaded).
  • Getters and Setters for the instance variables. There is no setter for as it is not designed to be changed.
  • methods and , which adds/subtracts the given to/from the balance, respectively.
  • A , which returns "", with rounded to two decimal places.

Write the class and a test driver to test all the methods.

The Account Class (Account.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class Account { private int accountNumber; private double balance; public Account(int accountNumber, double balance) { this.accountNumber = accountNumber; this.balance = balance; } public Account(int accountNumber) { this.accountNumber = accountNumber; this.balance = 0.0; } public int getAccountNumber() { return this.accountNumber; } public double getBalance() { return this.balance; } public void setBalance(double balance) { this.balance = balance; } public void credit(double amount) { balance += amount; } public void debit(double amount) { if (balance < amount) { System.out.println("amount withdrawn exceeds the current balance!"); } else { balance -= amount; } } public String toString() { return String.format("A/C no:%d, Balance=%.2f", accountNumber, balance); } }
A Test Driver for the Account Class (TestAccount.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class TestAccount { public static void main(String[] args) { Account a1 = new Account(1234, 99.99); System.out.println(a1); Account a2 = new Account(8888); System.out.println(a2); a1.setBalance(88.88); System.out.println(a1); System.out.println("The account Number is: " + a1.getAccountNumber()); System.out.println("The balance is: " + a1.getBalance()); a1.credit(10); System.out.println(a1); a1.debit(5); System.out.println(a1); a1.debit(500); System.out.println(a1); } }

The expected outputs are:

A/C no:1234, Balance=99.99 A/C no:8888, Balance=0.00 A/C no:1234, Balance=88.88 Account Number is: 1234 Balance is: 88.88 A/C no:1234, Balance=98.88 A/C no:1234, Balance=93.88 amount withdrawn exceeds the current balance! A/C no:1234, Balance=93.88

EG. 2: The Date class

A class models a calendar date with day, month and year, is designed as shown in the class diagram. It contains the following members:

  • 3 instance variables , , and .
  • Constructors, getters and setters for the instance variables.
  • A method , which sets the , and .
  • A , which returns "", with leading zero for and if applicable.

Write the class and a test driver to test all the methods. No Input validations are required for , , and .

The Date Class (Date.java)
public class Date { private int year, month, day; public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return this.year; } public int getMonth() { return this.month; } public int getDay() { return this.day; } public void setYear(int year) { this.year = year; } public void setMonth(int month) { this.month = month; } public void setDay(int day) { this.day = day; } public String toString() { return String.format("%02d/%02d/%4d", month, day, year); } public void setDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } }
A Test Driver for the Date Class (TestDate.java)
public class TestDate { public static void main(String[] args) { Date d1 = new Date(2016, 4, 6); System.out.println(d1); d1.setYear(2012); d1.setMonth(12); d1.setDay(23); System.out.println(d1); System.out.println("Year is: " + d1.getYear()); System.out.println("Month is: " + d1.getMonth()); System.out.println("Day is: " + d1.getDay()); d1.setDate(2988, 1, 2); System.out.println(d1); } }

The expected outputs are:

04/06/2016 12/23/2012 Year is: 2012 Month is: 12 Day is: 23 01/02/2988

EG. 3: The Time class

A class called , which models a time instance with hour, minute and second, is designed as shown in the class diagram. It contains the following members:

  • 3 instance variables , , and .
  • Constructors, getters and setters.
  • A method to set , and .
  • A that returns "" with leading zero if applicable.
  • A method that advances instance by one second. It returns instance to support chaining (cascading) operations, e.g., . Take note that the of is .

Write the class and a test driver to test all the public methods. No input validations are required.

The Time Class (Time.java)
public class Time { private int second, minute, hour; public Time(int second, int minute, int hour) { this.second = second; this.minute = minute; this.hour = hour; } public Time() { this.second = 0; this.minute = 0; this.hour = 0; } public int getSecond() { return this.second; } public int getMinute() { return this.minute; } public int getHour() { return this.hour; } public void setSecond(int second) { this.second = second; } public void setMinute(int minute) { this.minute = minute; } public void setHour(int hour) { this.hour = hour; } public String toString() { return String.format("%02d:%02d:%02d", hour, minute, second); } public void setTime(int second, int minute, int hour) { this.second = second; this.minute = minute; this.hour = hour; } public Time nextSecond() { ++second; if (second >= 60) { second = 0; ++minute; if (minute >= 60) { minute = 0; ++hour; if (hour >= 24) { hour = 0; } } } return this; } }
A Test Driver (TestTime.java)
public class TestTime { public static void main(String[] args) { Time t1 = new Time(1, 2, 3); System.out.println(t1); Time t2 = new Time(); System.out.println(t2); t1.setHour(4); t1.setMinute(5); t1.setSecond(6); System.out.println(t1); System.out.println("Hour is: " + t1.getHour()); System.out.println("Minute is: " + t1.getMinute()); System.out.println("Second is: " + t1.getSecond()); t1.setTime(58, 59, 23); System.out.println(t1); System.out.println(t1.nextSecond()); System.out.println(t1.nextSecond().nextSecond().nextSecond()); } }

The expected outputs are:

03:02:01 00:00:00 04:05:06 Hour is: 4 Minute is: 5 Second is: 6 23:59:58 23:59:59 00:00:02

EG. 4: The Time class with Input Validation

In this example, we shall validate the inputs to ensure that , , and . We re-write our class as follows. Take note that all the validations are done in the setters. All other methods (such as constructors and invoke the setters to perform input validations - so as to avoid duplication of codes.

public class Time { private int second; private int minute; private int hour; public void setSecond(int second) { if (second >=0 && second <= 59) { this.second = second; } else { this.second = 0; System.out.println("error: invalid second"); } } public void setMinute(int minute) { if (minute >=0 && minute <= 59) { this.minute = minute; } else { this.minute = 0; System.out.println("error: invalid minute"); } } public void setHour(int hour) { if (hour >=0 && hour <= 23) { this.hour = hour; } else { this.hour = 0; System.out.println("error: invalid hour"); } } public void setTime(int second, int minute, int hour) { this.setSecond(second); this.setMinute(minute); this.setHour(hour); } public Time(int second, int minute, int hour) { this.setTime(second, minute, hour); } public Time() { this.second = 0; this.minute = 0; this.hour = 0; } public int getSecond() { return this.second; } public int getMinute() { return this.minute; } public int getHour() { return this.hour; } public String toString() { return String.format("%02d:%02d:%02d", hour, minute, second); } public Time nextSecond() { ++second; if (second == 60) { second = 0; ++minute; if (minute == 60) { minute = 0; ++hour; if (hour == 24) { hour = 0; } } } return this; } }

EG. 5 (Advanced): The Time Class with Input Validation via Exception Handling

In the previous example, we print a error message and set the variable to 0, if the input is invalid. This is less than perfect. The proper way to handle invalid inputs is via the so-called exception handling mechanism.

The revised that uses exception handling mechanism is as follows:

public class Time { private int second; private int minute; private int hour; public void setSecond(int second) { if (second >=0 && second <= 59) { this.second = second; } else { throw new IllegalArgumentException("Invalid second!"); } } public void setMinute(int minute) { if (minute >=0 && minute <= 59) { this.minute = minute; } else { throw new IllegalArgumentException("Invalid minute!"); } } public void setHour(int hour) { if (hour >=0 && hour <= 23) { this.hour = hour; } else { throw new IllegalArgumentException("Invalid hour!"); } } public void setTime(int second, int minute, int hour) { this.setSecond(second); this.setMinute(minute); this.setHour(hour); } public Time(int second, int minute, int hour) { this.setTime(second, minute, hour); } public Time() { this.second = 0; this.minute = 0; this.hour = 0; } public int getSecond() { return this.second; } public int getMinute() { return this.minute; } public int getHour() { return this.hour; } public String toString() { return String.format("%02d:%02d:%02d", hour, minute, second); } public Time nextSecond() { ++second; if (second == 60) { second = 0; ++minute; if (minute == 60) { minute = 0; ++hour; if (hour == 24) { hour = 0; } } } return this; } }
Exception Handling

What to do if an invalid , or was given as input? Print an error message? Terminate the program Abruptly? Continue operation by setting the parameter to its default? This is a really hard decision and there is no perfect solution that suits all situations.

In Java, instead of printing an error message, you can throw an so-called object (such as ) to the caller, and let the caller handles the exception gracefully. For example,

public void setHour(int hour) { if (hour >= 0 && hour <= 23) { this.hour = hour; } else { throw new IllegalArgumentException("Invalid hour!"); } }

The caller can use the construct to handle the exception gracefully. For example,

try { Time t = new Time(60, 59, 12); System.out.println("This and the remaining will be skipped, if exception occurs"); } catch (IllegalArgumentException ex) { ex.printStackTrace(); }

The statements in the -clause will be executed. If all the statements in the -clause are successful, the -clause is ignored, and execution continues to the next statement after . However, if one of the statement in the -clause throws an exception (in this case, an ), the rest of -clause will be skipped, and the execution will be transferred to the -clause. The program always continues to the next statement after the (instead of abruptly terminated).

A Test Driver Class for the Time Class (TestTime.java)
public class TestTime { public static void main(String[] args) { Time t1 = new Time(1, 2, 3); System.out.println(t1); // Time t2 = new Time(60, 59, 12); try { Time t3 = new Time(60, 59, 12); System.out.println("This line will be skipped, if exception occurs"); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } System.out.println("Continue after exception!"); } }

Without the proper , the "" will abruptly terminate the program, i.e., the rest of the program will not be run (try it by un-commenting the statement). With proper handling, the program can continue its operation (i.e., graceful handling of exception).

EG. 6: The Point class

A class models a 2D point at , as shown in the class diagram. It contains the following members:

  • 2 instance variables and , which maintain the location of the point.
  • Constructors, getters and setters.
  • A method , which sets the and of the point; and a method , which returns the and in a 2-element array.
  • A , which returns "".
  • 3 versions of overloaded :
    • returns the distance from instance to the given point at .
    • returns the distance from instance to the given instance (called ).
    • returns the distance from instance to .
The Point Class (Point.java)
public class Point { private int x, y; public Point() { this.x = 0; this.y = 0; } public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } public String toString() { return "(" + this.x + "," + this.y + ")"; } public int[] getXY() { int[] results = new int[2]; results[0] = this.x; results[1] = this.y; return results; } public void setXY(int x, int y) { this.x = x; this.y = y; } public double distance(int x, int y) { int xDiff = this.x - x; int yDiff = this.y - y; return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } public double distance(Point another) { int xDiff = this.x - another.x; int yDiff = this.y - another.y; return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } public double distance() { return Math.sqrt(this.x*this.x + this.y*this.y); } }
A Test Driver (TestPoint.java)
public class TestPoint { public static void main(String[] args) { Point p1 = new Point(1, 2); System.out.println(p1); Point p2 = new Point(); System.out.println(p2); p1.setX(3); p1.setY(4); System.out.println(p1); System.out.println("X is: " + p1.getX()); System.out.println("Y is: " + p1.getY()); p1.setXY(5, 6); System.out.println(p1); System.out.println("X is: " + p1.getXY()[0]); System.out.println("Y is: " + p1.getXY()[1]); p2.setXY(10, 11); System.out.printf("Distance is: %.2f%n", p1.distance(10, 11)); System.out.printf("Distance is: %.2f%n", p1.distance(p2)); System.out.printf("Distance is: %.2f%n", p2.distance(p1)); System.out.printf("Distance is: %.2f%n", p1.distance()); } }

EG. 7: The Ball class

A class models a moving ball, is designed as shown in the class diagram. It contains the following members:

  • 4 variables , , , , which maintain the position of the ball and the displacement per move step.
  • Constructors, getters and setters.
  • Method and , which sets the position and step size of the ball; and and .
  • A , which returns "".
  • A method , which increases and by and respectively; and returns instance to support chaining operation.
The Ball Class (Ball.java)
public class Ball { private double x, y, xStep, yStep; public Ball(double x, double y, double xStep, double yStep) { this.x = x; this.y = y; this.xStep = xStep; this.yStep = yStep; } public double getX() { return this.x; } public void setX(double x) { this.x = x; } public double getY() { return this.y; } public void setY(double y) { this.y = y; } public double getXStep() { return this.xStep; } public void setXStep(double xStep) { this.xStep = xStep; } public double getYStep() { return this.yStep; } public void setYStep(double yStep) { this.yStep = yStep; } public String toString() { return "Ball@(" + x + "," + y + "),speed=(" + xStep + "," + yStep + ")"; } public double[] getXY() { double[] results = new double[2]; results[0] = this.x; results[1] = this.y; return results; } public void setXY(double x, double y) { this.x = x; this.y = y; } public double[] getXYStep() { double[] results = new double[2]; results[0] = this.xStep; results[1] = this.yStep; return results; } public void setXYStep(double xStep, double yStep) { this.xStep = xStep; this.yStep = yStep; } public Ball move() { x += xStep; y += yStep; return this; } }
A Test Driver (TestBall.java)
public class TestBall { public static void main(String[] args) { Ball b1 = new Ball(1, 2, 11, 12); System.out.println(b1); b1.setX(3); b1.setY(4); b1.setXStep(13); b1.setYStep(14); System.out.println(b1); System.out.println("x is: " + b1.getX()); System.out.println("y is: " + b1.getY()); System.out.println("xStep is: " + b1.getXStep()); System.out.println("yStep is: " + b1.getYStep()); b1.setXY(5, 6); b1.setXYStep(15, 16); System.out.println(b1); System.out.println("x is: " + b1.getXY()[0]); System.out.println("y is: " + b1.getXY()[1]); System.out.println("xStep is: " + b1.getXYStep()[0]); System.out.println("yStep is: " + b1.getXYStep()[1]); System.out.println(b1.move()); System.out.println(b1.move().move().move()); } }

Try: To support bouncing ball within a rectangular boundary, add a variable called , and methods and .

EG. 8: The Student Class

Suppose that our application requires us to model students. A student has a name and an address. We are required to keep track of the courses taken by each student, together with the grades (between 0 and 100) for each of the courses. A student shall not take more than 30 courses for the entire program. We are required to print all course grades, and also the overall average grade.

We can design the class as shown in the class diagram. It contains the following members:

  • instance variables (), (), (), () and (). The keeps track of the number of courses taken by this student so far. The and are two parallel arrays, storing the courses taken (e.g., ) and their respective grades (e.g. ).
  • A constructor that constructs an instance with the given and . It also constructs the and arrays and set the to 0.
  • Getters for and ; setter for . No setter is defined for as it is not designed to be changed.
  • A , which prints "".
  • A method , which appends the given and into the and arrays, respectively; and increments .
  • A method , which prints "".
  • A method , which returns the average grade of all the courses taken.
The Student Class (Student.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 public class Student { private String name; private String address; private String[] courses; private int[] grades; private int numCourses; private static final int MAX_COURSES = 30; public Student(String name, String address) { this.name = name; this.address = address; courses = new String[MAX_COURSES]; grades = new int[MAX_COURSES]; numCourses = 0; } public String getName() { return this.name; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String toString() { return name + "(" + address + ")"; } public void addCourseGrade(String course, int grade) { courses[numCourses] = course; grades[numCourses] = grade; ++numCourses; } public void printGrades() { System.out.print(name); for (int i = 0; i < numCourses; ++i) { System.out.print(" " + courses[i] + ":" + grades[i]); } System.out.println(); } public double getAverageGrade() { int sum = 0; for (int i = 0; i < numCourses; ++i) { sum += grades[i]; } return (double)sum/numCourses; } }
A Test Driver for the Student Class (TestStudent.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class TestStudent { public static void main(String[] args) { Student ahTeck = new Student("Tan Ah Teck", "1 Happy Ave"); System.out.println(ahTeck); ahTeck.setAddress("8 Kg Java"); System.out.println(ahTeck); System.out.println(ahTeck.getName()); System.out.println(ahTeck.getAddress()); ahTeck.addCourseGrade("IM101", 89); ahTeck.addCourseGrade("IM102", 57); ahTeck.addCourseGrade("IM103", 96); ahTeck.printGrades(); System.out.printf("The average grade is %.2f%n", ahTeck.getAverageGrade()); } }

The expected outputs are:

Tan Ah Teck(1 Happy Ave) Tan Ah Teck(8 Kg Java) Tan Ah Teck 8 Kg Java Tan Ah Teck IM101:89 IM102:57 IM103:96 The average grade is 80.67

Notes: We used arrays In this example, which has several limitations. Arrays need to be pre-allocated with a fixed-length. Furthermore, we need two parallel arrays to keep track of two entities. There are advanced data structures that could represent these data better and more efficiently.

Exercises

LINK TO EXERCISES

LINK TO JAVA REFERENCES & RESOURCES

0 Thoughts to “Java Assignments For Students Pdf Printer

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *