Part A: Bank Account (50 points) Write two classes to describe BankAccount objec
ID: 3725756 • Letter: P
Question
Part A: Bank Account (50 points)
Write two classes to describe BankAccount objects.
A bank account is described by the account owner's name, an account ID (stored as text), and the balance in the account.
A savings account is a type of bank account that is described by this same information and also by an interest rate.
In each class, include (or use inherited versions of):
instance data variables
two constructors
one constructor takes a starting balance; the second constructor creates an account with no money
you decide what other parameters should be included in each
getters and setters
include appropriate value checks when applicable
a toString method
you decide the output
a deposit method
include appropriate value checks
a withdrawal method
include appropriate value check
Write a driver program to demonstrate your class, including code to do the following:
create bank account objects using both constructors
make deposits and withdrawals (with both valid and invalid values)
invoke other methods
Note: your driver program should not be interactive with the user. It should only create objects and invoke methods on those objects to demonstrate that your class works.
You will be graded both on your syntax but also your class design. For full credit, follow good principles of programming and object-oriented design, considering all of the design elements listed below.
Extra Credit (10 points)
Include a minimum balance as part of what describes a savings account. Update the class as necessary, making sure that the account is not allowed to go below this minimum balance.
Part B: Shapes (50 points)
I have provided a PolyShape class that represents polygons by their number of sides and an array that contains their side lengths. You will write child classes for the PolyShape class.
Do not modify the PolyShape class. You will get 0 points for submitting files that use a modified PolyShape class.
PolyShape:
import java.util.Arrays;
public class PolyShape {
private int numSides;
private int[] sideLengths;
/*
* note: PolyShape constructor takes a variable-length parameter list;
* this means it can be invoked with any number of integers;
* inside the constructor, sideLengths is treated as an int[]
*
*/
public PolyShape(int ... sideLengths) {
this.sideLengths = sideLengths;
this.numSides = sideLengths.length;
}
public int getNumSides() {
return numSides;
}
public int[] getSideLengths() {
return Arrays.copyOf(sideLengths, sideLengths.length);
}
public int getPerimeter() {
int perim = 0;
for(int n : sideLengths)
perim += n;
return perim;
}
public String toString() {
String s = "I am a shape with " + numSides + " sides of length: ";
for(int length : sideLengths)
s += length + " ";
s += " I am a polygon.";
return s;
}
}
Write four classes that extend PolyShape (either directly or indirectly).
Quadrilateral
This class represents polygons with four sides
Rectangle
The class represents four-sided polygons where opposite sides are equal length.
This class should have a method getArea() that takes no parameters and returns an integer.
Square
This class represents four-sided polygons where all four sides are of equal length.
This class should have a method getArea() that takes no parameters and returns an integer.
Triangle
This class represents polygons with three sides
This class should have methods isIsoceles and isEquilateral that returns a boolean if two or three (respectively) sides are of equal length
Note you do not have to make sure the three side lengths represent a mathematically valid triangle!
In each class, include:
one or more constructors
getters and setters (if appropriate/necessary)
toString method that returns the number of sides, the side lengths, and all possible names for a shape.
For example, a square might print "I have four sides of length 3, 3, 3, and 3. I am a polygon. I am a quadrilateral. I am a rectangle. I am a square."
You will be graded both on your syntax but also your class design. For full credit, follow good principles of programming and object-oriented design, considering all of the design elements listed below.
Extra Credit (10 points)
Print out a text-based graphic of the shape created when the user creates a square or a rectangle. For example, a 4x5 rectangle might print:
*****
* *
* *
*****
For Part A and Part B
When designing your classes, consider the following.
For all required methods, your class can either include the method directly or inherit it- whichever makes sense for that method!
Think about what the parent-child relationship should be between the classes.
Move common code as high up in the hierarchy as possible.
Use super whenever possible.
Avoid repeated code.
Follow appropriate naming and style conventions.
Follow good principals of object-oriented design and inheritance.
Explanation / Answer
////////////////////////////////////////// PART A ////////////////////////////////////////////
package banking;
public abstract class Account {
private String ownerName;
private String accountId;
private double balance;
public abstract String deposit(double amount);
public abstract double withdrawl(double amount) ;
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
@Override
public String toString() {
return " => Account [ownerName=" + ownerName + ", accountId=" + accountId
+ ", balance=" + balance + "]";
}
}
package banking;
import java.util.Random;
public class SavingAccount extends Account {
//initialize with default interest rate
private float interestRate = 5.5f;
//initialize with default minimumBalance
private double minimumBalance = 500;
Random rand = new Random();
public SavingAccount(String ownerName) {
super();
int rand_int1 = rand.nextInt(1000);
this.setAccountId(""+rand_int1);
this.setOwnerName(ownerName);
}
public SavingAccount(String ownerName,double balance,double minimumAmountRequired) {
super();
int rand_int1 = rand.nextInt(1000);
this.setAccountId(""+rand_int1);
this.setBalance(balance);
this.setMinimumBalance(minimumAmountRequired);
this.setOwnerName(ownerName);
}
public double getMinimumBalance() {
return minimumBalance;
}
public void setMinimumBalance(double minimumBalance) {
this.minimumBalance = minimumBalance;
}
@Override
public double withdrawl(double amount) {
if (amount > 0 && getBalance()- amount > getMinimumBalance()) {
setBalance(getBalance() - amount);
} else {
amount = 0;
}
return amount;
}
@Override
public String deposit(double amount) {
String result = "failure";
if (amount > 0) {
setBalance(getBalance() + amount);
result = "success";
}
return result;
}
public float getInterestRate() {
return interestRate;
}
public void setInterestRate(float interestRate) {
this.interestRate = interestRate;
}
@Override
public String toString() {
return "SavingAccount [interestRate=" + interestRate
+ ", minimumBalance=" + minimumBalance + super.toString() + "]";
}
}
package banking;
public class BankDriver {
public static void main(String[] args) {
Account accWithNoBalance = new SavingAccount("Ashish");
Account accWithStartBalance = new SavingAccount("Chegg", 3000, 1000);
System.out.println("Below Account has been created");
System.out.println(accWithNoBalance.toString());
System.out.println(accWithStartBalance.toString());
// deposit in both account
System.out.println("Deposit amount 200 to Ashish Account "
+ accWithNoBalance.deposit(200));
System.out.println("Deposit amount 200 to Chegg Account "
+ accWithStartBalance.deposit(200));
// updated account details
System.out.println(accWithNoBalance.toString());
System.out.println(accWithStartBalance.toString());
// withdrawl with invalid amount
System.out.println("Withdrawing amount 100 from Ashish Account "
+ accWithNoBalance.withdrawl(100));
System.out.println("Withdrawing amount 4500 from Ashish Account "
+ accWithStartBalance.withdrawl(4500));
// withdrawl with valid amount
accWithNoBalance.deposit(600);
System.out.println("Withdrawing amount 100 from Ashish Account "
+ accWithNoBalance.withdrawl(100));
System.out.println("Withdrawing amount 500 from Ashish Account "
+ accWithStartBalance.withdrawl(500));
// updated account details
System.out.println(accWithNoBalance.toString());
System.out.println(accWithStartBalance.toString());
}
}
/////////////////////////////////////////// PART A ENDS //////////////////////////////////////////
//////////////////////////////////// PART B STARTS //////////////////////////////////////////////
package shape;
public class Quadrilateral extends PolyShape {
public Quadrilateral(int a,int b,int c,int d) {
super(new int[] { a, b, c,d });
}
}
package shape;
public class Rectangle extends PolyShape {
public Rectangle(int length, int breadth) {
super(new int[] { length, breadth, length, breadth });
}
public Integer getArea() {
Integer result = 0;
int[] sides = getSideLengths();
if (sides.length == 4) {
result = sides[0] * sides[1];
}
return result;
}
}
///////////////////////////////////////// Square
package shape;
public class Square extends PolyShape {
public Square(int lengthOfOneSide) {
super(new int[] { lengthOfOneSide, lengthOfOneSide, lengthOfOneSide,
lengthOfOneSide });
}
public Integer getArea() {
Integer result = 0;
int[] sides = getSideLengths();
if (sides.length == 4) {
result = sides[0] * sides[1];
}
return result;
}
}
///////////////////////////////////////// Triangle
package shape;
public class Triangle extends PolyShape {
public Triangle(int sideA, int sideB, int sideC) {
super(new int[] { sideA, sideB, sideC });
}
boolean isEquilateral() {
boolean result = false;
int[] sides = getSideLengths();
if (sides[0] == sides[1] && sides[1] == sides[2])
result = true;
return result;
}
boolean isIsoceles() {
boolean result = false;
int[] sides = getSideLengths();
if (sides[0] == sides[1] || sides[1] == sides[2]
|| sides[2] == sides[0])
result = true;
return result;
}
}