1. Write a class called House to hold details of the location (street number, st
ID: 3687523 • Letter: 1
Question
1. Write a class called House to hold details of the location (street number, street name, town) and the year of construction and the property tax. Write the appropriate contructor, selector and mutator methods. Also write a toString method to return the details of the house.
2. Write a subclass Home with additional instance fields for the name of the occupying family and the date they moved in.
There should be two overloaded constructor methods:
public Home (int house, String street, String town, String family, Date movedIn)
and
public Home (int house, String street, String town, int yearConstructed, int propertyTax, String family, Date movedIn)
These should make use of the super keyword where appropriate.
There should be the usual methods to set and get the values of the new instance fields:
setFamily getFamily setDateMovedIn getDateMovedIn
There should be a toString method that calls the toString method provided by the House class to return the details of the house and then return the text "has been occupied by FAMILY since DATE", with suitable values inserted.
3. Write a simple driver application that prompts the user to enter the appropriate information about a home, creates an instance of the Home class, and then prints out the details of that home
Explanation / Answer
// House.java
public class House{
int streetNumber;
String streetName;
String town;
int year;
double propertyTax;
public int getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(int streetNumber) {
this.streetNumber = streetNumber;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPropertyTax() {
return propertyTax;
}
public void setPropertyTax(double propertyTax) {
this.propertyTax = propertyTax;
}
public House(int streetNumber, String streetName, String town) {
this.streetNumber = streetNumber;
this.streetName = streetName;
this.town = town;
}
public House(int streetNumber, String streetName, String town, int year, double propertyTax) {
this.streetNumber = streetNumber;
this.streetName = streetName;
this.town = town;
this.year = year;
this.propertyTax = propertyTax;
}
public String toString(){
return "Street Number: " + streetNumber + " Street Name: " + streetName + " Town: " + town + " Year: " + year + " Property Tax: " + propertyTax;
}
}
// Home.java
import java.util.*;
public class Home extends House{
String familyName;
Date moveInDate;
public Home(int streetNumber, String streetName, String town, int year, double propertyTax, String familyName, Date moveInDate) {
super(streetNumber, streetName, town, year, propertyTax);
this.familyName = familyName;
this.moveInDate = moveInDate;
}
public Home (int house, String street, String town, String family, Date movedIn){
super(house, street, town);
this.familyName = family;
this.moveInDate = movedIn;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public Date getMoveInDate() {
return moveInDate;
}
public void setMoveInDate(Date moveInDate) {
this.moveInDate = moveInDate;
}
public String toString(){
return super.toString() + "has been occupied by " + familyName + " since " + moveInDate;
}
}
// Hometest.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Hometest{
public static void main(String args[]){
int streetNumber;
String streetName;
String town;
int year;
double propertyTax;
String familyName = null;
Date moveInDate = null;
Scanner in = new Scanner(System.in);
System.out.print("Enter street number: ");
streetNumber = in.nextInt();
System.out.print("Enter street name: ");
streetName = in.next();
System.out.print("Enter town: ");
town = in.next();
System.out.print("Enter construction year: ");
year = in.nextInt();
System.out.print("Enter property tax: ");
propertyTax = in.nextDouble();
System.out.print("Enter family name: ");
town = in.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
System.out.print("Enter Date of move in: ");
try {
moveInDate = dateFormat.parse(in.next());
} catch (ParseException e) {
e.printStackTrace();
}
Home home = new Home(streetNumber, streetName, town, year, propertyTax, familyName, moveInDate);
System.out.println(home);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.