Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Home { public static void main ( String [ ] args ) { HomeListing [ ] listings = new HomeListing [ 2 ]

public class Home {
public static void main(String[] args){
HomeListing[] listings = new HomeListing[2];
listings[0]= new ListingWithDetails(1,"354 Old Route 73",3500,4,48000,100, "All mailboxes have to be identical.");
listings[1]= new RentalListing(2,"4763 Old Time Way", 43000,3,1500,1000);
for (HomeListing listing : listings){
listing.priceListing();
}
}
}
public abstract class HomeListing {
protected int id;
protected String address;
protected double squareFootage;
protected int bedrooms;
protected double listPrice;
public HomeListing(int id, String address, double squareFootage, int bedrooms){
setId(id);
setAddress(address);
setSquareFootage(squareFootage);
setBedrooms(bedrooms);
setListPrice(listPrice);
}
public abstract void priceListing();
public HomeListing(){
this(0,"",0.0,0);
}
public int getId(){
return id;
}
public void setId(int id){
if (id >0){
this.id = id;
} else {
System.out.println("Invalid");
}
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
public double getSquareFootage(){
return squareFootage;
}
public void setSquareFootage(double squareFootage){
if (squareFootage >0){
this.squareFootage = squareFootage;
} else {
System.out.println("Invalid");
}
}
public int getBedrooms(){
return bedrooms;
}
public void setBedrooms(int bedrooms){
if (bedrooms >0){
this.bedrooms = bedrooms;
} else {
System.out.println("Invalid");
}
}
public double getListPrice(){
return listPrice;
}
public void setListPrice(double listPrice){
if (listPrice >0){
this.listPrice = listPrice;
} else {
System.out.println("Invalid");
}
}
public String toString(){
return String.format("ID: %d, Address: %s, Square Footage: %.2f, Bedrooms: %d",
id, address, squareFootage, bedrooms);
}
}
class ListingWithDetails extends HomeListing {
private double monthlyFee;
private String specialDetails;
public ListingWithDetails(int id, String address, double squareFootage, int bedrooms, double listPrice, double monthlyFee, String specialDetails){
super(id, address, squareFootage, bedrooms);
setListPrice(listPrice); // Ensure setListPrice is called
setMonthlyFee(monthlyFee);
setSpecialDetails(specialDetails);
}
public double getMonthlyFee(){
return monthlyFee;
}
public void setMonthlyFee(double monthlyFee){
if (monthlyFee >=0){
this.monthlyFee = monthlyFee;
} else {
System.out.println("Invalid");
}
}
public String getSpecialDetails(){
return specialDetails;
}
public void setSpecialDetails(String specialDetails){
this.specialDetails = specialDetails;
}
public void priceListing(){
System.out.println(String.format("ID: %d, Address: %s, Square Footage: %.2f, Bedrooms: %d, List Price: %.2f, Monthly Fee: %.2f, Special Details: %s",
id, address, squareFootage, bedrooms, getListPrice(), monthlyFee, specialDetails));
}
}class RentalListing extends HomeListing {
private double monthlyRent;
private double depositAmount;
public RentalListing(int id, String address, double squareFootage, int bedrooms, double monthlyRent, double depositAmount){
super(id, address, squareFootage, bedrooms);
setMonthlyRent(monthlyRent);
setDepositAmount(depositAmount);
}
public double getMonthlyRent(){
return monthlyRent;
}
public void setMonthlyRent(double monthlyRent){
if (monthlyRent >=0){
this.monthlyRent = monthlyRent;
} else {
System.out.println("Invalid");
}
}
public double getDepositAmount(){
return depositAmount;
}
public void setDepositAmount(double depositAmount){
if (depositAmount >=0){
this.depositAmount = depositAmount;
} else {
System.out.println("Invalid");
}
}
public void priceListing(){
System.out.println(String.format("ID: %d, Address: %s, Square Footage: %.2f, Bedrooms: %d, Monthly Rent: %.2f, First Month's Rent: %.2f",
id, address, squareFootage, bedrooms, monthlyRent, monthlyRent + depositAmount));
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions