Workshop #2: Class and Object
Learning Outcomes:
Upon successful completion of this workshop, you will have demonstrated the abilities to:
Design and implement a class.
Create an object from a class
Describe to your instructor what you have learned in completing this workshop.
Requirements:
Part 1: Find classes and use UML to draw class structure [2 points]
Problem: Mr. Hung is the owner of the shop that sells guitars. He wants you to build him a shop
management app. This app is used for keeping track of guitars. Each guitar contains
serialNumber, price, builder, model, back Wood, top Wood. The guitar can create a melodious
sound. The shop will keep guitars in the inventory. The owner can add a new guitar to it, he
search also the guitar by the serialNumber.
Requirement: Write your paper down classes in the problem and use UML draw class structure.
Note: show members of a class: fields and methods
Do it yourself before getting help
Guideline:
Apply design guideline :
Main nouns: Guitar
Auxiliary nouns describe details of the guitar:serialNumber, price, builder, model, back
Wood, top Wood.
Verbs describe behaviors of the guitar: create Sound
Continue finding other nouns
Main nouns: Inventory
Auxiliary nouns describe details of the inventory: the list(array) of guitars
Verbs describe behaviors of the inventory: add a new guitar, search the guitar by
serialNumber.
Using UML to draw a class diagram
Part 2: Implement the Guitar [3 points].
Step by step workshop instructions:
- Create a new project named “workshop2”
- In the project, create a new file named “Guitar.java”
1
o Declare fields with access modifier as private: String serialNumber, int price,
String builder, String model, String backWood, String topWood
o Declare and implement methods with access modifier as public:
public Guitar() {…} : to assign all fields to empty values
public Guitar( String serialNumber, int price, String builder, String model,
String backWood, String topWood) {…}: to assign all fields by input
parameters
public String getSerialNumber(){…}: return the value of the field
serialNumber.
public void setSerialNumber(String serialNumber){…}: if the input
parameter is not empty then assign it to the field serialNumber.
Implement getter/setter of all other fields
public void createSound(){…}: in the method, invoke all getters and use
System.out to print out values after getting.
- In the project, create a new file named “Tester.java. Create the method main in here,
you type:
public class Tester {
public static void main(String[] args) {
Guitar obj1=new Guitar();
Guitar obj2=new Guitar("G123",2000,"Sony","Model123","hardWood","softWood");
System.out.println("State of obj1:");
obj1.createSound();
System.out.println("State of obj2:");
obj2.createSound();
System.out.println("set price = 3000 of obj1");
obj1.setPrice(3000);
System.out.println("get price of obj1:" + obj1.getPrice() );
}
}
The output is:
2
Part 3: Implement Employee management program [5 point]
- Write an employee management program for Fsoft according to the following
description:
- Employee information: Code, Fullname, Phone, Salary, Salary coefficient, department.
- The program allows the user to enter the number of employees.
- Print out the list of employees.
- Print out the total salary of the employees.
- Update employee salary based on the employee code entered by the user.
- Remove an employee based on the employee code entered by the user.