This is a custom, creative exercise, not a problem from the text. Imagine you ar
ID: 3630155 • Letter: T
Question
This is a custom, creative exercise, not a problem from the text.
Imagine you are a programmer for a realtor. You are
asked to create a program that uses a Property class
and three subclasses of Property named Acreage, Condo,
and House. Define each class in its own source code file
within the same Java package. Avoid public data members
to safeguard the data in your objects.
Give the Property class just two data members, street address
(we will assume all properties are in same town) and list price.
These data members will be inherited by the subclasses.
You decide on their access modifiers. Give this
class, and all subclasses, a getInfo method that will report all
object data. Demonstrate method overriding with this method.
Note that the first point states that subclasses have more information than a parent class.
Add these data members to the indicated subclasses:
Acreage class data (1 member): # of acres.
Condo class data (2 members): monthly maintenance fee, unit number.
House class data (3 members): square feet, # of bedrooms, # of baths.
You will need a number of constructors, and possibly other
methods in some classes. To test your classes, create a main
class called RealEstate. In it, create an instance of each subclass
and run the getInfo methods of each instance.
The code I have looks like this:
import java.util.*;
class Acreage
{
int acres;
Acreage(int acres)
{
this.acres=acres;
}
public void getInfo()
{
System.out.println("Acreage class");
System.out.println("No of Acres:"+acres);
}
}
class Condo extends Acreage
{
int fee,unitnumber;
Condo(int acres,int fee,int unitnumber)
{
super(acres);
this.fee=fee;
this.unitnumber=unitnumber;
}
public void getInfo()
{
//calling its baseclass Acreage getInfo mehod()
super.getInfo();
System.out.println("Condo class");
System.out.println("Maintaince fee:"+fee+"unit Number:"+unitnumber);
}
}
class House extends Condo
{
int sqFeet,bedRooms,bathRooms;
House(int acres,int fee,int unitnumber,int sqFeet,int bedRooms,int bathRooms )
{
super(acres,fee,unitnumber);
this.sqFeet=sqFeet;
this.bedRooms=bedRooms;
this.bathRooms=bathRooms;
}
public void getInfo()
{
//calling its baseclass Condo getInfo mehod()
super.getInfo();
System.out.println("Square Feet:"+sqFeet);
System.out.println("Bed Rooms:"+bedRooms);
System.out.println("Bath Rooms:"+bathRooms);
}
}
class Property extends House
{
String address;
int price;
Property(int acres,int fee,int unitnumber,int sqFeet,int bedRooms,int bathRooms,String address,int price)
{
super(acres,fee,unitnumber,sqFeet,bedRooms,bathRooms);
this.address=address;
this.price=price;
}
public void getInfo()
{
super.getInfo();
System.out.println("Address:"+address);
System.out.println("Price:$"+price);
}
}
class Driver
{
public static void main(String[] args)
{
//Create instance of Acreage class
Acreage acr=new Acreage(10);
//Call getInfo mehod of Acreage class (base class)
acr.getInfo();
//Create instance of Codo class subclass
Condo cnd=new Condo(10,20,30);
//Call getInfo mehod of subclass(Child class)
cnd.getInfo();
//Create instance of House class(Child class)
House huse=new House(10,20,30,40,50,60);
//Call getInfo mehod of huse class (base class)
huse.getInfo();
Property p=new Property(10,20,30,40,50,60,"Wales",2000);
p.getInfo();
}
}
However, my feedback on the problem was:
"Asked for 5 files, inheritance is not as asked, data members lack access specifiers."
Any help would be appreciated.
Explanation / Answer
From your description, the classes you should get are:
Property.java -- The Property class
House.java -- The House class (inheriting from the Property class)
Condo.java -- the condo class also inheriting from property
Acreage.java -- the acreage class also inheriting from property class
RealEstate.java -- the main class to run / test your program
That's a total of 5 files - and that's what you should submit. They are all to be declared under a same package, say "realtorsystem" for example
Access modifiers are the get/set methods you create for each data member - these are used to manipulate data members safely when these are declared private.
The getInfo class is the one that has a basic implementation in the Property class, but you OVERRIDE it in every subclass to fit the subclass requirements.
Property.java looks like this:
package realtorsystem; // package declaration
public class Property {
//private data members
private String streetAddress;
private int listPrice;
public Property(String addr, int p) // constructor
{
this.streetAddress = addr;
this.listPrice = p;
}
public String getStreetAddress() { //access modifier - getter
return streetAddress;
}
public int getListPrice() //access modifier - getter
{
return listPrice;
}
public void getInfo() // getInfo implementation
{
String info = "Address: " + this.getStreetAddress()
+ " List Price: " + this.getListPrice();
System.out.println(info);
}
}
House.java looks like this:
package realtorsystem; // same package
public class House extends Property // inherits from Proprty class
{
//private data members
//in addition to those from Proprty class
private int numOfBedrooms;
private int numOfBaths;
private double squareFeet;
//constructor
public House(String addr, int p, int bed, int bath, double feet)
{
super(addr, p); // constructor from superclass
numOfBedrooms = bed;
numOfBaths = bath;
squareFeet = feet;
}
public double getSquareFeet() //access modifier - getter
{
return this.squareFeet;
}
public int getNumOfBedrooms() //access modifier - getter
{
return this.numOfBedrooms;
}
public int getNumOfBaths() //access modifier - getter
{
return this.numOfBaths;
}
//Overriding getInfo method
@Override
public void getInfo()
{
String info = "Address: " + this.getStreetAddress() +
" List Price: " + this.getListPrice() +
" Square Feet: " + this.getSquareFeet() +
" Num of Bedrooms: " + this.getNumOfBedrooms() +
" Num of Baths: " + this.getNumOfBaths() ;
System.out.println(info);
}
}
Condo.java is next - same comments as before apply:
package realtorsystem;
public class Condo extends Property {
private int unitNumber;
private double monthlyFee;
public Condo (String addr, int price, int unit, double monthfee)
{
super(addr,price);
unitNumber = unit;
monthlyFee = monthfee;
}
public int getUnitNumber()
{
return unitNumber;
}
public double getMonthlyFee()
{
return monthlyFee;
}
//Overriding getInfo method
@Override
public void getInfo()
{
String info = "Address: " + this.getStreetAddress() +
" List Price: " + this.getListPrice() +
" Unit Number: " + this.getUnitNumber() +
" Monthly Maintenance Fee: " + this.getMonthlyFee();
System.out.println(info);
}
}
Acreage.java looks like this
package realtorsystem;
public class Acreage extends Property {
private int numOfAcres;
public Acreage(String addr, int price, int acres)
{
super(addr, price);
numOfAcres = acres;
}
public int getNumOfAcres()
{
return numOfAcres;
}
//Overriding getInfo method
@Override
public void getInfo()
{
String info = "Address: " + this.getStreetAddress() +
" List Price: " + this.getListPrice() +
" Number of Acres: " + this.getNumOfAcres();
System.out.println(info);
}
}
Finally, the main class / test class is RealEstate.java
package realtorsystem;
public class RealEstate {
public static void main(String[] args) {
House aHouse = new House("H Street",200000,5,3,100);
Condo aCondo = new Condo("C Street", 100000, 12, 500);
Acreage anAcreage = new Acreage("A Street", 150000, 60);
System.out.println("House Info:");
aHouse.getInfo();
System.out.println("Condo Info:");
aCondo.getInfo();
System.out.println("Acreage Info:");
anAcreage.getInfo();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.