Question

In: Computer Science

JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...

JAVA

A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed )

1. Add New private String name (customer name) for both, add a New int taxID for Savings only.

2. Add equals() METHOD TO CHECK any 2 accounts

Demonstrate with AccountDemo.java in which you do the following:

3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same. Check 2 equal accounts and 2 different accounts.

4. Withdraw 100 dollars from savings , deposit the same amount to a checking , then display accounts.

import java.util.*;

public class Account

{private int accNum; double balance;

public Account (int accNum, double balance)

{

this.accNum = accNum;

this.balance = balance;

}

public int getAccNum()

{

return accNum;

}

public double getBalance()

{

return balance;

}

public void deposit (double depositAmt)

{

double newBalance = balance + depositAmt;

}

public void withdraw (double withdrawAmt)

{…………….fill here………………….}

public void display()

{

System.out.println();

System.out.println("Account Number :" + accNum );

System.out.println("Balance :" + balance);

}

}

Solutions

Expert Solution

CODE:

package com.company;

import java.io.*;
import java.util.Objects;

class Account {
    private int accNum;
    double balance;
    //constructor
    public Account (int accNum, double balance) {
        this.accNum = accNum;
        this.balance = balance;
    }
    //getters and setters
    public int getAccNum() {
        return accNum;
    }

    public double getBalance() {
        return balance;
    }
    //deposit method
    public void deposit (double depositAmt) {
        double newBalance = balance + depositAmt;
        balance = newBalance;
    }
    //withdraw method
    public void withdraw (double withdrawAmt) {
        //withdrawal Amount will be deducted from the balance only
        //when it is less than equal to the balance
        if(withdrawAmt <= balance)
            balance -= withdrawAmt;
        else{
            System.out.println("Withdraw amount greater than the balance");
        }
    }
    //display method
    public void display() {
        System.out.println();
        System.out.println("Account Number :" + accNum );
        System.out.println("Balance :" + balance);
    }
}

class Savings extends Account{
    private String name;
    private int taxId;
    //constructor
    public Savings(String name,int taxId, int accNum, double balance) {
        super(accNum, balance);
        this.name = name;
        this.taxId = taxId;
    }

    @Override
    public boolean equals(Object o) {
        //if the object is equal to this then return true
        if (this == o)
            return true;
        //if the object is not an instance of Savings
        if (!(o instanceof Savings))
            return false;
        Savings savings = (Savings) o;
        //if all the parameters are same then returns true
        return taxId == savings.taxId && Objects.equals(name, savings.name)
                && getAccNum() == savings.getAccNum() && getBalance() == savings.getBalance();
    }

    @Override
    public void display() {
        super.display();
        System.out.println("Name: "+name);
        System.out.println("TaxId: "+taxId);
    }
}

class Checking extends Account{
    private String name;
    //constructor
    public Checking(String name, int accNum, double balance) {
        super(accNum, balance);
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        //if the object is equal to this then return true
        if (this == obj)
            return true;
        //if the object is not an instance of Checking
        if (!(obj instanceof Checking))
            return false;
        Checking checking = (Checking) obj;
        //if all the parameters are same then returns true
        return Objects.equals(name, checking.name)
                && getAccNum() == checking.getAccNum() && getBalance() == checking.getBalance();
    }
    //display method
    @Override
    public void display() {
        super.display();
        System.out.println("Name: "+name);
    }
}

//main driver class
class Main {
    //main method
    public static void main(String[] args) throws IOException {
        //1 savings account
        Savings savings = new Savings("John",124,1626,1352.7);
        //3 checking account
        Checking checking1 = new Checking("Harry",5156,363.2);
        Checking checking2 = new Checking("Matt",5176,3623.2);
        Checking checking3 = new Checking("Harry",5156,363.2);
        //printing the result of two equals call methods
        System.out.println(checking1.equals(checking3));
        System.out.println(checking1.equals(checking2));
        //withdrawing $100 from savings
        savings.withdraw(100);
        //depositing $100 to checking
        checking2.deposit(100);
        //displaying two accounts
        savings.display();
        checking2.display();
    }
}

___________________________________________

CODE IMAGES:

_______________________________________________

OUTPUT:

________________________________________________

Feel free to ask any questions in the comments section

Thank You!


Related Solutions

JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods: 1) Module1 data fields: a. an object of type Module1Data named m1d b. an object of type Scanner named scanner c. a double named input 2) Module1Data methods: a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
This is in Java 1. Create an application called registrar that has the following classes: a....
This is in Java 1. Create an application called registrar that has the following classes: a. A student class that minimally stores the following data fields for a student:  Name  Student id number  Number of credits  Total grade points earned             And this class should also be provides the following methods:  A constructor that initializes the name and id fields  A method that returns the student name field  A method that returns the student...
Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT