(No code template for this week\'s lab. It should be fairly short! Problem 1 (6
ID: 3869601 • Letter: #
Question
(No code template for this week's lab. It should be fairly short! Problem 1 (6 pts) Suppose that you have a class named BankAccount that represents (surprise!) a bank account. The BankAccount class has two instance variables: private double balance private double annualInterestRate . . The BankAccount class also has the following methods. Note that no code has been provided for the method bodies, but you should be able to determine what the methods do from the descriptions and method headers.) public BankAccount(double b, double ir) A constructor that sets the instance variables to the passed-in values public double checkBalance0 Returns the current balance of this BankAccount object. . public double deposit(double amt) Updates the balance by depositing amt into the account. Returns the new balance. public double withdraw(double amt) Updates the balance by withdrawing amt from the account. Returns the new balance. public double payInterest(0 Updates the balance by making a single annual interest payment based on the current balance. Returns the new balance. e On paper or in a separate document, write the code required for the following actions (1 pt each). You do NOT have to write any code for the BankAccount class. Assume that it already exists as described above. 1. 2. 3. Declare and instantiate a BankAccount object named acctl, with an initial balance of $0 and an annual interest rate of 0.5%. Declare and instantiate a BankAccount object named acct2, with an initial balance of $500 and an annual interest rate of 1.1%. Display the current balances of both acctl and acct2 on the screen. Deposit $120 to acctl and display the new balance on the screen. 4.Explanation / Answer
class BankAccount
{
private double balance;
private double annualInterestRate;
public BankAccount(double b,double ir)
{
balance = b;
annualInterestRate = ir;
}
public double checkBalance()
{
return balance;
}
public double deposit(double amt)
{
balance = balance + amt;
return balance;
}
public double withdraw(double amt)
{
balance = balance - amt;
return balance;
}
public double payInterest()
{
balance = balance + balance *annualInterestRate/100;
return balance;
}
}
class TestBankAccount
{
public static void main(String args[])
{
BankAccount acct1 = new BankAccount(0,0.5);
BankAccount acct2 = new BankAccount(500,1.1);
System.out.println("Current balance of acct1 : "+acct1.checkBalance());
System.out.println("Current balance of acct2 : "+acct2.checkBalance());
acct1.deposit(120);
System.out.println("After depositing $120 Current balance of acct1 : "+acct1.checkBalance());
acct2.withdraw(80);
System.out.println("After withdrawing $80 Current balance of acct2 : "+acct2.checkBalance());
acct1.payInterest();
acct2.payInterest();
System.out.println("After pay interest, Current balance of acct1 : "+acct1.checkBalance());
System.out.println("After pay interest ,Current balance of acct2 : "+acct2.checkBalance());
}
}
Output:
Current balance of acct1 : 0.0
Current balance of acct2 : 500.0
After depositing $120 Current balance of acct1 : 120.0
After withdrawing $80 Current balance of acct2 : 420.0
After pay interest, Current balance of acct1 : 120.6
After pay interest ,Current balance of acct2 : 424.62
2.
class Car
{
private String make;
private String model;
private String color;
private double price;
private double mileage;
public Car()
{
mileage = 0;
make = "none ";
model = "none ";
color = "none ";
price = 0.0;
}
public Car(String make,String model,String color,double price)
{
mileage = 0;
this.make = make;
this.model = model;
this.color = color;
this.price = price;
}
public void setPrice(double p)
{
price = p;
}
public void paint(String c)
{
color = c;
}
public void displayCarInfo()
{
System.out.println(" Car Make: "+make +" Model : "+model +" Color : "+color+ " Price : $"+price +" Mileage : "+mileage);
}
public void travel(double distance)
{
System.out.println(" The car is travelling "+ distance + "miles");
mileage = mileage +distance;
}
}
class TestCar
{
public static void main(String args[])
{
Car car1 = new Car("Porche","Cayman S","black",63800);
Car car2 = new Car("Nissan","Leaf","blue",29010);
car1.displayCarInfo();
car2.displayCarInfo();
car1.paint("purple");
car2.paint("green");
car1.travel(12000);
car2.travel(8000);
car1.setPrice(50000);
car2.setPrice(24500);
car1.displayCarInfo();
car2.displayCarInfo();
}
}
Output:
Car Make: Porche Model : Cayman S Color : black Price : $63800.0 Mileage : 0.0
Car Make: Nissan Model : Leaf Color : blue Price : $29010.0 Mileage : 0.0
The car is travelling 12000.0miles
The car is travelling 8000.0miles
Car Make: Porche Model : Cayman S Color : purple Price : $50000.0 Mileage : 12000.0
Car Make: Nissan Model : Leaf Color : green Price : $24500.0 Mileage : 8000.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.