Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part one: Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives Use methods of the Character class and String class to process text Be

Part one:

Chapter 9 Lab

Text Processing and Wrapper Classes

Lab Objectives

Use methods of the Character class and String class to process text

Be able to use the StringTokenizer and StringBuffer classes

Introduction

In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will be printed to the console if the entry is not a valid military time.

Think about how you would convert any military time 00:00 to 23:59 into conventional time. Also think about what would be valid military times. To be a valid time, the data must have a specific form. First, it should have exactly 5 characters. Next, only digits are allowed in the first two and last two positions, and that a colon is always used in the middle position. Next, we need to ensure that we never have over 23 hours or 59 minutes. This will require us to separate the substrings containing the hours and minutes. When converting from military time to conventional time, we only have to worry about times that have hours greater than 12, and we do not need to do anything with the minutes at all. To convert, we will need to subtract 12, and put it back together with the colon and the minutes, and indicate that it is PM. Keep in mind that 00:00 in military time is 12:00 AM (midnight) and 12:00 in military time is 12:00 PM (noon).

Task #1 Character and String Class Methods

Copy the files Time.java and TimeDemo.java into NetBeans or other IDE tools. Note, the java files may not compile before you finish the coding.

In the Time.java file, add conditions to the decision structure which validates the data. Conditions are needed that will

a. Check the length of the string

b. Check the position of the colon

c. Check that all other characters are digits

Add lines that will separate the string into two substrings containing hours and minutes. Convert these substrings to integers and save them into the instance variables.

In the TimeDemo class, add a condition to the loop that converts the users answer to a capital letter prior to checking it.

Compile, debug, and run. Test out your program using the following valid input:

00:00, 12:00, 04:05, 10:15, 23:59, 00:35, and the following invalid input:

7:56, 15:78, 08:60, 24:00, 3e:33, 1:111..

Time.java:

/**Represents time in hours and minutes using the customary conventions*/ public class Time { /**hours in conventional time*/ private int hours; /**minutes in conventional time*/ private int minutes; /**true if afternoon time, false if morning time*/ private boolean afternoon;

/**Constructs a cutomary time (12 hours, am or pm) from a military time ##:## @param militaryTime time in the military format ##:##*/ public Time(String militaryTime) { //Check to make sure something was entered if (militaryTime == null) { System.out.println( "You must enter a valid miliary time." ); } //Check to make sure there are 5 characters // To do - Task #1 step 2 item a else if (//CONDITION TO CHECK LENGTH OF STRING) { System.out.println(militaryTime + " is not a valid miliary time." ); } else { //Check to make sure the colon is in //the correct spot // To do - Task #1 step 2 item b if (//CONDITION TO CHECK COLON POSITION) { System.out.println(militaryTime + " is not a valid miliary time." ); } //Check to make sure all other characters are digits // To do - Task #1 step 2 item c else if (!Character.isDigit(militaryTime.charAt(0))) { System.out.println(militaryTime + " is not a valid miliary time." ); } else if (//CONDITION TO CHECK FOR DIGIT) { System.out.println(militaryTime + " is not a valid miliary time." ); } else if (//CONDITION TO CHECK FOR DIGIT) { System.out.println(militaryTime + " is not a valid miliary time." ); } else if (//CONDITION TO CHECK FOR DIGIT) { System.out.println(militaryTime + " is not a valid miliary time." ); } else { //SEPARATE THE STRING INTO THE HOURS //AND THE MINUTES, CONVERTING THEM TO //INTEGERS AND STORING INTO THE //INSTANCE VARIABLES // To do - finish Task #1 step 3 - get minutes hours = Integer.parseInt(militaryTime.substring(0,2)); //validate hours and minutes are valid values if(hours > 23) { System.out.println(militaryTime + " is not a valid miliary time." ); } else if(minutes > 59) { System.out.println(militaryTime + " is not a valid miliary time." ); } //convert military time to conventional time //for afternoon times else if (hours > 12) { hours = hours - 12; afternoon = true; System.out.println(this.toString()); } //account for midnight else if (hours == 0) { hours = 12; System.out.println(this.toString()); } //account for noon else if (hours == 12) { afternoon = true; System.out.println(this.toString()); } //morning times don't need converting else { System.out.println(this.toString()); } } } }

/**toString method returns a conventional time @return a conventional time with am or pm*/ public String toString() { String am_pm; String zero = ""; if (afternoon) am_pm = "PM"; else am_pm = "AM"; if (minutes

return hours + ":" + zero + minutes + " " + am_pm; } }

TimeDemo:

import java.util.Scanner;

public class TimeDemo { public static void main (String [] args) { Scanner keyboard = new Scanner(System.in); char answer = 'Y'; String enteredTime; String response; // To do - Task #1 step 4 while (//CHECK ANSWER AFTER CONVERTING TO CAPITAL) { System.out.print( "Enter a miitary time using the ##:## form "); enteredTime = keyboard.nextLine(); Time now = new Time (enteredTime); System.out.println( "Do you want to enter another (Y/N)? "); response = keyboard.nextLine(); answer = response.charAt(0); } } }

-------------------------------------------------------------

Part Two:

Chapter 10 Lab Inheritance Objectives Be able to derive a class from an existing class Be able to define a class hierarchy in which methods are overridden and fields are hidden Be able to use derived-class objects Implement a copy constructor Introduction In this lab, you will be creating new classes that are derived from a class called BankAccount. A checking account is a bank account and a savings account is a bank account as well. This sets up a relationship called inheritance, where BankAccount is the superclass and CheckingAccount and SavingsAccount are subclasses. This relationship allows CheckingAccount to inherit attributes from BankAccount, like owner, balance, and accountNumber, but it can have new attributes that are specific to a checking account, like a fee for clearing a check. It also allows CheckingAccount to inherit methods from BankAccount, like deposit, that are universal for all bank accounts. You will write a withdraw method in CheckingAccount that overrides the withdraw method in BankAccount, in order to do something slightly different than the original withdraw method. You will use an instance variable called accountNumber in SavingsAccount to hide the accountNumber variable inherited from BankAccount.

The UML diagram for the inheritance relationship is as follows:

image text in transcribed Task #1 Extending BankAccount 1. Copy the files AccountDriver.java and BankAccount.java into NetBeans or other IDE tools. BankAccount.java is complete and will not need to be modified. 2. Create a new class called CheckingAccount that extends BankAccount. The outline of this class is provided in CheckingAccoutn.java. You can copy it to NetBeans and fill in the details. 3. It should contain a static constant FEE that represents the cost of clearing one check. Set it equal to 15 cents. 4. Write a constructor that takes a name and an initial amount as parameters. It should call the constructor for the superclass. It should initialize accountNumber to be the current value in accountNumber concatenated with 10 (All checking accounts at this bank are identified by the extension 10). There can be only one checking account for each account number. Remember since accountNumber is a private member in BankAccount, it must be changed through a mutator method. 5. Write a new instance method, withdraw, that overrides the withdraw method in the superclass. This method should take the amount to withdraw, add to it the fee for check clearing, and call the withdraw method from the superclass. Remember that to override the method, it must have the same method heading. Notice that the withdraw method from the superclass returns true or false depending if it was able to complete the withdrawal or not. The method that overrides it must also return the same true or false that was returned from the call to the withdraw method from the superclass. 6. Compile and debug this class. Task #2 Creating a Second Subclass 1. Create a new class called SavingsAccount that extends BankAccount. See the outline of SavintsAccount.java provided. 2. It should have an instance variable called savingsNumber, initialized to 0. In this bank, you have one account number, but can have several savings accounts with that same number. Each individual savings account is identified by the number following a dash. For example, 100001-0 is the first savings account you open, 100001-1 would be another savings account that is still part of your same account. This is so that you can keep some funds separate from the others, like a Christmas club account. 3. An instance variable called accountNumber that will hide the accountNumber from the superclass, should also be in this class. 4. Write a constructor that takes a name and an initial balance as parameters and calls the constructor for the superclass. It should initialize accountNumber to be the current value in the superclass accountNumber (the hidden instance variable) concatenated with a hyphen and then the savingsNumber. 5. Write a method called postInterest that has no parameters and returns no value. This method will calculate one month's worth of interest on the balance and deposit it into the account. 6. Write a method that overrides the getAccountNumber method in the superclass. 7. Write a copy constructor that creates another savings account for the same person. It should take the original savings account and an initial balance as parameters. It should call the copy constructor of the superclass, assign the savingsNumber to be one more than the savingsNumber of the original savings account. It should assign the accountNumber to be the accountNumber of the superclass concatenated with the hypen and the savingsNumber of the new account. 8. Compile and debug this class. 9. Use the AccountDriver class to test out your classes. If you named and created your classes and methods correctly, it should not have any difficulties. If you have errors do not edit the AccountDriver class. You must make your classes work with this program. 10. Running the program should give the following output: Account Number 100001-10 belonging to Benjamin Franklin Initial balance = $1000.00 After deposit of $500.00, balance = $1500.00 After withdrawal of $1000.00, balance = $499.85 Account Number 100002-0 belonging to William Shakespeare Initial balance = $400.00 After deposit of $500.00, balance = $900.00 Insuffient funds to withdraw $1000.00, balance = $900.00 After monthly interest has been posted, balance = $901.88 Account Number 100002-1 belonging to William Shakespeare Initial balance = $5.00 After deposit of $500.00, balance = $505.00 Insuffient funds to withdraw $1000.00, balance = $505.00 Account Number 100003-10 belonging to Isaac Newton

AccountDriver.java:

//Demonstrates the BankAccount and derived classes

import java.text.*; // to use Decimal Format

public class AccountDriver { public static void main(String[] args) { double put_in = 500; double take_out = 1000;

DecimalFormat myFormat; String money; String money_in; String money_out; boolean completed;

// to get 2 decimals every time myFormat = new DecimalFormat("#.00");

//to test the Checking Account class CheckingAccount myCheckingAccount = new CheckingAccount ("Benjamin Franklin", 1000); System.out.println ("Account Number " + myCheckingAccount.getAccountNumber() + " belonging to " + myCheckingAccount.getOwner()); money = myFormat.format(myCheckingAccount.getBalance()); System.out.println ("Initial balance = $" + money); myCheckingAccount.deposit (put_in); money_in = myFormat.format(put_in); money = myFormat.format(myCheckingAccount.getBalance()); System.out.println ("After deposit of $" + money_in + ", balance = $" + money); completed = myCheckingAccount.withdraw(take_out); money_out = myFormat.format(take_out); money = myFormat.format(myCheckingAccount.getBalance()); if (completed) { System.out.println ("After withdrawal of $" + money_out + ", balance = $" + money); } else { System.out.println ("Insuffient funds to withdraw $" + money_out + ", balance = $" + money); } System.out.println();

//to test the savings account class SavingsAccount yourAccount = new SavingsAccount ("William Shakespeare", 400); System.out.println ("Account Number " + yourAccount.getAccountNumber() + " belonging to " + yourAccount.getOwner()); money = myFormat.format(yourAccount.getBalance()); System.out.println ("Initial balance = $" + money); yourAccount.deposit (put_in); money_in = myFormat.format(put_in); money = myFormat.format(yourAccount.getBalance()); System.out.println ("After deposit of $" + money_in + ", balance = $" + money); completed = yourAccount.withdraw(take_out); money_out = myFormat.format(take_out); money = myFormat.format(yourAccount.getBalance()); if (completed) { System.out.println ("After withdrawal of $" + money_out + ", balance = $" + money); } else { System.out.println ("Insuffient funds to withdraw $" + money_out + ", balance = $" + money); } yourAccount.postInterest(); money = myFormat.format(yourAccount.getBalance()); System.out.println ("After monthly interest has been posted," + "balance = $" + money); System.out.println();

// to test the copy constructor of the savings account class SavingsAccount secondAccount = new SavingsAccount (yourAccount,5); System.out.println ("Account Number " + secondAccount.getAccountNumber()+ " belonging to " + secondAccount.getOwner()); money = myFormat.format(secondAccount.getBalance()); System.out.println ("Initial balance = $" + money); secondAccount.deposit (put_in); money_in = myFormat.format(put_in); money = myFormat.format(secondAccount.getBalance()); System.out.println ("After deposit of $" + money_in + ", balance = $" + money); secondAccount.withdraw(take_out); money_out = myFormat.format(take_out); money = myFormat.format(secondAccount.getBalance()); if (completed) { System.out.println ("After withdrawal of $" + money_out + ", balance = $" + money); } else { System.out.println ("Insuffient funds to withdraw $" + money_out + ", balance = $" + money); } System.out.println();

//to test to make sure new accounts are numbered correctly CheckingAccount yourCheckingAccount = new CheckingAccount ("Issac Newton", 5000); System.out.println ("Account Number " + yourCheckingAccount.getAccountNumber() + " belonging to " + yourCheckingAccount.getOwner());

} }

BankAccount.java:

/**Defines any type of bank account*/ public abstract class BankAccount { /**class variable so that each account has a unique number*/ protected static int numberOfAccounts = 100001;

/**current balance in the account*/ private double balance; /** name on the account*/ private String owner; /** number bank uses to identify account*/ private String accountNumber;

/**default constructor*/ public BankAccount() { balance = 0; accountNumber = numberOfAccounts + ""; numberOfAccounts++; }

/**standard constructor @param name the owner of the account @param amount the beginning balance*/ public BankAccount(String name, double amount) { owner = name; balance = amount; accountNumber = numberOfAccounts + ""; numberOfAccounts++; }

/**copy constructor creates another account for the same owner @param oldAccount the account with information to copy @param the beginning balance of the new account*/ public BankAccount(BankAccount oldAccount, double amount) { owner = oldAccount.owner; balance = amount; accountNumber = oldAccount.accountNumber; }

/**allows you to add money to the account @param amount the amount to deposit in the account*/ public void deposit(double amount) { balance = balance + amount; }

/**allows you to remove money from the account if enough money is available,returns true if the transaction was completed, returns false if the there was not enough money. @param amount the amount to withdraw from the account @return true if there was sufficient funds to complete the transaction, false otherwise*/ public boolean withdraw(double amount) { boolean completed = true;

if (amount

/**accessor method to balance @return the balance of the account*/ public double getBalance() { return balance; }

/**accessor method to owner @return the owner of the account*/ public String getOwner() { return owner; }

/**accessor method to account number @return the account number*/ public String getAccountNumber() { return accountNumber; }

/**mutator method to change the balance @param newBalance the new balance for the account*/ public void setBalance(double newBalance) { balance = newBalance; }

/**mutator method to change the account number @param newAccountNumber the new account number*/ public void setAccountNumber(String newAccountNumber) { accountNumber = newAccountNumber; } }

CheckingAccount.java:

public class CheckingAccount extends BankAccount { // To do - Task #1 step 3 // define a private static doubl field // named FEE and set the initial value as 15 cents public CheckingAccount(String name, double begBal) { super(name, begBal); // To do - Task #1 step 4 // using the inherited method // getAccountNumber, then concatenated with 10 // then using the inherited method // setAccountNumber } public boolean withdraw (double amount) // overide the method { // To do - Task #1 step 5 // add FEE to the amount then call // super.withdraw(amount); // and return the result } }

SavingsAccount.java:

public class SavingsAccount extends BankAccount { private double rate = .025; //annual rate // To do - Task #2 step 2, 3 // define integer savingsNumber and initiate it as 0 // define String accountNumber public SavingsAccount(String name, double begBal) { // To do - Finish Task #2 step 4 to call super class constructor // with name and begBal as parameter accountNumber = super.getAccountNumber() + "-" + savingsNumber; } public SavingsAccount(SavingsAccount oldAccount, double begBal) { // To do - Finish Task #2 step 7 super(oldAccount, begBal); // add 1 to oldAccount.savingsNumber // also assign accountNumber, reference to Task#2 step 4 } public void postInterest( ) { // To do - Finish Task #2 step 5 // use getBalance to get current balance // add monthly interest (rate/12) then use setBalance to set // the new balance into the account. } // To do - Finish Task #2 step 6 }

Bank Account -balance double -owner: String accountNumber String numberOf Accounts int Bank Account Bank Account name :String, amount double): BankAccount (oldAccount BankAccount,amount double) deposit (amount double): void withdraw(amount double) boolean get Balance(): double getowner(): String get AccountNumber ():String setBalance (amount: double): void +setAccountNumber (newAccountNumber :String) void CheckingAccount Savings Account FEE:double rate double savingsNumber: int CheckingAccount (name: String, accountNumber :String amount double) withdraw (amount:double) :boolean Savings Account (name: String, amount:double): +Savings Account (oldAccount:SavingsAccount, amount double): postinterest :void +getAccountNumber ():String

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Prepare a short profile of Lucy Clifford ?

Answered: 1 week ago

Question

Prepare a short profile of Rosa parks?

Answered: 1 week ago

Question

Prepare a short profile of victor marie hugo ?

Answered: 1 week ago

Question

Discuss the key people management challenges that Dorian faced.

Answered: 1 week ago

Question

How fast should bidder managers move into the target?

Answered: 1 week ago