THINK FIRST·CODE LATER

← All labs

Stock: a class that computes from its own data

Problem

Write a class Stock (not public, same file) and a public class StockApp.

A Stock has four private fields: symbol, name, previousClosingPrice and currentPrice. The constructor receives the symbol and the name; the two prices have accessors and mutators; getChangePercent() returns the change from the previous close to the current price, in percent.

main reads lines symbol name previousClose currentPrice until the end of the input, prints one line per stock, then the number of stocks and the best one of the day. For the input

ORCL Oracle 34.5 34.35
KEAN KeanTech 10 12.5
SLOW SlowCo 80 80

the program prints

ORCL (Oracle): 34.50 -> 34.35, change -0.43%
KEAN (KeanTech): 10.00 -> 12.50, change 25.00%
SLOW (SlowCo): 80.00 -> 80.00, change 0.00%
Stocks read: 3
Best of the day: KEAN (25.00%)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l10-6-data-1.txt

ORCL Oracle 34.5 34.35
KEAN KeanTech 10 12.5
SLOW SlowCo 80 80
What your program must print for l10-6-data-1.txt
ORCL (Oracle): 34.50 -> 34.35, change -0.43%
KEAN (KeanTech): 10.00 -> 12.50, change 25.00%
SLOW (SlowCo): 80.00 -> 80.00, change 0.00%
Stocks read: 3
Best of the day: KEAN (25.00%)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l10-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l10-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.