QuestionQuestion

Fundamentals of Computer Science
Learning Objectives:
• Exploring BlueJ IDE with Java SE 8 (JDK)
• Inheritance
Purpose: In this assignment, we will keep track of a checking account in a bank that is operated in US or CN (Canadian) dollars. Further, the deposits may be in US or CN dollars. Obviously we will have to convert the CN dollar into US dollar and vice versa before crediting the account. For simplicity, we will use the conversion factor:
                      CN $1.00 = US $0.79
That is, 100 Canadian cents equals 79 US cents. In reverse,
US $1.00 = CN $1.26
That is, 100 US cents equals 126 Canadian cents. We will define a class Money and then extend it to a class NAMoney (North American Money) for this assignment.

Specification of Money class
a. It has two instance variables, dollars and cents, both of type int. (Make them protected, not private.)
b. It has a constructor that accepts two parameters, dollars and cents, to initialize the instance variables. If the parameter value passed for cents is more than 100, the constructor, will ensure that the dollar amount is suitably adjusted to keep the cents below 100. That is, if we attempt to construct a Money object with 15 dollars and 325 cents, it leads to a Money object with 18 dollars and 25 cents. (Use division and mod operators suitably.) The constructor does not allow for negative values with suitable assert statement.
c. It has two getter methods, getDollars and getCents.

Specification of the NAMoney class
a. NAMoney extends Money.
b. In addition to the two inherited instance variables, it has one more. The third instance variable is named kind and it is of String type (to keep track of “US” or “CN”).
c. The constructor obviously accepts three parameters – dollars, cents, and kind.   Use the super to inherit the two instance variables dollars and cents, and initialize them suitably. Initialize kind using the parameter provided.
d. It has one getter method, getKind method.
e. It also has a toString method. The toString method returns a String object of the form "US$ 10.02" if dollars = 10, cents = 2, and kind = "US". Observe that for Canadian dollar, the first three characters will be "CN$". Also observe that 2 cents should show as "02".
f. Finally, it has the addMoney method. The method is also invoked on an NAMoney object. It accepts one parameter of NAMoney type. Both are guaranteed to be of the same kind ("US" or "CN"). It adds the money value represented by the object on which it is invoked with that of the parameter passed. It creates a new NAMoney object corresponding to the total dollars and cents and returns a reference to the newly created NAMoney object.

For example, let amount1, amount2, and amount3 be three NAMoney objects. To add amount1 and amount2 and place the result in amount3, the addMoney method is invoked as follows:
amount3 = amount1.addMoney (amount2);
Thus, the method is invoked on amount1 object, passing amount2 as a parameter. Inside the addMoney method, the dollars and cents associated with amount1 are accessed as this.dollars, and this.cents. The dollars and cents associated with amount2 are obtained using the getter methods.
1. Create a BlueJ project named LAB10 as follows:
a. Open the P drive and create a folder named BlueJ, if it does not exist already. Under this folder, create another subfolder named CSC203 for this course, if it does not exist already.
b. Launch BlueJ and create a BlueJ project named LAB10 under the CSC203 folder mentioned in the previous step.
2. Create a class named Money. Choose Options and Preferences and then check Display Line Numbers. Also, choose the options Class and Page Setup... and set the Orientation to Landscape. Keep the class outline and start editing.
a. Declare the instance variables, and write the code for the constructor and the two getter methods.(Remember that the instance variables have the qualifier protected.)
b. Test the class by interactively creating a new Money Object. Supply 15 and 325 as the two parameters and inspect the created Money object. Ensure that it shows 18 dollars and 25 cents.
3. Create a class named NAMoney. Keep the class outline and start editing.
a. Declare the instance variable, and write the code for the constructor, the getter method and the toString method.
b. Write the code for the addMoney method. Declare three variables – totalDollars, totoalCents and totolKind. Compute and assign values for these appropriately. Create and return a new NAMoney with the totalDollars, totalCents and totalKind.
c. Test the class by interactively creating two objects, say amount1 and amount2 corresponding to US$ 10.52 and US$ 4.56, respectively. Invoke the addMoney method on amount1 with amount2 as the parameter. Inspect the retuned object and verify that it corresponds to US$ 15.08. Also check if the toString method works as expected.
4. Create a class named CurrencyConverter. Open the editor and delete all the code placed in it automatically by BlueJ. Use a web browser, access the class site on Blackboard, locate the file named CurrencyConverter.java, open it, copy and then paste it as code for the class CurrencyConverter. Replace my name with that of yours and change the date suitably. Make sure to delete blank lines on top and bottom. The code for CurrencyConverter class is complete. There is no need to change it in any way.
5. Create a class named CheckingAccount. Open the editor and delete all the code placed in it automatically by BlueJ. Use a web browser, access the class site on Blackboard, locate the file named CheckingAccount.java, open it, copy and then paste it as code for the class CheckingAccount. Replace my name with that of yours and change the date suitably. Make sure to delete blank lines on top and bottom. The code for CheckingAccount class is incomplete. Write appropriate lines of code as indicated by the comment lines.
6. Finally, test the program.
7. After you are sure the program works fine and the output is what you need, clear the terminal window.
8. Finally,
a. In the Project Window showing the classes, rearrange the classes so that their relationship is seen clearly. Print the project window showing the four classes and their relationship.
b. In the project window, select the option Project and then select Page Setup. Choose Landscape. Print to an ADOBE pdf printer, in landscape orientation, and save the files under P:\BlueJ\CSC203\LAB10:
• The source code for CheckingAccount as CheckingAccount.pdf
• The source code for CurrencyConvereter as CurrencyConvereter.pdf
• The source code for NAMoney as NAMoney.pdf
• The source code for Money as Money.pdf
c. Print two-sided the four files created – CheckingAccount.pdf, CurrencyConverter.pdf, NAMoney.pdf, and Money.pdf.
d. Print two-sided CheckingAccountOutput.pdf.

Worksheet for Lab 10
The following code will be used in the Money Class
1. Each Money object has two instance variables. Write code to declare these two instance variables:
a. dollars, an int
b. cents, an int
Use the visibility modifier protected rather than private.
2. Write the constructor for the Money class. The constructor has two parameters, corresponding to the two instances variables with the same name. Both parameters are required to be >= 0. But cents may be more than 100. If so, adjust dollars suitably. For example, 15 dollars 325 cents is adjusted to 18 dollars 25 cents. (Use % and / as needed.) Include appropriate assert statement before initialization of the instance variables.
3. Write the two getter methods for the Money class.  
The following code will be used in the NAMoney Class
4. Include a new instance variable kind of String type. Make the visibility modifier private.   
5. Write the constructor for the NAMoney class that extends Money. The constructor has three parameters, corresponding to the three instances variables (two inherited and one new) with the same name. dollars and cents are required to be >= 0. The parameter kind can only be "US" or "CN". Also, cents may be more than 100. If so, adjust dollars suitably. For example, 15 dollars 325 cents is adjusted to 18 dollars 25 cents. (Use % and / as needed.)   Include appropriate assert statement before initialization of the three instance variables.
6. Write a getter method corresponding to the new instance variable.
7. The toString method returns a String object of the form "US$ 10.02" if dollars = 10, cents = 2, and kind = "US". Observe that for Canadian dollar, the first three characters will be "CN$". Also observe that 2 cents should show as "02". Write the code for the toString method.
8. Finally, develop the code for the addMoney method. The method is invoked on an NAMoney object. It accepts one parameter of NAMoney type. Both are guaranteed to be of the same kind ("US" or "CN"). It adds the money value represented by the object on which it is invoked with that of the parameter passed. It creates a new NAMoney object corresponding to the total dollars and cents and returns a reference to the newly created NAMoney object.
For example, let amount1, amount2, and amount3 be three NAMoney objects. To add amount1 and amount2 and place the result in amount3, the addMoney method is invoked as follows:
amount3 = amount1.addMoney (amount2);
The following code will be used in the CheckingAccount Class
9. Create a zero balance (0 dollars and 0 cents) NAMoney object named balance of the kind given by accountkind
10. Given the scanner keyboard, read values by the user for three variables: depositKind, dollars, and cents. (Prompting has been done already.)
11. Create an NAMoney object named deposit from the three values read: depositKind, dollars, and cents.
12. The CurrencyConverter class has a static method named convertNAMoney. The method accepts two parameters: an NAMoney object and a String representing the kind, so the amount represented by the parameter NAMoney object can be converted to the appropriate value, taking into account the second parameter. The method returns an NAMoney object which represents the converted value. Invoke the convertNAMoney method to convert deposit to accountKind and assign the returned value to convertedDeposit.
13. Add convertedDeposit to balance.
14. Print balance with the leading caption Final balance is

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/**
* Used for tracking deposits into an account.
*
*/
import java.util.*;
public class CheckingAccount
{
    public static void main(String[] args)
    {
       String accountKind; String depositKind; // US or CN dollar for account and deposit
       int dollars, cents;
       NAMoney balance, deposit, convertedDeposit;
       Scanner keyboard = new Scanner (System.in); // to read user input

       // display purpose
       System.out.println ("This program tracks deposits in "
            + "dollars and cents,");
       System.out.println ("in US or CN currencies. Invalid "
            + "currency value");
       System.out.println ("leads to termination of program.");
       System.out.println (); // print blank line
       System.out.println ("Author: Kad Lakshmanan\tVersion: April 11, 2018");
       System.out.println (); // print blank line

       // Obtain account type -- either US or CN
       System.out.print ("Enter US or CN to indicate"
            + " type of account maintained: ");   
       accountKind = keyboard.next();
       while (!accountKind.equals("US") && !accountKind.equals("CN")) {
            System.out.print ("Enter US or CN to indicate"
                + " type of account maintained: ");   
            accountKind = keyboard.next();
       }

       // create zero balance object of account kind and print
       balance = new NAMoney(0, 0,accountKind);
       System.out.println ("Initial balance is " + balance.toString());
       System.out.println (); // print blank line

       // Get deposit amount
       System.out.print ("Enter currency kind of deposit, dollars, "
            + "and cents: ");
       depositKind = keyboard.next();
       dollars = keyboard.nextInt();
       cents...

By purchasing this solution you'll be able to access the following files:
Solution.docx, Solution.zip and Solution.pdf.

50% discount

Hours
Minutes
Seconds
$35.00 $17.50
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats