E9 (10 marks)
Focus: basics of Object Oriented Programing
Q1. Design a class named Stock that contains:
Four private attributes:
String symbol for the stock’s symbol.
String name for the stock’s name.
double prevClosingPrice that stores the stock price for the previous day.
double currentPrice that stores the stock price for the current time.
A two-argument constructor that creates a stock with the specified symbol and name.
Getter and setter methods for name, prevClosingPrice, and currentPrice.
A method named getChangePercent() that returns the percentage changed from
prevClosingPrice to currentPrice.
Write a test program that creates a Stock object with the stock symbol SUNW, the name
Sun MicroSystems Inc., and the previous closing price of 34.5. Set a new current price to
34.35 and display the price-change percentage.
Sample run
Q2. Design a class named StopWatch that contains:
Private data fields startTime and endTime.
A method named start() that sets the startTime to the current time.
A method named stop() that sets the endTime to the current time.
A method named getElapsedTime() that returns the elapsed time for the stopwatch
in milliseconds.
Use the static method: System.currentTimeMillis() to read the current time in
milliseconds.
Write a test program that measures the execution time of creating an array of 1,000,000
random numbers and sorting them using Arrays.sort() method.
Sample run:
Most questions are based on / taken from the exercises and examples listed in the textbook used by the course.
Q3. Design a class named QuadraticEquation for a quadratic equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐. The
class contains:
Private data fields a, b, and c that represent three coefficients.
A constructor to set a, b, and c.
Three getter methods for a, b, and c.
A method getDiscriminant() that returns the discriminant 𝑏 2 − 4𝑎𝑐.
Two methods getRealRoot1() and getRealRoot2(). If the discriminant is
nonnegative, the methods should return the two roots of the equation:
However, if the discriminant is negative, let these methods return 0.
Write a test program that prompts the user to enter values for a, b, and c and displays the
result based on the discriminant. If the discriminant is positive, display the two roots. If the
discriminant is 0, display the one root. Otherwise, display “The equation has no real roots.”
Sample run:
Lab Instructions
For the above exercises, the TA will discuss each question with you. S/he will hear ideas from you
and give an overview of how the question should be solved. After that, you will be given time to
solve the question by yourself during the lab time. Once you finish, you should show your
solution to your TA for grading. The TA will eventually give the solutions to you. You should then
attempt to fix any errors you had with your solutions and then show the updated version of your
solution again to the TA.
Marking guide:
60% for attempting to solve the questions before answers are distributed, and
40% for correct code after the answers are given away.
Most questions are based on / taken from the exercises and examples listed in the textbook used by the course.