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 program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
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...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
JAVA Design a class named Person and its two derived classes named Student and Employee. Make...
JAVA Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a...
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 Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account 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 Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
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...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT