Programming 2
Tutorial 2
Exercise 1 (required)
Create a class named Product with three attributes: name, price, and
discount. The class should also include two methods:
• Calculate import tax (10% of the product price)
• Display information on the screen
The information displayed on the screen should include:
✓ Product name
✓ Unit price
✓ Discount
✓ Import tax
Product
+ name: String
+ price: double
+ discount: double
+ getImportTax():double
+ displayInfo(): void
+ input(): void
Exercise 2 (required)
Write a program to create 2 products, with information entered from the
keyboard, and then call the display method to output the information of the 2
created Product objects.
Create a class containing the main() method:
In the main() method, create 2 objects, product1 and product2, from the
Product class.
Call the input() method of the product1 and product2 objects to input data from
the keyboard.
Call the display() method of the product1 and product2 objects to output the
information of each object to the screen.
Exercise 3
Upgrade the Product class by adding public access modifiers for the
display() method and private for the getIncomeTax() method.
Additionally, add two constructors. The first constructor takes three parameters:
name, price, and discount. The second constructor takes two parameters:
name and price (implicitly assuming no discount).
Write a program to create two products, one with a discount and one without, then
display the information of the two products on the screen.
Exercise 4
Extend the below BankAccount example:
public class BankAccount {
double balance;
int transactions;
public BankAccount(double initial) {
[Link] = initial;
[Link] = 1;
}
public void deposit(double amount) {
balance += amount;
transactions++;
}
public void withdraw(double amount) {
balance -= amount;
transactions++;
}
public void monthlyFee() {
[Link](10);
}
}
• Add a method named annualInterest to add annual interest to the account’s balance.
(*) Hint: You should add an attribute called interestRate for this feature and modify
the constructor to initialize this attribute.
• A bank account should have the name of the account holder. Implement this feature.
(*) Hint: You should add an attribute called holderName for this feature and modify
the constructor to initialize this attribute.
• Add a method called toString() which returns a string of this format:
(if the balance is negative, put the – sign before the dollar sign)
Benson, $117.25
Mathew, -$17.50
• Implement a method called transfer to send money from one bank account to another.
There is a $0.5 fee per transfer. Also, if the account does not have enough money to
transfer and pay the fee, print out suitable error messages.
• Write a program called BankAccountDemo to test/demonstrate this BankAccount class.
Deliverable
[Link]
[Link]
Submission
For this tutorial, you are required to submit all your Java programs as a single .zip file. Please
follow the guidelines below to ensure your submission is well-organized and easy to review:
1. Package Structure:
Create a separate package for each exercise. For example, if you have exercises
named "Counting Monetary Units" and "Financial Compound Value", they are
all related to financial, so just create a package named financial.
Place all the Java packages and files related to each exercise inside the
corresponding package. Here is an example:
2. Zipping Your Work:
Once you have organized your code into packages, compress the entire project
folder into a .zip file.
The name of the zip file should follow the format:
S2026_PR2TT02_StudentID_StudentName_TutorialWeekNo.zip (e.g.,
S2026_PR2TT02_123456789_JohnDoe_TutorialWeek01.zip).
3. Submission:
Upload the .zip file to this tutorial’s submission box on the course website via the FIT Portal.