Question: Create a class called TestCashRegister (JAVA). The requirements for TestCashRegister class is as given below. Create a main method and write the code for main
Create a class called TestCashRegister (JAVA). The requirements for TestCashRegister class is as given below. Create a main method and write the code for main method as given below:
Create a CashRegister object called cashReg1, using the default constructor .
Prompt the user to provide a value for amount and scan this into a variable called amount.
Call the recordPayment method for cashReg1 and to this method, pass on the value of amount, (obtained from user).
Call the getter for cashReg1 to obtain the value of payment. Print this value of payment on the output.
Call the printBalance method for cashReg1 .
Prompt the user to provide a new value for purchase amount and scan this into the variable called amount.
Call the recordPurchase method for cashReg1 and to this method, pass on the value of amount.
Call printBalance method for cashReg1 to display the values purchase and payment.
Create a new object called cashReg2 using the overloaded constructor.
Call the printBalance method for cashReg2.
Prompt the user to provide a new value of payment amount and scan this into the variable called amount.
Call the recordPayment method for cashReg2 and pass on the value of amount.
Call printBalance method for cashReg2 to print out the balance.
Prompt the user to provide a new value of purchase amount and scan this into the variable called amount.
Call the recordPurchase method for cashReg2 and pass on the value of amount.
Call printBalance method for cashReg2
cashRegister.java:
import java.util.*;
class CashRegister { double payment,purchase,balance;
CashRegister() { payment=0; purchase=0; }
CashRegister(double payment1,double purchase1) { payment=payment1; purchase=purchase1; }
void recordPayment(double amount) { payment=amount; }
void recordPurchase(double amount) { purchase=amount; }
void printBalance() { balance=Math.round(payment-purchase); System.out.println("Value of payment : "+payment); System.out.println("Value of purchase : "+purchase); System.out.println("Balance : "+balance); } }
class TestCashRegister { public static void main(String args[]) { double amount; Scanner sc=new Scanner(System.in); CashRegister cashReg1=new CashRegister(); System.out.print(" Enter amount of payment : "); amount=sc.nextDouble(); cashReg1.recordPayment(amount); System.out.print("Enter amount of purchase : "); amount=sc.nextDouble(); cashReg1.recordPurchase(amount); cashReg1.printBalance(); System.out.println(" ");
CashRegister cashReg2=new CashRegister(0,0); System.out.print(" Enter amount of payment : "); amount=sc.nextDouble(); cashReg2.recordPayment(amount); System.out.print("Enter amount of purchase : "); amount=sc.nextDouble(); cashReg2.recordPurchase(amount); cashReg2.printBalance(); System.out.println(" "); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
