Question

Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube....

Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, and the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method, and volume() method to all classes. Write a program that instantiates objects of your classes, tests all instance methods, and outputs each object’s area and volume when appropriate. Lab 2: InheritanceTest2 Implement an inheritance hierarchy for Account class, Checking Account class, and Savings Account class. CheckingAccount class has a special field called withdrawFee and SavingAccount has a special field called interestRate. Write a program that instantiates objects of all your classes, tests all instance methods, and display the balance of each account. Lab 3: FitnessHealthInformation GymsRUs has a need to provide fitness/health information to their clients, including BMI and maximum heart rate. Your task is to write a console program to do this. Body mass index (BMI) is a measure of body fat based on a person’s height and weight. BMI can be used to indicate if you are overweight, obese, underweight, or normal. The formula to calculate BMI is The following BMI categories are based on this calculation. Category BMI Range Underweight less than 18.5 Normal between 18.5 and 24.9 Overweight between 25 and 29.9 Obese 30 or more Max heart rate is calculated as 200 minus a person’s age. FUNCTIONAL REQUIREMENTS Design and code a class called HealthProfile to store information about clients and their fitness data. The attributes (name, age, weight, and height) are private instance variables. The class must include the following methods. method description setName Receives a value to assign to private instance variable setAge Receives a value to assign to private instance variable setWeight Receives a value to assign to private instance variable setHeight Receives TWO inputs (height in feet, inches). Converts and stores the total INCHES in private instance variable getName Returns private instance variable getAge Returns private instance variable getWeight Returns private instance variable getHeight Returns private instance variable (inches) getBMI Calculates and returns BMI getCategory Returns category based on BMI getMaxHR Calculates and returns maximum heart rate Create a SEPARATE TEST CLASS, Lab1Main, to prompt for user input and display output using the HealthProfile class. Process multiple inputs using a loop. You can assume all user input is valid. SAMPLE OUTPUT Enter name or X to quit: John Smith Your age: 31 Your weight: 185 Your height - feet: 5 Your height - inches: 10 Health Profile for Marc Legagneur BMI: 19.3 BMI Category: Fit Max heart rate: 185 Enter name or X to quit: John Smith Your age: 31 Your weight: 120 Your height - feet: 5 Your height - inches: 2 Health Profile for Magic Jones BMI: 21.9 BMI Category: normal Max heart rate: 170 Enter name or X to quit: X

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Point.java

public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}

=====================================

// Square.java

public class Square extends Point {
   private double sideLength;

   public Square(Point start, double length) {
       super(start.getX(), start.getY());
       this.sideLength = length;
   }

   /**
   * @return the side
   */
   public double getSide() {
       return sideLength;
   }

   /**
   * @param side
   * the side to set
   */
   public void setSide(double side) {
       this.sideLength = side;
   }

   public double getArea() {
       return 4 * sideLength;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "A Square start at point: " + super.toString()
               + ", side Length: " + getSide() + ", area: " + getArea();
   }

}

========================================

// Cube.java

public class Cube extends Square {

   public Cube(Point start, double depth) {
       super(start, depth);
   }

   public double getArea() {
       return 6 * getSide();
   }

   public double getVolume() {
       return Math.pow(getSide(), 3);
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "A Cube start at point :(" + getX() + "," + getY()+ "), side : " + getSide() + " Volume : " + getVolume();
   }

}

====================================

// Test.java

public class Test {

   public static void main(String[] args) {
       Square s = new Square(new Point(3, 4), 5);
       Cube c = new Cube(new Point(5, 6), 7);

       System.out.println(s);
       System.out.println(c);

   }

}

======================================

Output:

A Square start at point: (3.0,4.0), side Length: 5.0, area: 20.0
A Cube start at point :(5.0,6.0), side : 7.0 Volume : 343.0

=======================================

2)

// Account.java

public abstract class Account {
// Class variable so that each account
// has a unique number
protected static int numberOfAccounts = 100001;
// Current balance in the account
private double balance;
// Name on the account
private String owner;
// Number bank uses to identify account
private String accountNumber;
/**
* Default constructor
*/
public Account() {
balance = 0;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
/**
* Standard constructor
*
* @param name
* The owner of the account.
* @param amount
* The beginning balance.
*/
public Account(String name, double amount) {
owner = name;
balance = amount;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
/**
* Copy constructor creates another account for the same owner.
*
* @param oldAccount
* The account with information to copy.
* @param amount
* The beginning balance of the new account.
*/
public Account(Account oldAccount, double amount) {
owner = oldAccount.owner;
balance = amount;
accountNumber = oldAccount.accountNumber;
}
/**
* Allows you to add money to the account.
*
* @param amount
* The amount to deposit in the account.
*/
public void deposit(double amount) {
balance = balance + amount;
}
/**
* Allows you to remove money from the account if enough money is available,
* returns true if the transaction was completed, returns false if there was
* not enough money.
*
* @param amount
* The amount to withdraw from the account.
* @return True if there was sufficient funds to complete the transaction,
* false otherwise.
*/
public boolean withdraw(double amount) {
boolean completed = true;
if (amount <= balance) {
balance = balance - amount;
} else {
completed = false;
}
return completed;
}
/**
* Accessor method to balance
*
* @return The balance of the account.
*/
public double getBalance() {
return balance;
}
/**
* accessor method to owner
*
* @return The owner of the account.
*/
public String getOwner() {
return owner;
}
/**
* Accessor method to account number
*
* @return The account number.
*/
public String getAccountNumber() {
return accountNumber;
}
/**
* Mutator method to change the balance
*
* @param newBalance
* The new balance for the account.
*/
public void setBalance(double newBalance) {
balance = newBalance;
}
/**
* Mutator method to change the account number
*
* @param newAccountNumber
* The new account number.
*/
public void setAccountNumber(String newAccountNumber) {
accountNumber = newAccountNumber;
}
}


===================================

// SavingsAccount.java

public class SavingsAccount extends Account {
// Declaring instance variables
private double rate=2.5;
private static int savingsNumber=0;
private String accountNumber;
// Parameterized constructor
public SavingsAccount(String name,double amount) {
super(name,amount);
//super.setAccountNumber(super.getAccountNumber());
this.accountNumber=super.getAccountNumber()+"-"+savingsNumber;
savingsNumber++;
}
  
public SavingsAccount(SavingsAccount oldAccount,double amount)
{
super(oldAccount,amount);
this.rate = oldAccount.getInterestRate();
//super.setAccountNumber(super.getAccountNumber());
this.accountNumber=super.getAccountNumber()+"-"+savingsNumber;
savingsNumber++;
}
// getters
public double getInterestRate() {
return rate;
}
// This method will calculate the Interest
public void postInterest() {
deposit((getBalance() * (getInterestRate()/1200)));
  
}
public String getAccountNumber()
{
return accountNumber;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.printf("AccountNumber %s has been created for %s \nInitial balance = $%.2f",accountNumber,getOwner(),getBalance());
return "";
}
}


========================================

// CheckingAccount.java

public class CheckingAccount extends Account {
  
//Declaring instance variables
private final static double withdrawfee=0.15;
//Parameterized constructor
public CheckingAccount(String name,double amount) {
super(name,amount);
super.setAccountNumber(getAccountNumber()+"-10");
}
  
//This method will deduct the Monthly fee
public boolean withdraw(double amount)
{
boolean b=false;
if(amount>100 && getBalance()+withdrawfee>=amount)
{
super.withdraw(amount+withdrawfee);
System.out.printf("After withdrawl of $%.2f, balance = %.2f\n",amount,getBalance());
b=true;
}
else if(getBalance()>=amount)
{

super.withdraw(amount+withdrawfee);
System.out.printf("After withdrawl of $%.2f, balance = %.2f\n",amount,getBalance());
b=true;
}
else
{
b=false;
  
}
return b;
}
  
}


=====================================

// Test.java

public class Test {
public static void main(String[] args) {
double put_in = 500;
double take_out = 1000;
String money;
String money_in;
String money_out;
boolean completed;
// Test the CheckingAccount class.
CheckingAccount myCheckingAccount = new CheckingAccount(
"Benjamin Franklin", 1000);
System.out.println("Account Number "
+ myCheckingAccount.getAccountNumber() + " belonging to "
+ myCheckingAccount.getOwner());
money = String.format("%.2f", myCheckingAccount.getBalance());
System.out.println("Initial balance = $" + money);
myCheckingAccount.deposit(put_in);
money_in = String.format("%.2f", put_in);
money = String.format("%.2f", myCheckingAccount.getBalance());
System.out.println("After deposit of $" + money_in + ", balance = $"
+ money);
completed = myCheckingAccount.withdraw(take_out);
money_out = String.format("%.2f", take_out);
money = String.format("%.2f", myCheckingAccount.getBalance());
if (completed) {
System.out.println("After withdrawal of $" + money_out
+ ", balance = $" + money);
} else {
System.out.println("Insuffient funds to " + "withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
// Test the SavingsAccount class.
SavingsAccount yourAccount = new SavingsAccount("William Shakespeare",400);
System.out.println("Account Number " + yourAccount.getAccountNumber()
+ " belonging to " + yourAccount.getOwner());
money = String.format("%.2f", yourAccount.getBalance());
System.out.println("Initial balance = $" + money);
yourAccount.deposit(put_in);
money_in = String.format("%.2f", put_in);
money = String.format("%.2f", yourAccount.getBalance());
System.out.println("After deposit of $" + money_in + ", balance = $"
+ money);
completed = yourAccount.withdraw(take_out);
money_out = String.format("%.2f", take_out);
money = String.format("%.2f", yourAccount.getBalance());
if (completed) {
System.out.println("After withdrawal of $" + money_out
+ ", balance = $" + money);
} else {
System.out.println("Insuffient funds " + "to withdraw $"
+ money_out + ", balance = $" + money);
}
yourAccount.postInterest();
money = String.format("%.2f", yourAccount.getBalance());
System.out.println("After monthly interest " + "has been posted, "
+ "balance = $" + money);
System.out.println();
// Test the copy constructor of the
// SavingsAccount class.
SavingsAccount secondAccount = new SavingsAccount(yourAccount, 5);
System.out.println("Account Number " + secondAccount.getAccountNumber()
+ " belonging to " + secondAccount.getOwner());
money = String.format("%.2f", secondAccount.getBalance());
System.out.println("Initial balance = $" + money);
secondAccount.deposit(put_in);
money_in = String.format("%.2f", put_in);
money = String.format("%.2f", secondAccount.getBalance());
System.out.println("After deposit of $" + money_in + ", balance = $"
+ money);
secondAccount.withdraw(take_out);
money_out = String.format("%.2f", take_out);
money = String.format("%.2f", secondAccount.getBalance());
if (completed) {
System.out.println("After withdrawal of $" + money_out
+ ", balance = $" + money);
} else {
System.out.println("Insuffient funds " + "to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
// Test to make sure new accounts are
// numbered correctly.
CheckingAccount yourCheckingAccount = new CheckingAccount(
"Issac Newton", 5000);
System.out.println("Account Number "
+ yourCheckingAccount.getAccountNumber() + " belonging to "
+ yourCheckingAccount.getOwner());
}

}

=======================================

Output:

Account Number 100001-10 belonging to Benjamin Franklin
Initial balance = $1000.00
After deposit of $500.00, balance = $1500.00
After withdrawl of $1000.00, balance = 499.85
After withdrawal of $1000.00, balance = $499.85

Account Number 100002-0 belonging to William Shakespeare
Initial balance = $400.00
After deposit of $500.00, balance = $900.00
Insuffient funds to withdraw $1000.00, balance = $900.00
After monthly interest has been posted, balance = $901.88

Account Number 100002-1 belonging to William Shakespeare
Initial balance = $5.00
After deposit of $500.00, balance = $505.00
Insuffient funds to withdraw $1000.00, balance = $505.00

Account Number 100003-10 belonging to Issac Newton

================================

Add a comment
Know the answer?
Add Answer to:
Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...

    JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • IN JAVA Design and code a class hierarchy that demonstrates your understanding of inheritance and polymorphism....

    IN JAVA Design and code a class hierarchy that demonstrates your understanding of inheritance and polymorphism. Your hierarchy should contain at least 5 classes and one driver program that instantiates each type of object and runs multiple methods on those objects. Inheritance and Polymorphism must be apparent in the project. Please keep in mind that polymorphism was in chapter 37, so you will need to have read both chapters to understand what goes into this project. Bonus if you add...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • Write a program in java. You are tasked with writing an application that will keep track...

    Write a program in java. You are tasked with writing an application that will keep track of Insurance Policies. Create a Policy class called InsurancePolicies.java that will model an insurance policy for a single person. Use the following guidelines to create the Policy class: • An insurance policy has the following attributes: o Policy Number o Provider Name o Policyholder’s First Name o Policyholder’s Last Name o Policyholder’s Age o Policyholder’s Smoking Status (will be “smoker” or “non-smoker”) o Policyholder’s...

  • I am having problems with my java homework in class. Write a program that will interactively...

    I am having problems with my java homework in class. Write a program that will interactively read the name, age, weight in pounds, and height in inches for five people and output the BMI for each one. At the end of the program, it should output the average age, average height and average weight of the five participants. IT HAS TO INCLUDE: Declare and use a program constant of data type int named CONVERSION_FACTOR to represent the value 703. Use...

  • Please solve the following, please type out the code, and do not hand write because its...

    Please solve the following, please type out the code, and do not hand write because its hard to read. I would greatly appreciate it. (JAVA) Problem 1 In a class called H1P1, write a method called bmiOne that takes as arguments a mass in kilograms (a double) and a height in meters (also a double), and returns the body mass index (or BMI) for the given data. If we have mass -m kg and height-h meters, then BMI = Write...

  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Please write this code in C++ Object-Oriented Programming, specify which files are .h, and .cpp, and...

    Please write this code in C++ Object-Oriented Programming, specify which files are .h, and .cpp, and please add comments for the whole code. Include a class diagrams, and explain the approach you used for the project and how you implemented that, briefly in a few sentences. Please note the following: -Names chosen for classes, functions, and variables should effectively convey the purpose and meaning of the named entity. - Code duplication should be avoided by factoring out common code into...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT