Lab 01: Mini Problems
Learning Objectives
Students should
- be able to translate an imperative/procedural code into an object-oriented code.
- be able to write simple Java program and use JShell for testing and prototyping.
Initializing
To get the files, run the following command from your PE node.
We recommend creating a new directory called mini
to store all your lab mini problems.
1 |
|
Mini Problem 1
Alice wants to invest in several companies. She decides to create a list of her portfolio and track her progress. There are three kinds of companies she can invest in. The information needed for the companies differ depending on the kind of companies.
- MNC: Only the name and the valuation.
- Startup: Besides the name and the valuation, we also need to know the number of years for the valuation to double. We call the number of years as X.
- SME: Besides the name and the valuation, we also need to know the amount to increase each year.
Depending on the kind of company, the valuation may increase or decrease as follows.
- MNC: The valuation will always be the same.
- Startup: The valuation will double after every X number of years where X is the number stored as above.
- SME: The valuation will always increase by a fixed amount each year.
Alice wants to keep track of the valuation of the companies in her portfolio for a given number of years. At the end, she wants to print the latest valuations.
- MNC: The format will be
[name]: $valuation
. - Startup:
<name>: $valuation
- SME: The format will be
(name): $valuation
.
Mini1.1.in | |
---|---|
1 2 3 4 |
|
Mini1.1.out | |
---|---|
1 2 3 |
|
Potential Changes
As designs are intricately related to potential changes, you should think about the following potential changes.
- We may want to add different kinds of companies.
- We may want to change the behavior of each kind of companies independently of one another.
Running and Testing
First, you need to enter the directory mini1
using the following command.
1 2 |
|
Then you need to compile it:
1 |
|
You can run the following command to test a input file individually.
1 |
|
Alternatively, you can also run the test script.
1 |
|
Mini Problem 2
Bob -- a startup founder -- wants to design a simple payroll system for his company. The main goal is simple: he wants to know how much money he needs to prepare to pay her employees each month. On top of that, he mentioned a few other requirements as listed below.
- Each employee should have a unique consecutive ID, starting from 0.
- There are three types of employees: managers, full-time, and interns.
- Managers and full-time employees are paid monthly while interns are paid hourly.
- Full-time employees and interns can be paid overtime. This is simply the overtime hours * overtime rate.
Since not all information are useful, the constructor need not receive all information. But you are guaranteed that there will be no other kinds of employees besides those listed above.
Initially Alice was tasked to handle this project. However, since her timeline is very tight, he rushed this project and the resulting code does not adhere to basic OOP principles. Alice asked for your help to refactor her code and make it more readable and extendable. Help Alice improve the quality of the code of this payroll system!
Potential Changes
As designs are intricately related to potential changes, you should think about the following potential changes.
- We may want to add different kinds of employees.
- We may want to change the behavior of each kind of employees independently of one another.
Running and Testing
First, you need to enter the directory mini2
using the following command.
1 2 |
|
You will find Employee.java
and Payroll.java
but there is no main
method in any of these files.
We will be using JShell to run and test our program.
To load the files using JShell, we can either (i) pass the Java file as an argument to JShell or (ii) open the files interactively inside JShell using /open
.
Option (i) | |
---|---|
1 2 3 4 |
|
Option (ii) | |
---|---|
1 2 3 4 5 |
|
If you made any changes to your code, you can also reload them using the /open
command (i.e., option (ii)).
This will update the class definition to the latest definition in your JShell session.
To test your program, you may create objects and invoke their methods.
The following is a sample run in a script called Sample.jsh
.
Sample.jsh | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
You can load this script using the following command:
1 2 3 4 5 6 7 8 9 |
|