Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Faculty of Computer Science and Information Technology

SSK 3101 (Computer Programming II)


Semester I 2020/2021

Lab 2 (Individual)

Learning Objective:
 Construct a Simple Object-Oriented Program (P4)

Instructions:
1. Find solution to each problem and get help from your lecturer whenever you
encounter any problem.
2. Demonstrate your source program and the output to your lecturer.
3. Submit UML diagram for each problem after the lab session.
4. Submitted by 2/11/2020 at 5pm

Questions:

1. Design a class named Stock that contains:


 A string data field named symbol for the stock's symbol.
 A string data field named name for the stock's name.
 A double data field named previousClosingPrice that stores the stock price
for the previous day.
 A double data field named currentPrice that stores the stock price for the
current time.
 A constructor that creates a stock with the specified symbol and name.
 A method named getChangePercent() that returns the percentage changed
from previousClosingPrice to currentPrice.

Draw the UML diagram for the class then implement the class. Write a test program
that creates a Stock object with the stock symbol ORCL, the name Oracle
Corporation, and the previous closing price of 34.5. Set a new current price to 34.35
and display the price-change percentage.

2. Design a class named Employee that has the following fields:


 name. The name field references s String object that holds the employee’s
name.
 idNumber. The idNumber is an integer variable that holds the employee’s
identification number.
 department. The department field references a String object that holds the
name of the department where the employee works.
 position. The position field references a String object that holds the
employee’s job title.
 A constructor that accepts the following values as arguments and assigns them
to the appropriate fields: name, idNumber, department, and position.
 A constructor that accepts the following values as arguments and assigns them
to the appropriate fields: name, and idNumber. The department, and position
fields should be assigned an empty string (“”).
 A no-arg constructor that assigns empty strings (“”) to the name, department,
and position fields, and 0 to the idNumber field.

Draw the UML diagram for the class then implement the class. Write appropriate
mutator/setter methods that store values in these fields and accessor /getter
methods that get the values in these fields. Once you have written the class, write
a test program that creates an array of three Employee objects to hold the
following data:

Name ID Number Department Position


Susan Meyers 47899 Accounting Vice President
Mark Jones 39119 IT Programmer
Joy Rogers 81774 Manufacturing Engineer

Figure 1: List of employees

The program should store this data in three objects and then display the data for
each employee on the screen as listed in Figure 1.

2. Smart Beauty Shop needs a Fat Gram Calculator that calculates the percentage of
calories from fat, given a food item, the total of calories and fat grams in a food item.
The number of calories from fat cannot be greater than the total of calories in the
food item. If the program determines that the number of calories from fat is greater
than the number of calories in the food item, it should display an error message
indicating that the input is invalid.

Fat contains 9 calories per gram. The calculator should display the percentage of
the calories that come from fat. The percentage of calories from fat can be
calculated as follows:
Calories from fat = grams of fat * 9
Percentage of calories from fat = (calories from fat / total calories) * 100

If the calories from fat are less than 30% of the total calories of the food, it should
also display a message indicating the food is low fat.
The number of calories from fat cannot be greater than the total of calories in the
food item. If the program determines that the number of calories from fat is greater
than the number of calories in the food item, it should display an error message
indicating that the input is invalid.

Draw the UML diagram for the class then implement the class and write an
application that creates a food object. Your class should have the following:
a. Give an appropriate name for your class
b. Define appropriate data member and their data type
c. Constructor with and without arguments
d. Setter method and getter method
e. Other appropriate methods.
f. Test your application with food items below:

Food Item Total Calories Fat (gram)


Crunchy Peanut 539.7 32.22
Muruku 111.0 7.00
Food A 211.0 7.00
Nasi lemak 644.0 83.0

You might also like