Create an Investment class that contains fields to hold the initial value of an investment, the
current value, the profit (calculated as the difference between current value and initial value),
and the percent profit (the profit divided by the initial value). Include a constructor that requires
initial and current values and a display function. Create a House class that includes fields for
street address and square feet, a constructor that requires values for both fields, and a display
function. Create a HouseThatIsAnInvestment class that inherits from Investment and House. It
includes a constructor and a display function that calls the display functions of the parents
import java.util.Scanner;
class Investment {
// contains fields to hold the initial value of an investment, the
// current value, the profit (calculated as the difference between current value
// and initial value),
// and the percent profit (the profit divided by the initial value).
private double initialValue;
private double currentValue;
private double profit;
private double percentProfit;
/**
* Constructor
*
* @param initialValue
* @param currentValue
*/
public Investment(double initialValue, double currentValue) {
this.initialValue = initialValue;
this.currentValue = currentValue;
}
public double calculateProfit() {
profit = currentValue - initialValue;
return profit;
}
public double calculatePercentProfit() {
profit = calculateProfit();
percentProfit = profit / initialValue;
return percentProfit;
}
public void display() {
System.out.println("The initial value of an investment: " + initialValue);
System.out.println("The current value of an investment: " + currentValue);
System.out.println("The profit: " + profit);
System.out.println("The percent profit: " + calculatePercentProfit());
}
}
class House extends Investment {
private String streetAddress;
private double squareFeet;
/**
* Constructor
* @param initialValue
* @param currentValue
* @param streetAddress
* @param squareFeet
*/
public House (double initialValue,double currentValue,String streetAddress,double squareFeet) {
super(initialValue, currentValue);
this.streetAddress=streetAddress;
this.squareFeet=squareFeet;
}
public void display() {
super.display();
System.out.println("The street address: " + streetAddress);
System.out.println("The square feet: " + squareFeet);
}
}
class HouseThatIsAnInvestment extends House {
public HouseThatIsAnInvestment(double initialValue, double currentValue, String streetAddress, double squareFeet) {
super(initialValue, currentValue, streetAddress, squareFeet);
}
}
public class MusicDemo {
public static void main(String[] args) {
HouseThatIsAnInvestment houseThatIsAnInvestment=new HouseThatIsAnInvestment(20, 150, "Street Address",25.5);
houseThatIsAnInvestment.calculateProfit();
houseThatIsAnInvestment.calculatePercentProfit();
houseThatIsAnInvestment.display();
}
}
Comments
Leave a comment