Question

I need help Writing a test java program that creates objects of Account, SavingsAccount, and CheckingAccount...

I need help Writing a test java program that creates objects of Account, SavingsAccount, and CheckingAccount and invokes their toString() methods.

Account.java

TestBankAccount.java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File name :Account.java

class Account{//base class definition
   //class variables declaration
   private static int nextAvailableAcctNumber=0;//variable common for all the objects
   double bal;
   int number;
   //constructors definition
   Account(double bal){
       nextAvailableAcctNumber++;
       number=nextAvailableAcctNumber;
       this.bal=bal;  

   }
   Account(){
       this(0.00);
   }
   Account(Account acct){
       this.bal=acct.bal;
       this.number=acct.number;
      
   }
   //method for deposit
   void deposit(double amt){
       if(amt>0)
           bal+=amt;
   }
   //method to override the object class toString method to return the details of the class
   public String toString(){

       return "\nAccount number: "+number+"\nBalance: "+bal;
   }
   //method to compare two objects of Accont type
   public boolean equals(Account thisAccount){
       if(this.number==thisAccount.number)
           return true;
       else
           return false;

   }
   //method to return the balance of the account
   double getBal(){
       return bal;
   }
   //method to return the account number
   int getAcctNumber(){
       return number;
   }
}
File name:SavingsAccount.java

class SavingsAccount extends Account{
   double rate;
   static double DEFAULT_RATE=0.05,DEFAULT_BALANCE=1000.00;
   SavingsAccount(double bal,double rate){
       super(bal);//to call the parent class constructor
       this.rate=rate;
  
   }
   SavingsAccount(double bal){
       super(bal);
       rate=DEFAULT_RATE;  
   }
   SavingsAccount(){
       super(DEFAULT_BALANCE);
       rate=DEFAULT_RATE;      

   }
   SavingsAccount(SavingsAccount acct){
       super(acct);
       rate=acct.rate;
      
   }
  
   double compound(){
       return bal+=bal*rate;  
   }
   double withd(double amt){
       if(amt<bal)
           bal-=amt;
       else
           amt=0;
       return amt;
      
   }
   double getRate(){
       return rate;
   }
   public String toString(){
       return "\n"+super.toString()+"\nRate: "+rate;

   }
}
File Name :CheckingAccount.java

class CheckingAccount extends Account{
   double limit,charge;
   static double DEFAULT_LIMIT=1000.00,DEFAULT_CHARGE=2.00;
   CheckingAccount(double bal,double limit,double charge){
       super(bal);
       this.limit=limit;
       this.charge=charge;
   }
   CheckingAccount(double bal){
       super(bal);
       limit=DEFAULT_LIMIT;
       charge=DEFAULT_CHARGE;

   }
   CheckingAccount(){
       super(1000.00);
       limit=DEFAULT_LIMIT;
       charge=DEFAULT_CHARGE;

   }
   CheckingAccount(CheckingAccount acct){
       super(acct);
       limit=acct.limit;
       charge=acct.charge;

   }                  
  
   //methods
   double withd(double amt){
       if(amt<bal)
           bal-=amt;
       else
           amt=0;
       return amt;

   }
   public String toString(){
       return "\n"+super.toString()+"\nLimit: "+limit+"\nCharge: "+charge;

   }
   double getLimit(){
       return limit;
   }
   double getCharge(){
       return charge;
   }

}
File name :TestBankAccount.java

//driver class to test other classes
class TestBankAccount{
   public static void main(String args[]){
       //SavingAccount class checking
       SavingsAccount sa1=new SavingsAccount(2000,8);
       SavingsAccount sa2=new SavingsAccount(3000);
       SavingsAccount sa3=new SavingsAccount();
       SavingsAccount sa4=new SavingsAccount(sa3);
      
       System.out.println(sa1.toString());
       System.out.println(sa2.toString());
       System.out.println(sa3.toString());
       System.out.println(sa4.toString());
       System.out.println("\nBalance :"+sa2.getBal());
       System.out.println("Account Number :"+sa2.getAcctNumber());
       System.out.println("getRate() :"+sa2.getRate());
       System.out.println("Amount withdrawl :"+sa2.withd(200));
       System.out.println("Afer withdrawl :"+sa2.getBal());
       System.out.println("Test for equals() :"+sa3.equals(sa4));
       System.out.println("After compounding :"+sa3.compound());


       //ChekingAccount class verification
  
       CheckingAccount ca1=new CheckingAccount(2000,500.00,5.00);
       CheckingAccount ca2=new CheckingAccount(3000);
       CheckingAccount ca3=new CheckingAccount();
       CheckingAccount ca4=new CheckingAccount(ca3);
      
       System.out.println(ca1.toString());
       System.out.println(ca2.toString());
       System.out.println(ca3.toString());
       System.out.println(ca4.toString());
       System.out.println("\nBalance :"+ca2.getBal());
       System.out.println("Account Number :"+ca2.getAcctNumber());
       //System.out.println("getRate() :"+ca2.getRate());
       System.out.println("Amount withdrawl :"+ca2.withd(200));
       System.out.println("Afer withdrawl :"+ca2.getBal());
       System.out.println("Test for equals() :"+ca3.equals(ca4));
       System.out.println("Balance before deposit :"+ca3.getBal());
       ca3.deposit(5000);
       System.out.println("Balance after deposit of 5000:"+ca3.getBal());
   }
}

Output:

Explanation:

the above program includes not only the toString method it also includes different constructors, different methods

Add a comment
Know the answer?
Add Answer to:
I need help Writing a test java program that creates objects of Account, SavingsAccount, and CheckingAccount...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Bank Accounts Look at the Account class Account.java and write a main method in a different...

    Bank Accounts Look at the Account class Account.java and write a main method in a different class to briefly experiment with some instances of the Account class. • Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the attributes of an...

  • According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes...

    According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes Account - number: int - openDate: String - name: String - balance: double + Account(int, String, String, double) + deposit(double): void + withdraw (double): boolean + transferTo(Account, double): int + toString(): String CheckingAccount + CheckingAccount(int, String, String, double) + transferTo(Account, double): int Given the above UML diagam, define Account and CheckingAccount classes as follow: Account (8 points) • public Account(int nu, String op, String...

  • 11.3 (Subclasses of Account) In Programming Exercise 9.7, the Account class was defined to model a...

    11.3 (Subclasses of Account) In Programming Exercise 9.7, the Account class was defined to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and with- draw funds. Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Draw the UML diagram for the classes and implement them. Write a test program that creates objects of...

  • I need help wrting a java program that creates an array with 20 randomly chosen integers....

    I need help wrting a java program that creates an array with 20 randomly chosen integers. The program should prompt the user for an index of the array and display the corresponding element value. If the specified index is out of bounds,display the message "Out of Bounds".

  • I NEED THIS IN SIMPLE JAVA NOTHING TOO COMPLEX. PLEASE WITH COMMENTS Try writing a Blueprint/data...

    I NEED THIS IN SIMPLE JAVA NOTHING TOO COMPLEX. PLEASE WITH COMMENTS Try writing a Blueprint/data class of your own that groups together related information of interest to you (e.g. a Movie has a title, director, lead actor/actress, year, etc. or a BaseballHitter has the following attributes: atBats, hits, runs, homeruns... and methods computeBattingAverage and printStats; the batting average would be computed as hits divided by atBats) and a Driver or main class that uses it (i.e. creates objects and...

  • Hi I need help creating a program in JAVA. This is material from chapter 9 Objects...

    Hi I need help creating a program in JAVA. This is material from chapter 9 Objects and Classes from the Intro to JAVA textbook. Mrs. Quackenbush's Rent-A-Wreck car rental ..... Mrs. Q asked you to create a program that can help her track her growing fleet of junkers. Of course she expects you to use your best programming skills, including: 1. Plan and design what attributes/actions an automobile in a rental fleet should have (example. Track number of Trucks, SUVs,...

  • need help writing a translator program in java. the program needs to be able to preform...

    need help writing a translator program in java. the program needs to be able to preform basic math calculations of addition,subtraction,division,multiplication. I also need to create my own syntax for them so for example if a user would be prompted to enter the syntax for addition they could type in ":/a 2 3" and the output would be 5 subtraction "//s 5 3 and the ouput would be 2, division "!!d 6 3" the output would be 2, multiplication "**m...

  • Using Java, design a program that creates an array of Card objects, shuffles them, then recursively...

    Using Java, design a program that creates an array of Card objects, shuffles them, then recursively sorts them by value and then recursively sorts them by value and suit

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT