How to build a class



How to build a class

Building a class is an art and technique. You will learn through analysis, math background, and life experience. Here is a scientific approach.

1. Identify the set and its members. The two concepts set and members always go together. In Java set and members are called class and objects respectively.

2. Primitive classes. They are classes predefined by compiler. They are int, boolean, float, double, char.

3. Predefined non-primitive classes. They are built and saved in the packages or files. If we import them, we don’t have to build them. For example the class String is located in java.lang. We are able to import the classes we have built and use them.

4. Non-primitive classes. After we identify the set and its members, we don’t see the set as a primitive class or predefined non-primitive class, we must build it.

• Express the set and its members in mathematics. In this case, a dummy member with its characteristics/attributes represents the set. For example: Set of rational numbers = { p/q | p is an integer and q is a non-zero integer}. Plane = {(x, y) | x and y are real numbers}.

• List the attributes: Each dummy member is composed of the changeable components and fixed components. For example rational number p/q, the p and q are changeable while the symbol slash is fixed. In the plane, the point (x,y) has the changeable x and y while the parentheses and comma is fixed. The changeable components are characterized after the vertical bar. They are the attributes. We must list the dummy attributes in the lines.

Example 1: the rational number p/q has the following attributes:

p is an integer for numerator;

q is a non-zero integer for denominator;

Example 2: the point in the plane (x, y) has the following attributes:

x is a floating-point number for x-coordinate;

y is a floating point number for y-coordinate;

• Form the class with the following rules:

Rule 1: Use the set name as the name of the class

Rule 2: Use primitive or predefined non-primitive classes to declare the attributes.

Rule 3: If Rule 2 cannot define the attribute, then either we must create a new class or we use a superset of primitive or predefined non-primitive classes.

Example 1:

// Set of Rational Numbers

class SetOfRationals // Rule 1

{

int numerator; // Rule 2

int denominator; // Rule 3

}

Example 2:

// Plane = { (x, y) | x and y are floating point numbers}

class Plane // Rule 1

{

float x; // Rule 2

float y; // Rule 2

}

A Technique to Test a Class; DASO

When a class is built, it must be tested. In general we will write a program (method) to test it. Here are the steps we want to perform.

Step 1. Declare a variable to hold the members of the class: . During the compilation, computer will allocate a space to hold a reference number of a future member. However, computer will not allocate memory for the member. Hence the variable will be initialized null.

Step 2. Allocate for member. After the declaration, we must have an instruction to request allocation for a member (object) of the set, and then save the allocation reference into the variable. ( new (); However, no particular value is given to the allocated memory.

Step 3. Store the member . As soon as the memory of the member is allocated, we must put the member in by assigning the characteristic values to its attributes.

. ( a first characteristic value.

. ( a second characteristic value.

Step 4. Output the object attributes to test our class.

Example: A main method is constructed to test the class of rational numbers.

// Set of Rational Numbers

class SetOfRationals // Rule 1

{

int numerator; // Rule 2

int denominator; // Rule 3

}

class Test

{

// a method to test a class

public static void main(String[] args) throws IOException

{

SetOfRationals a ; // Step 1

a = new SetOfRationals(); // Step 2

a.numerator = 9; // Step 3

a.denominator = 4; // Step 3

a.numerator = a.numerator +5;

System.out.println(a.numerator + "/" + a.denominator); // Step 4

SetOfRationals b = new SetOfRationals(); // Steps 1 and 2

b.numerator = 19; // Step 3

b.denominator = 1;

System.out.println(b.numerator + "/" + b.denominator); // Step 4

SetOfRationals c = new SetOfRationals(); // Steps 1 and 2

c.numerator = 0; // Step 3

c.denominator = 1;

System.out.println(c.numerator + "/" + c.denominator); // Step 4

}

}

Compile and Run the Program.

• You are able to save the program under the same name as the class you just constructed. However the file name will have the suffix java. In this example, it is saved to SetOfRationals.java. The command to compile is (notice the suffix java within the command):

javac SetOfRationals.java

• After the compilation, the compiler javac will generate two object files: SetOfRationals.class and Test.class.

• To execute the program, you must execute the class having the main method. In this example it is Test.class. The command is (notice that there is no suffix class in the command):

java Test

• You must trace the program starting from the main method and check the output.

From this point to the end of the lesson, you will see the new class is expanded while the test class is getting smaller because many instructions from the test class are moved to the new class. First the class is enlarged to include the groups of instructions to construct the members into variables. These groups are called constructors. Second, the class will be expanded to include the methods—the sequence of instructions to perform some tasks on the members such as print the member, add two members, etc..

From Class to Constructors

I recall in the steps DASO (declare, allocate, store, and output) to test a new class. Many instructions from this testing are moved within the class. For example the step 3 becomes the constructors.

Constructors: A constructor is a group of instructions to store a member(object) of the class. We can call it to construct a particular object while the class defines the general attributes of all members. Consider the above example of the class SetOfRationals. The class Test has the following instructions to store 9 and 4 into numerator and denominator. We move them out and put them in the class SetOfRationals.

// Set of Rational Numbers

class SetOfRationals // Rule 1

{

int numerator; // Rule 2

int denominator; // Rule 3

// constructor

public SetOfRationals( int inputNum, int inputDenum)

{

numerator = inputNum;

denominator = inputDenum;

}

}

class Test

{

// a method to test a class

public static void main(String[] args) throws IOException

{

SetOfRationals a ; // Step 1

a = new SetOfRationals(9, 4); // Step 2

a.numerator = 9; // Step 3

a.denominator = 4; // Step 3

a.numerator = a.numerator +5;

System.out.println(a.numerator + "/" + a.denominator); // Step 4

SetOfRationals b = new SetOfRationals(19, 1); // Step 1 & 2

b.numerator = 19; // Step 3

b.denominator = 1; // Step 3

System.out.println(b.numerator + "/" + b.denominator); // Step 4

SetOfRationals c = new SetOfRationals(0, 1); // Step 1 & 2

c.numerator = 0; // Step 3

c.denominator = 1; // Step 3

System.out.println(c.numerator + "/" + c.denominator); // Step 4

}

}

Rules to build a constructor:

Rule 4: Name of constructor is the name of the class.

Example: public SetOfRationals( int inputNum, int inputDenum)

Rule 5: The parameter list is behind the constructor name. This list includes the attribute values to make up the object. The list must have a sequence of attribute type and attribute name.

Example: public SetOfRationals( int inputNum, int inputDenum)

Rule 6: the test program supplies The attribute values and they are included in the parentheses in allocation.

Example: a = new SetOfRationals(9, 4);

where 9 replaces inputNum and 4 replaces inputDenum.

Rule 7: If the test program does not supply the attribute values, the computer will issue an error unless there is a constructor without any parameter. In this case we call it the default constructor.

Example: a = new SetOfRationals() will generate rational 0/1

// Set of Rational Numbers

class SetOfRationals // Rule 1

{

int numerator; // Rule 2

int denominator; // Rule 3

// constructor

public SetOfRationals( int inputNum, int inputDenum)

{

numerator = inputNum; // Step 3

denominator = inputDenum; // Step 3

}

// default constructor

public SetOfRationals( )

{

numerator = 0; // Step 3

denominator = 1; // Step 3

}

}

class Test

{

// a method to test a class

public static void main(String[] args) throws IOException

{

SetOfRationals a ; // Step 1

a = new SetOfRationals(9, 4); // Step 2

a.numerator = 9; // Step 3

a.denominator = 4; // Step 3

a.numerator = a.numerator +5;

System.out.println(a.numerator + "/" + a.denominator); // Step 4

SetOfRationals b = new SetOfRationals(19, 1); // Step 1 & 2

b.numerator = 19; // Step 3

b.denominator = 1; // Step 3

System.out.println(b.numerator + "/" + b.denominator); // Step 4

SetOfRationals b = new SetOfRationals();

System.out.println(b.numerator + "/" + b.denominator);

}

}

From Class to Methods

So far, a class can include the constructors to construct its members either a default member or a non-default member. However, class can be expanded to have any set of instructions (called it a method) to perform certain task. In our example of SetOfRationals, we can move in the println instruction from the main of Test. The title of the method is very similar to the title of constructors. The differences are:

Rule 8: All methods names must be different from the name of their class.

Rule 9: All methods must specify the returned object type or class. If nothing to return, then use void.

// Set of Rational Numbers

class SetOfRationals // Rule 1

{

int numerator; // Rule 2

int denominator; // Rule 3

// constructor

public SetOfRationals( int inputNum, int inputDenum)

{

numerator = inputNum; // Step 3

denominator = inputDenum; // Step 3

}

// default constructor

public SetOfRationals( )

{

numerator = 0; // Step 3

denominator = 1; // Step 3

}

// a method to print

public void Display( )

{

System.out.println(numerator + "/" + denominator); // Step 4

}

}

class Test

{ // a method to test a class

public static void main(String[] args) throws IOException

{

SetOfRationals a ; // Step 1

a = new SetOfRationals(9, 4); // Step 2

a.numerator = 9; // Step 3

a.denominator = 4; // Step 3

a.numerator = a.numerator +5;

System.out.println(a.numerator + "/" + a.denominator); // Step 4

SetOfRationals b = new SetOfRationals(19, 1); // Steps 1& 2

b.numerator = 19; // Step 3

b.denominator = 1; // Step 3

System.out.println(b.numerator + "/" + b.denominator); // Step 4

SetOfRationals c = new SetOfRationals(); // Steps 1& 2

System.out.println(c.numerator + "/" + c.denominator); // Step 4

}

}

Implementation, Compilation, and Execution:

Here is a successful compiled program.

// S Pham

// 11/14/1999

// A Set Of Rationals is built with a testing program Test

import java.io.*; // due to System.out.println

// Set of Rational Numbers

class SetOfRationals // Rule 1

{

int numerator; // Rule 2

int denominator; // Rule 3

// constructor

public SetOfRationals( int inputNum, int inputDenum)

{

numerator = inputNum; // Step 3

denominator = inputDenum; // Step 3

}

// default constructor

public SetOfRationals( )

{

numerator = 0; // Step 3

denominator = 1; // Step 3

}

// a method to print

public void display( ) // Rule 9 for void

{

System.out.println(numerator + "/" + denominator); // Step 4

}

}

class Test

{

// a method to test a class

public static void main(String[] args) throws IOException

{

SetOfRationals a ; // Step 1

a = new SetOfRationals(9, 4); // Step 2

a.numerator = a.numerator +5;

a.display();

SetOfRationals b = new SetOfRationals(19, 1); // Steps 1& 2

b.display();

SetOfRationals c = new SetOfRationals(); // Steps 1& 2

c.display();

}

}

Here is the output given by Java during the execution:

F:\Courses\COMP110>path F:\courses\jdk1.1.7b\bin

F:\Courses\COMP110>path

PATH=F:\COURSES\JDK11~1.7B\BIN

F:\Courses\COMP110>javac rational.java

F:\Courses\COMP110>java Test

14/4

19/1

0/1

F:\Courses\COMP110>

Problems:

1. Modify the method Display to handle the special cases: display only the numerators if the denominator is 1.

2. In the class Test, write some instructions to display the rational number 9/4 in a decimal form. After the execution is completed without error, move these instructions to class SetOfRationals, and then form a new method named printDecimal.

3. Modify and test the method printDecimal so that the output is rounded off at the hundredths.

4. Write a method to add too rational numbers. The formula for addition is as follows:

p/q + r/s = (ps+qr)/qs

5.

Solution 1:

public void display()

{

if (denominator ==1)

{System.out.println(numerator);}

else

{System.out.println(numerator + "/" + denominator); }

}

Solution 3.

public void printDecimal()

{

float decimalForm = (float) numerator / denominator;

System.out.println("decimal number w/o rounding: " + decimalForm);

decimalForm = decimalForm* 100.0f +.5f;

System.out.println("hundred times +.5: " + decimalForm);

int truncate = (int) decimalForm;

System.out.println("Truncate to be an integer : " + truncate);

decimalForm = (float) truncate/100;

System.out.println("Decimal with rounding at 100th: " + decimalForm);

}

output:

4/3

decimal number w/o rounding: 1.3333334

hundred times +.5: 133.83334

Truncate to be an integer : 133

Decimal with rounding at 100th: 1.33

5/3

decimal number w/o rounding: 1.6666666

hundred times +.5: 167.16666

Truncate to be an integer : 167

Decimal with rounding at 100th: 1.67

-----------------------

Replace by:

a.Display();

b.Display();

c.Display();

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download