© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_30

30. Design Considerations

Ron Dai1 
(1)
Seattle, WA, USA
 

We have learned some fundamental concepts about classes and objects in Java. Now let’s look at several examples from the class design perspective.

Practical Case 1

The following is a design of a Rectangle class . It wants to compute a rectangle’s area, perimeter, and diagonal, given its width and height values as input parameters.
public class Rectangle {
       private int width;
       private int height;
       private int area;
       private double diagonal;
       private int perimeter;
       public Rectangle (int width, int height) {
              this.width = width;
              this.height = height;
              this.area = width*height;
              this.diagonal = Math.sqrt(width * width + height * height);
              this.perimeter = (width + height) * 2;
       }
       public int getArea() {
              return this.area;
       }
       public double getDiagonal() {
              return this.diagonal;
       }
       public int getPerimeter() {
              return this.perimeter;
       }
}
The computations of area, parameter, and diagonal are being done inside the Rectangle constructor, which is executed every time an object of the Rectangle class is initialized. It works if we consistently want to get the values of the area, perimeter, and diagonal of the rectangle. But when we sometimes only want to query the area, perimeter, or diagonal of the rectangle, some part of the computations become excessive. A much better design approach is an “on-demand” implementation as shown here.
public class Rectangle {
       private int width;
       private int height;
       public Rectangle (int width, int height) {
              this.width = width;
              this.height = height;
       }
       public int getArea() {
              return this.width * this.height;
       }
       public double getDiagonal() {
              return Math.sqrt(this.width * this.width
              + this.height * this.height);
       }
       public int getPerimeter() {
              return (this.width + this.height) * 2;
       }
}

Practical Case 2

The following example is an implementation of a Game class design. It looks good except for a couple of private field type design choices.
  • The price for goods is usually a small integer plus two decimal places to the right of the decimal point. Neither float types nor double types can accurately represent this form of number used for money calculations because of floating-point inaccuracies. It is recommended to represent the dollar price in cents, so you only need the program to take care of the integer computations. On some occasions, computing money in dollars may be good enough.

  • The gameType should not be defined as a true/false Boolean value. It should use “String” data type. (Or, we may consider using enumeration, if we have a known list of fixed names for the gameType.)
    public class Game {
           private int price;
           private boolean gameType;
           private String platform;
           public Game() { }
           public int getPrice() {
                  return this.price;
           }
           public int setPrice(int price) {
                  return this.price=price;
           }
           public boolean getGameType() {
                  return this.gameType;
           }
           public boolean setGameType(boolean gameType) {
                  return this.gameType=gameType;
           }
           public String getPlatform() {
                  return this.platform;
           }
           public String setPlatform(String platform) {
                  return this.platform=platform;
           }
    }

Practical Case 3

How do we test a class we have designed in Eclipse?

There are at least two simple approaches. Assume you have designed a class called MyClass . It has one public integer data field - myNumber, and one method to double its integer number value - doubleMe().

Approach A

Both the original class and test code are contained in one Java file as shown here:
public class MyClass {
       // class design part of code
       public int myNumber;
       public MyClass() {       }
       public int doubleMe() {
              return this.myNumber * 2;
       }
       // test part of code
       public static void main(String arg[]) {
              // declare and initialize an object
              MyClass myObject = new MyClass();
              myObject.myNumber = 2019;
              int output = myObject.doubleMe();
              // output the resulting data and validate it
              System.out.println("My result is: " + output);
       }
}

Approach B

The following two classes are in separate Java files:

In MyClass.java:
public class MyClass {
       public int myNumber;
       public MyClass() {
       }
       public int doubleMe() {
              return this.myNumber * 2;
       }
}
In TestMyClass.java:
public class TestMyClass {
       public static void main(String arg[]) {
              MyClass myObject = new MyClass();
              myObject.myNumber = 2019;
              int output = myObject.doubleMe();
              System.out.println("My result is: "  + output);
       }
}

Practical Case 4

What are the differences between a static and a non-static field or method? And, when do we use static fields and static methods?

In most of the code examples depicted earlier, we used non-static fields and methods (a.k.a. instance fields and instance methods). Both an instance field and an instance method belong to the object instantiated, which means they are not activated until after the object has been created.

However, static fields and static methods belong to the class level. They can be accessed by class name, instead of by any object instantiated from the class. The values stored in static fields and computed by static methods are shared among all objects created from the same class.

The first and most familiar static method to us is “main()method, if you recall. It can reside in any public class. This method is a unique entry point of any application. It has to be associated with a class. In other words, it doesn’t live in any object instance.

In the Demo class example, there is a static field counter that tracks the number of objects created during runtime. There is a non-static field (i.e., instance field) - myNumber that is associated with an individual object instance. The non-static method (i.e., instance method) - getNumber() also belongs to the object created.
public class Demo {
       private static int counter;
       public static int getCounter() {
              return counter;
       }
       private int myNumber;
       public int getNumber() {
              return this.myNumber;
       }
       public Demo(int number) {
              this.myNumber = number;
              counter++;
              System.out.println("I am no. " + counter + " object so far.");
       }
}
The next is a test class to demonstrate how the static field (i.e., counter) and the static method (i.e., Demo.getCounter()) work, in comparison to the non-static field (i.e., myNumber) and the non-static method (i.e., getNumber()).
public class TestDemo {
       public static void main(String[] args) {
              Demo demo1 = new Demo(21);
       System.out.println("demo1 myNumber: " + demo1.getNumber());
              System.out.println("object counts: " + Demo.getCounter());
              Demo demo2 = new Demo(57);
              System.out.println("demo2 myNumber: " + demo2.getNumber());
              System.out.println("object counts: " + Demo.getCounter());
              Demo demo3 = new Demo(99);
              System.out.println("demo3 myNumber: " +
              demo3.getNumber());
              System.out.println("object counts: " + Demo.getCounter());
       }
}
The output from the console is:
I am no. 1 object so far.
demo1's myNumber: 21
object counts: 1
I am no. 2 object so far.
demo2's myNumber: 57
object counts: 2
I am no. 3 object so far.
demo3's myNumber: 99
object counts: 3
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.194.57