RentableDwelling and ApartmentBuilding is done I am stuck on Townhouse, Studio nad tester.
Write an abstract class (Links to an external site.), RentableDwelling, containing:
- a (private (Links to an external site.)) double (Links to an external site.) instance variable (Links to an external site.), rentPerUnit
- a (private (Links to an external site.)) String instance variable (Links to an external site.), name
- a constructor (Links to an external site.) accepting a double (Links to an external site.) and a String used to initialize (Links to an external site.) rentPerUnit and name
- an abstract method (Links to an external site.) getRentalIncome that returns a double (Links to an external site.)
write a (non-abstract) subclass, ApartmentBuilding, containing:
- an int (Links to an external site.) instance variable (Links to an external site.) numberOfApartments
- a constructor (Links to an external site.) accepting a double and String (Links to an external site.), passed to the superclass constructor (Links to an external site.), and an int (Links to an external site.) used to initialize (Links to an external site.) numberOfApartments
- the method (Links to an external site.) getRentalIncome calculated as the product (Links to an external site.) of numberOfApartments and the rent per unit. getRentalIncome returns a double (Links to an external site.).
write a (non-abstract) subclass, TownHouse, containing:
- an int (Links to an external site.) instance variable (Links to an external site.) numberOfRooms
- a constructor (Links to an external site.) accepting a double (Links to an external site.) and String, passed to the superclass constructor (Links to an external site.), and an int (Links to an external site.) used to initialize (Links to an external site.) numberOfRooms
- the method (Links to an external site.) getRentalIncome calculated as the product (Links to an external site.) of numberOfRooms and the rent per unit. getRentalIncome returns a double (Links to an external site.).
write a (non-abstract) subclass, Studio, containing:
- a non arg constructor (Links to an external site.) accepting passing "studio" and 800 to the superclass constructor (Links to an external site.),
- the method (Links to an external site.) getRentalIncome returning 800.
Write a tester class which declares an array of RentableDwelling and reading following data from file rentals.txt and creating and object of type ApartmentBuilding, TownHouse or Studio based on first letter of first string and storing them in polymorphic array of RentalDwellings. Also implement toString for each of subclass. Use a loop to display the contents of array.
A000 Hampton_Ridge 800 8
A000 WindingWood 1000 20
T000 Avalon 200 4
S000 Kevin 800 8
Output of your program with above input file can be
Name: Hampton_Ridge
Rental Income: 6400.0
Name: WindingWood
Rental Income: 20000.0
Name: Avalon
Rental Income: 800.0
Name: Studio
Rental Income: 800.0
Hints: You don't need to take input from user. Your input should come from the file rentals.txt. Create a file rentals.txt and copy the data I provided or create your own data.
Data format is following:
String String double int
A000 Hampton_Ridge 800 8
A000 WindingWood 1000 20
T000 Avalon 200 4
S000 Kevin 800 8
Where first string is property id and first letter of this string identifies if it is a Apartment Building, TownHouse or Studio. If first letter of property id is A, that means it is an Apartment building, if it
T, then its a townhouse ad if it is S, it means its a studio.
Second string in the input is name of the property, double is the rent per unit, int is the number of units or number of rooms.
In above data, first two lines are for apartment building, third is for townhouse and 4th is for studio. For the input of studio, last two input can be ignored as Studio has fixed rent of 800.
Rental Income for Apartment building is calculated by multiplying rentperunit and number of units. Rental Income for Townhouse is calculated by multiplying rentperunit and number of rooms.Rental Income for Studio is fixed as 800.
In tester class you need to declare an array of RentableDwelling and when you are reading from file, you need to check first letter of property id, to decide if you have to create an object of Apartment Building or TownHouse or Studio. Since the array is of type Rentable Dwelling, you can store objects of either of Apartment Building or Studio or TownHouse.
Your output should just output the name of property and rental income for that property.