Professional Documents
Culture Documents
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
Week: 04
1) Days Alive
You can construct and set a Calendar object from a given year, month,
and day, like this:
You can then find out what the result is, by applying the get method:
System.out.println(later.get(Calendar.YEAR));
System.out.println(later.get(Calendar.MONTH));
System.out.println(later.get(Calendar.DATE));
However, that approach does not solve our problem (unless you are willing to replace 100
with other values until, by trial and error, you obtain today’s date.) Instead, use the
getTimeInMillis method like this:
Page 1 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
long timeDiff = (day2.getTimeInMillis() -
day1.getTimeInMillis());
int daysAlive = (int) (timeDiff / (1000*60*60*24));
In our situation, one of the Day objects is jamesGoslingsBirthday, and the other is today’s
date. This can be obtained with the constructor that has no arguments:
After learning the days alive concept, please create a simple java program using Calendar
class to have the following example output :
Today: 2022-03-01
Days alive: 7301
3) Help
4) Quit
Page 2 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
The numbers should be supplied automatically when options are added to the menu.
Here we turn the list in Step 1 into a set of methods, with specific types for the parameter
variables and the return values :
To complete the public interface, we need to specify the constructors. We have two choices:
- Supply a constructor Menu(String firstOption) that makes a menu with one option.
Either choice will work fine. If we decide in favor of the second choice, the user of the class
needs to call addOption to add the first option—after all, there is no sense in having a menu
with no options. At first glance, that seems like a burden for the programmer using the class.
But actually, it is usually conceptually simpler if an API has no special cases (such as having to
Page 3 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
supply the first option in the constructor). Therefore, we decide that “simplest is best” and
specify the constructor
public Menu()
Here is the documentation, with comments, that describes the class and its
methods:
/**
*/
/**
*/
public Menu()
/**
*/
/**
*/
Page 4 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
What data does a Menu option need to keep in order to fulfill its responsibilities? Of course,
in order to display the menu, it needs to store the menu text. Now consider the addOption
method. That method adds a number and the option to the menu text. Where does the
number come from? The menu object needs to store it too, so that it can increment
whenever addoption is called.
We now implement the constructors and methods in the class, one at a time, in the order
that is most convenient. The constructor seems pretty easy:
public Menu()
menuText = "";
optionCount = 0;
{
System.out.println(menuText);
}
Page 5 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
The addOption method requires a bit more thought. Here is the pseudocode:
A ) symbol
A "newline" character that causes the next option to appear on a new line
How do you add something to a string? If you look at the API of the String class, you will find
a method concat. For example, the call
menuText.concat(option)
creates a string consisting of the strings menuText and option. You can then store that string
back into the menuText variable:
menuText = menuText.concat(option);
We use the + operator in our solution because it is so convenient. Our method then
becomes
optionCount = optionCount + 1;
Page 6 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
Step 6 : Wrap it up
Please compile the previous steps into a working simple java program by implementing
classes to have the following output :
3) Help
4) Quit
Page 7 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
There are really two concepts here: a cash register that holds coins and computes their total,
and the values of individual coins. It makes sense to have a separate Coin class and have
coins responsible for knowing their values.
Page 8 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
public class CashRegister
{
. . .
public void receivePayment(int coinCount, Coin coinType)
{ . . . }
{
payment = payment + coinCount * coinType.getValue();
}
. . .
}
2) Minimizing Dependencies
Many methods need other classes in order to do their jobs. For example,
the receivePayment method of the restructured CashRegister class now
uses the Coin class. We say that the CashRegister class depends on the
Coin class.
The method depends on System.out. Not every computing environment has System.out. For
example, an automatic teller machine doesn’t display console messages. In other words, this
design violates the rule of minimizing dependencies. The printBalance method couples the
BankAccount class with the System and PrintStream classes.It is best to place the code for
producing output or consuming input in a separate class. That way, you decouple
input/output from the actual work of your classes.
Page 9 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
As a rule of thumb, a method that returns a value should not be a mutator. For example, one
would not expect that calling getBalance on a BankAccount object would change the
balance. (You would be pretty upset if your bank charged you a “balance inquiry fee”.) If you
follow this rule, then all mutators of your class have return type void.
Sometimes, this rule is bent a bit, and mutator methods return an informational value. For
example, the ArrayList class has a remove method to remove an object.
Page 10 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
/**
Computes the total balance of the given accounts.
@param accounts a list of bank accounts
*/
public double getTotalBalance(ArrayList<String> accounts)
{
double sum = 0;
while (studentNames.size() > 0)
{
BankAccount account = accounts.remove(0); // Not
recommended
sum = sum + account.getBalance();
}
return sum;
}
}
This method removes all names from the accounts parameter variable. After a call
allAccounts is empty!
Page 11 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
Quarter = 25 cents
Dime = 10 cents
Nickel = 5 cents
Penny = 1 cent
Already finish? Raise hand or type in the chat box to notify the lecturer that you have finished
this exercise.
Page 12 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
Up to now, we have used a very simple approach to testing. We provided tester classes whose
main method computes values and prints actual and expected values. However, that approach
has limitations. The main method gets messy if it contains many tests. And if an exception occurs
during one of the tests, the remaining tests are not executed.
Unit testing frameworks were designed to quickly execute and evaluate test suites and to make
it easy to incrementally add test cases. One of the most popular testing frameworks is JUnit.
1) Create Test
Page 13 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
By default, netbeans will create testSomeMethod() for the test method as below =
Page 14 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
public class CashRegisterTest {
public CashRegisterTest() {
}
@Test
public void testSomeMethod() {
}
You may adjust and create the test methods. Make sure that each test method contains Test
annotator and the method’s name starts with test. For example we can create two test
methods =
register.recordPurchase(0.75);
register.recordPurchase(1.50);
register.receivePayment(2, 0, 5, 0, 0);
register.recordPurchase(2.25);
register.recordPurchase(19.25);
register.receivePayment(21, 2, 0, 0, 0);
Page 15 of 16
ISYS6633003 - Programming Mastery Prepared By:
Binus International University - Exercise Handout 2021 Even Andreas Kurniawan, D6034
4) Run Test
To run the test, we can click the class name and select Test File.
After running the test, we can immediately get the result of the test.
Page 16 of 16