Coding
Datatype Class
In java, everything should be in the classes. There are two types of classes: data type class and driver class. Data Type Class -Data type class is a combination of the information that describe the group of objects of the same type and actions that is used to access these information. The information of object are hold by data members (fields) The actions are defined in methods in main() to declare an object of a data type it needs to access constructors Before writing the code of a data type class, we need to create the UML. UML of a data type class looks like a table with 1 column x 3 rows as below:
-The first row: the name of data type class in the center, Person_yourLastName
-The second row: list data members (or fields) of the object. Normally, the minus sign (-) means private access are placed in front of data members that limit accessing data members from outside of the data type class -The third row: list constructors, mutator, methods, accessor methods,
method toString() and other methods. Normally, the plus sign (+) means public will be placed in front of methods that allow the outside can access these methods
The following is the UML of class Person_White:
|
ClassName |
|
-dataMember1: String -dataMember2: int |
|
+ClassName() +ClassName(dataMember1: String, dataMember2: int) +setDataMember1(n: String): void +setDataMember2(h:int):void +getDataMember1(): String +getDatamember2():int +otherMethod(): double +toString(): String |
Based on the UML, we can write the code of the data type class (see other topics)
Data members: is the list of variables that are declared to hold the information of the object of class Person_White. These data members should be independent to each other. If there is a data member depend on the value of other data member, you should remove it from the list.
The syntax of the code to declare data members is:
datatype variableName;
Constructor: is the code that is used to create an object of the datatype class. One class can have no constructor, one constructor or many constructors. The constructor can be default constructor, no-argument constructor, parameterized constructor or copy constructor. The purpose of the code in a constructor is to initialize values for each data member that are declared in the data members list.
The syntax of constructors as below:
No-argument constructor:
public ClassName()
{
dataMember1 = 0; //if data member is a number
dataMember2 = “”; //if data member is a stringP
dataMember3 = null; //if dataMember3 is an object
}
Parameterized Constructor:
public ClassName(datatype1 var1, datatype2 var2, datatype3 var3)
{
dataMember1 = var1;
dataMember2 = var2;
dataMember3 = var3;
}
Copy constructor:
public ClassName(ClassName oneObject)
{
dataMember1 = oneObject.dataMember1;
dataMember2 = oneObject.dataMember2;
dataMember2 = oneObject.dataMember3;
}
How to create an object in main()
The syntax:
ClassName objectName = new ClassName(); //access no argument constructor or default constructor
ClassName objectName = new ClassName(var1, var2, var3); //access parameterized constructor
ClassName objectName = new ClassName(oneObject); //access copy constructor
Mutator methods: the purpose of mutator method is to set up new value to one data member
public void setDataMember ( datatype var )
{
dataMember = var;
}
How to access the mutator method in main() object.setDataMember(aVar
Accessor methods: is the method to return the value of one data member Syntax:
public datatypeReturn getDataMember()
{
return dataMember;
}
How to access the accessor method in main() datatype variableName = object.getDataMember();
Method toString(): is used to creating an output string that provide the output of the information of an object of data type class Syntax:
public String toString()
{
String str = “OUTPUT STRING\n";
str = str + "the output string of the object in required format\n”;
return str;
} How to access the method toString() from main()
System.out.println(object.toString());
Driver Class
PSEUDO-CODE
What is pseudo-code?
Before writing the code of main() with any language, the programmers should have the pseudo-code.
Pseudo-code is written in English. It consists of consecutive phrases that are used to explain tasks within the program’s algorithm to be qualified the requirements of the project.
Why do we need the pseudo-code?
The requirement of a project is always complicate. First, the programmers need to understand what the requirements asking for. Then, organize their thought and create the program.
Writing the pseudo-code is just thinking of how to solve the problem and list all the tasks we need and ignore about the syntax.
With the pseudo-code, it is easy to modify or to review the algorithm with the customers or other people who involve in the project but do not have the knowledge about how to write the code
How to write the pseudo-code?
1.Understand the requirements
2.List all the main tasks that must be accomplished on a piece of paper in English
3.Then focus on each task, try to break the main task down into very small tasks as you can that can be explained with a short phrase.
In the pseudo-code, it is not necessary to mention the need to declare variables but we need to list all the initialized values.
Normally, when we write the pseudo-code, we do not need to consider which language we will use to write the code.
The pseudo-code has more details that can help to write the code more easily.
For example with the requirement:
Provide the application that can help users to calculate the area of a rectangle with the length and width are entered from the keyboard then display the output in the following format:
RECTANGLE -----------------------
Length: 12.50 Width: 7.20 Area: 93.75 We can have the pseudo-code as below: -Read the information of the rectangle from the keyboard for length and width -Create the object of data type class Rectangle to pass length and width to the object -Use the object to access the method toString() to display the requested output
To create the object of a data type class:
There are several ways to create the object of a data type class
1. Create the object by access no argument constructor
2. Create the object by access the parameterized constructor
3. Create the object by access the copy constructor
1. Create the object by access no argument constructor:
Syntax:
DatatypeClass objectName = new DatatyteClass();
For example:
Rectangle rect = new Rectangle();
with this way, the length of the object rect is 0.0 and the width of rect is 0.0
2. Create the object by access the parameterized constructor
Syntax:
DatatypeClass objectName = new DatatypeCalss(dataMember1, dataMember2, ....):
For example:
Rectangle rect = new Rectangle(len, w);
With is way, length = len and width = w
3. Create the object by access the copy constructor
Syntax:
DatatypeClass objectName = new DatatypeClass( existingObject);
For example:
Rectangle rect1 = new Rectangle(5.5, 3.5); //CREATE OBECT rect1 BY PARAMETERIZED CONSTRUCTOR
Rectangle rect2 = new Rectngle(rect1); //CREATE OBJECT rect2 BY COPY CONSTRUCTOR
Syntax: objectName.method(); For example: Rectangle rect = new Rectangle(length, width); float area = rect.toString(); //OBJECT rect ACCESS METHOD toString()
Example
SYNTAX of if statement
if (condition)
statement; //statement is only executed when condition is true
EXAMPLE
int x = 5;
int y = 5;
if ( x == y ) //true
System.out.println("x is equal to y");
SYNTAX of if..else statement
if ( condition )
statement1; //statement1 is only executed when condition is true; otherwise skip this and jump to else
else
statement2; //statement2 is only executed when condition is false
EXAMPLE
char var1 = 'A';
char var2 = 'B';
if ( var1 == var2)
System.out.println("var1 and var2 arer the same");
else
System.out.println("var1 and var3 are different");
SYNTAX of if..else if statement
if ( condition1 )
statement1;
else if ( codition2 )
statement2;
else
statement3;
EXAMPLE
int x = 10;
int y = 12;
if ( x == y )
System.out.println( "x is equal to y");
else if (x > y )
System.out.println( "x is greater than y");
else
System.out.println( "x is less than y");
SYNTAX of nested if statement
if ( condition1 )
{
if ( condition2 )
statement1;
else
statement2;
}
else
{
statement3;
}
EXAMPLE
Scanner keyboard = new Scanner(System.in);
int x, y;
System.out.println("Enter value of x: "); x = keyboard.nextInt();
System.out.println("Enter value of y: "); y = keyboard,nextInt();
if ( x < 100 && y < 200)
{
if ( y < x )
System.out.println("y is less than 100");
}
SYNTAX swich STATEMENT
Scanner keyboard = new Scanner(System.in);
int task;
System.out.println("Type 1, 2, or 3 to select the task: "); task = keyboard.nextInt();
switch ( task )
{
case 1:
System.out.println("You select task 1");
break; //this tells skipping the rest
case 2:
System.out.println("You select task 2");
break;
case 3:
System.out.println("You select task 3");
break;
}
// IN THE SPECIAL SITUATION, TWO CASES HAVE THE SAME ACTIONS:
int task;
System.out.println("Type 1, 2, or 3 to select the task: "); task = keyboard.nextInt();
switch ( task )
{
case 1:
case 2:
System.out.println( "Task 1 and Task 2 do the same actions");
break;
case 3:
System.out.println( "You select task 3");
break;
}