Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

REVISION - JAVA PROGRAMMING

1. A mail-order house sells five products whose retail prices are as follows: Product 1 =
RM2.98; product 2 = RM4.50; product 3 = RM9.98; product 4 = 4.49 and product 5 =
RM6.87. Write an algorithm and draw a flowchart that reads a series of pairs of numbers
as follows:
a. product number
b. quantity sold
You must use switch statement to determine the retail price for each product. It should
calculate and display the total retail value of all products sold. Use sentinel-controlled
loop, -9999, to determine when the program should stop looping and display the final
results.
Use separate methods to:
 read the product number
 determine the price of each product based on the product number
 read the quantity of the product sold
 calculate the total price for each product sold
 display the total price

2. (Implementing inheritance and apply the use of keyword super and this)

Assume you have to develop a grading application for grading student’s activity. Each
activity will be given a numeric score (0 to 100), and accordingly will be given a grade
(A, B, C, D, or F). The grade is given based on the condition in Table 1 below.

Table 1: Scores and their corresponding grades

Condition Letter Grade


Score >= 90 A
Score >= 80 B
Score >= 70 C
Score >= 60 D
Score < 60 F

Create a class name GradedActivity, with an attribute to hold the score for the activity.
The SetScore method sets the score from a given parameter value and the getScore
method returns the score. A getGrade method should return a grade’s letter based on the
given numeric score, according to the rules given in Table 1.

Then create a new class Essay which extends the GradedActivity class. The new class is
to determine the grade a student receives for an essay based on the accumulated score (0
to 100), which is accumulated based on a list of score’s distributions, as follows:
Grammar: 0 to 30
Spelling: 0 to 20
Correct length: 0 to 20
Content: 0 to 30
For example, if a student receives score 30 for Grammar, 20 for Spelling, 12 for Correct
length and 10 for Content, the total score is 30+20+12+10=72 and thus given C as grade.
Use the keyword super to update the attribute of the superclass. Use the keyword this to
refer to the current class’ attributes and methods.

Implement the Java program to demonstrate the application of both classes.

3. (Implementing overriding and overloading method technique)

Using the solution and Java program produced in Question 2, complete the following
tasks:

a. Create a new method setScore() in class Essay, which accepts four parameters (each
parameter’s value will be given to each of the score’s distribution attributes). This
method will overload the existing method setScore() in class GradedActivity.

b. The class Essay actually contains an issue since it still allows a program to set the
score without setting the score’s distribution (if using setScore() with a parameter). To
protect this, add a new method setScore() with one parameter in class Essay to
override the existing method setScore() from class GradedActivity. This new method
will do nothing except showing an error message which informs the programmer that
the setScore() method with one parameter cannot be used for object from class Essay.

For example, the program may show the following error message (or whatever
message that you have defined) when an Essay’s object (named essayObject) is
calling the new method with value -2 i.e. essayObject.setScore(-2); :
Class Essay is not allowed to call setScore() method with single value (calling with
parameter -2).

c. Add a new getGrade() method in class Essay, which will override the existing
getGrade() method in class GradedActivity. This method will determine if any of the
score’s distributions is zero (0). If any of the distributions is 0, the grade ‘F’ should be
returned as grade, even though total score is beyond 60 (the minimum score for
grades above F).

You might also like