Below are 3 programs i’m working on in JAVA that go together. The first program (Name.java) is compelted, but i need help with the last two (BankAccount.java and BankingDemo.java). Each program has several steps to it, and i need help finishing steps 4 and 15. If you see any errors i might have made on any of the other steps please change them.
public class Name
{
private String lastName;
private String firstName;
/**
This constructor sets the starting balance
at 0.0.
*/
public Name(String n, String f)
{
lastName =n;
firstName =f;
}
//step 1: A copy constructor that copies one name object to another.
public Name(Name n)
{
lastName = n.lastName;
firstName = n.firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String l )
{
lastName=l;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String l )
{
firstName=l;
}
// step 2: equals(): boolean
public boolean equals(Name n)
{
if(lastName.equals(n.lastName) && firstName.equals(n.firstName))
return true;
else
return false;
}
//step3: toString():String
public String toString()
{
String sti = lastName + “, ” + firstName;
return sti;
}
}
public class BankAccount
{
private int accountNumber;
private char type;
private double balance; // Account balance
//step4: declare a private Name object named name.
/**
step 5:write a constructor which accepts four parameters to initialize
name, accountNumber, type, and balance */
public BankAccount(Name n, int acc, char c, double b)
{
name = new Name(n);
number = new accountNumber(acc);
kind = new type(c);
amount = new balance(b);
}
public void deposit(double b)
{
balance += b;
}
public void withdraw(double amount)
{
balance -= amount;
}
public double getBalance()
{
return balance;
}
public void setBalance(double b)
{
balance =b;
}
public void setAccountNumber(int a)
{
accountNumber=a;
}
public int getAccountNumber()
{
return accountNumber;
}
//step 6: write getName():Name
public String getName()
{
return name;
}
//step7: setName(n:Name):void
public void setName(Name n)
{
name = new Name(n);
}
//step8: getType():char
public String getType()
{
return kind;
}
//step 9: setType(char c):void
public void setType(char c)
{
kind = new type(c);
}
//step 10: toString():String
public String toString()
{
String = name + “, ” + accountNumber + “, ” + type + “, ” + balance;
}
}
import java.util.Scanner;
public class BankingDemo
{
public static void main(String[] args)
{
Scanner kb= new Scanner(System.in);
System.out.println(“Enter the first name and last name of the customer 1.”);
String f=kb.nextLine();
String n=kb.nextLine();
//step 11: create a Name object named name1 for the above customer.
Name name1 = new Name(n,f);
System.out.println(“Enter the first name and last name of the customer 2.”);
f=kb.nextLine();
n=kb.nextLine();
//step 12: create a Name object named name2 for the above customer.
Name name2 = new Name(n,f);
//step 13: compare the above two Name objects. If they have the same name,
//print “duplicate names”.
if(name1.equals(name2))
System.out.println(“Duplicate names.”);
//step14: create a BankAccount object for customer 1 with balance $1000.00,
//type ‘c’, and accountNumber “1000001”.
BankAccount acct1 = new BankAccount(name1, 1000001, ‘c’, 1000.0);
//step15: print the above BankAccount object using toString() method.
}
}
Expert Answer
Name.java
public class Name {
private String lastName;
private String firstName;
/**
* This constructor sets the starting balance at 0.0.
*/
public Name(String n, String f) {
lastName = n;
firstName = f;
}
// step 1: A copy constructor that copies one name object to another.
public Name(Name n) {
lastName = n.lastName;
firstName = n.firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String l) {
lastName = l;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String l) {
firstName = l;
}
// step 2: equals(): boolean
public boolean equals(Name n) {
if (lastName.equals(n.lastName) && firstName.equals(n.firstName))
return true;
else
return false;
}
// step3: toString():String
public String toString() {
String sti = lastName + “, ” + firstName;
return sti;
}
}
___________________
BankAccount.java
public class BankAccount {
private int accountNumber;
private char type;
private double balance; // Account balance
private Name name;
// step4: declare a private Name object named name.
/**
* step 5:write a constructor which accepts four parameters to initialize
* name, accountNumber, type, and balance
*/
public BankAccount(Name n, int acc, char c, double b) {
name = n;
accountNumber = acc;
type = c;
balance = b;
}
public void deposit(double b) {
balance += b;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
public void setBalance(double b) {
balance = b;
}
public void setAccountNumber(int a) {
accountNumber = a;
}
public int getAccountNumber() {
return accountNumber;
}
// step 6: write getName():Name
public Name getName() {
return name;
}
// step7: setName(n:Name):void
public void setName(Name n) {
name = new Name(n);
}
// step8: getType():char
public char getType() {
return type;
}
// step 9: setType(char c):void
public void setType(char c) {
this.type = c;
}
// step 10: toString():String
public String toString() {
return name + “, ” + accountNumber + “, ” + type + “, ” + balance;
}
}
________________________
BankingDemo.java
import java.util.Scanner;
public class BankingDemo {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println(“Enter the first name and last name of the customer 1.”);
String f = kb.next();
String n = kb.next();
// step 11: create a Name object named name1 for the above customer.
Name name1 = new Name(n, f);
System.out.println(“Enter the first name and last name of the customer 2.”);
f = kb.next();
n = kb.next();
// step 12: create a Name object named name2 for the above customer.
Name name2 = new Name(n, f);
// step 13: compare the above two Name objects. If they have the same
// name,
// print “duplicate names”.
if (name1.equals(name2))
System.out.println(“Duplicate names.”);
// step14: create a BankAccount object for customer 1 with balance
// $1000.00,
// type ‘c’, and accountNumber “1000001”.
BankAccount acct1 = new BankAccount(name1, 1000001, ‘c’, 1000.0);
// step15: print the above BankAccount object using toString() method.
System.out.println(acct1.toString());
}
}
____________________
Output:
Enter the first name and last name of the customer 1.
Ricky Pointing
Enter the first name and last name of the customer 2.
Sachin Tendulkar
Pointing, Ricky, 1000001, c, 1000.0