COSC 1
COSC 1437 – LAB3
TITLE
Datatype class, Driver class - Adult, AdultWeightManagement – Customer, SavingMoney
HOW TO DO EACH PART
*From now and on yourLastName will be changed to your last name.
*Your program should change White to your last name.
*Your program should change McKINLEY WHITE to your name.
*Change Mary Lane to the name of user who is using the Investment Application entered from the keyboard.
*Write the file name as the first comment line at the top of the program.
*After running your program, get the picture of the output window from your program with your on to paste at the bottom of the pseudo-code to turn in.
FROM LAB3, WE USE OBJECT ORIENTED PROGRAMMING TO WRITE THE CODE. TO DO THIS, WE SHOULD HAVE TWO TYPES OF CLASSES IN ONE PROJECT: DATA TYPE CLASS AND DRIVER CLASS.
TO KNOW HOW TO WRITE THE CODE OF A DATA TYPE CLASS AND HOW TO CREATE AN OBJECT AND USE THE OBJECT TO ACCESS THE METHODS OF DATA TYPE CLASS IN THE DRIVER CLASS; YOU CAN GET HELP BY READING THE TOPICS RELATING TO THESE IN ‘HOW TO DO LAB” AND FOLLOW THE INSTRUCTION IN THIS LAB.
FOR ANY QUESTIONS, CONTACT VIA EMAIL LiemLe@dcccd.edu OR JOINT TO OFFICE HOUR TO GET HELP.
*Step1: Read the requirement of each part; write the pseudo-code in a word document by listing the step by step what you suppose to do in main() and then save it with the name as Lab3_pseudoCode_yourLastName.
*Step2:
-start editor (for example eClipse) create the project with the following project name:
Part 1: FA2021_LAB3PART1_yourLastName
Part 2: FA2021_LAB3PART2_yourLastName
-add data type class:
Part1: FA2021_Adult_yourLastName
Part2: FA2021_Customer_yourLastName
-add a driver class (that contain main()
Part 1: Class: FA2021_AdultWeightManagmeent2_yourLastName
Part 2: Class: FA2021_SavingMoney_yourLastName
*Step3: follow step by step in the pseudo-code (or the flowchart) to write the java code in main() or driver class.
*Step:4 compile and run the program.
*Step5: debug if there are any errors to complete the program.
FROM LAB3, WE ARE GOING TO WRITE THE CODE BASED ON OBJECT ORIENTED PROGRAMMING
Object Oriented Programming concepts is the way the programmers define the data type of objects with the fields or data members (holding the information of objects), constructors (including no-argument constructor, parameterized constructor) and methods (operations are accessed by objects of the class)
In the driver class including main(), the programmers should declare an object of this data type by calling one of constructors then using this object to access the public methods of this data type to solve the problem of the requirement.
LAB3 PART1
READ THE REQUIREMENT, CREATE THE PSEUDO-CODE BEFORE WRITING THE CODE
REQUIREMENT
Create the project FA2021_LAB3PART1_yourLastName
In this Lab3 part1, we create the application FA2021_AdultWeightManagement_yourLastName that helps users to calculate the weight if the users provide their height and their expected BMI rate.
You must provide two classes:
· data type class: FA2021_Adult_yourLastName and driver class named
· driver class: FA2021_AdultWeightMangement2_yourLastName.
DATA TYPE CLASS
Before writing the code of a data type class, you must create the UML. If you do not have any tool to create UML, you can create one table with 1 column x 3 rows as below: the first row for class name in middle, the second row for the list of data members, the third row for the constructors and the list of methods.
-This is the first time. I provide the UML of class FA2021_Adult_yourLastName as below where White is the last name.
|
FA2021_Adult_Smith |
|
-name: String -height: int -expectedBMI: float |
|
+FA2021_Adult_Smith() +FA2021_Adult_Smith(name:String, height:int, respectedBMI:int) +calculateWeight(): double +toString(): String |
To write the code for Datatype class, you should base on the UML above:
-data members list: define variables to hold the information of an adult about name (String), height (int inches), expected BMI rate (float) those are listed
-provide 2 constructors: the no-argument constructor and parameterized constructor
-provide the method to calculate the weight by using the following formula:
weight = (BMI * height * height)/703
-provide the method toString() to create the output in the requested format.
For example: Your name is James Smith, the name of user is Mary Lane, the height is 80 inches, the respected BMI rate is 20.6 then the result weight is 187.5 pounds. The output as below:
----------------------------------------------
File: SU2021_AdultWeightManagement_White.java
Adult Weight Management – McKINLEY WHITE
Standard BMI: 18.5 – 24.9
----------------------------------------------
Name: Mary Lane
Height (inches): 80
BMI: 20.6
----------------------------------------------
Weight (pounds): 187.5
DRIVER CLASS
Before writing the code, you must create the pseudo-code for the main().
The driver class named FA2021_AdultWeightManagement2_yourLastName includes the function main().
In the function main(), after reading all the information of one adult that are needed for name, weight and expected BMI rate, you must create the object of the above FA2021_Adult_yourLastname class by calling one constructor of the data type class, then use this object to access the method toString() to display the result of the weight.
LAB3 PART2
requirement
Create a project named FA2021_LAB3PART2_LastName
Provide the application that helps customers to calculate their saving money after a year if the users provide their name, the money amount users deposit to the bank and the interest rate.
You must provide two classes:
· data type class FA2021_Customer_yourLastName
· driver class FA2021_SavingMoney_yourLastName
DATA TYPE CLASS
Before writing the code of Datatype class you must provide UML of data type class
To write the code of this data type class, you must base on the UML you provided above.
The data type class named FA2021_Customer_yourLastName that includes:
-data members list: define variables to hold values of an adult about name (String), account number(String), amount of money (float), interest rate (float)
-provide 2 constructors: the no-argument constructor and parameterized constructor
-provide the method to calculate the saving money by using the following formula:
Interest amount = amount of money * interest rate
Total saving money = amount of money + interest amount
-provide the method toString() to create the output in the requested format.
--------------------------------------------------
File: FA2021_SavingMoney_White.java
Saving Money Application – McKINLEY WHITE
--------------------------------------------------
Name of Bank Customer: Mary Lane
Amount of Money: 12000.00
Interest Rate: 0.49%
--------------------------------------------------
Total Saving Money: 12058.80
DRIVER CLASS
Before writing the code, you must create the pseudo-code for the main().
The driver class named FA2021_SavingMoney_yourLastName includes the function main().
In the function main(), after reading all the information of one customer about name, amount of money, interest rate. You must create the object of the class FA2021_Customer_yourLastname class by calling one constructor of the data type class, then use this object to access the method toString() to display the result of the saving money.
HOW TO TURN IN THE LAB
You should turn in the following files: (yourLastName should be your last name)
UML Pseudo-code and the output pictures of both parts
FA2021_Adult_yourLastName.java
FA2021_AdultWeightManagement_yourLastName.java
FA2021_Adult_yourLastName.class
FA2021_AdultWeightManagement_yourLastName.class
FA2021_Customer_yourLastName.java
FA2021_SavingMoney_yourLastName.java
FA2021_Customer_yourLastName.class
FA2021_SavingMoney_yourLastName.class
IF YOU GET ANY PROBLEM TO SUBMIT FILEs .class YOU CAN COMPRESS ALL PROJECT INTO ONE FILE .zip or .rar TO UPLOAD TO eCampus
HOW TO GRADE THE LAB
|
Item |
Max score |
|
Submit the lab on time |
|
|
Part 1: copy UML - pseudo-code – output picture |
1 |
|
Part 1: class Adult with data members, |
1 |
|
Part 1: no-argument constructor, parameterized constructor |
2 |
|
Part 1: method calculate the weight |
1 |
|
Part 1: method toString() |
1 |
|
Part 1: Driver class - Declare variables – read input |
1 |
|
Part 1: create the object |
1 |
|
Part 1: display the output by calling method toString |
1 |
|
Part 1: File name as comment at top and write the comment |
1 |
|
Part 1: compile success, qualified the requirement |
3 |
|
Part 2: UML- pseudo-code – output pictures |
2 |
|
Part 2: Class Customer: data members |
1 |
|
Part 2: no-argument constructor, parameterized constructor |
2 |
|
Part 2: methods to calculate saving money |
1 |
|
Part 2: method toString – prepare for the output |
1 |
|
Part 2: Driver class – declare variables – read input |
1 |
|
Part 2: Create the object |
1 |
|
Part 2: Display the output as requested by access toString |
1 |
|
Part 2: Compile success, qualified the requirement, |
3 |
|
Part 2: Write comments, file name at the top |
1 |
|
Total |
30 |
· 1. Datatype Class-UML - How To Create an Object
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)
·
2. Datatype member
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;
·
3. Constructor
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
·
4. Mutator Method
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
·
5. Accessor Method
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();
·
6. Method toString()
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());