Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your assignment is to create a House class and to test the class using a main program test that will be provided with the assignment.

Your assignment is to create a House class and to test the class using a main program test that will be provided with the assignment. The House class should have the following attributes for a single individual house : address (String -possibly has spaces in it), yearBuilt (year in which the house was built (int)), houseType (HouseType), numBedrooms(int), numBathrooms(double). HouseType is an enumerated type that has only these possible values, PRIVATE, VACATION, RENTAL. You will create the enumerated type in your package, asg3, and it must be named as described earlier.

In your House class you should provide methods to access each attribute of a House instance and methods to modify each attribute of a House instance. Also include a toString method that will create a String of the attributes of any instance of a House all on the same line, nicely formatted (WITH NO NEWLINE in the String). Attributes (data values) for House instances are stored on a data file with address on the first line, yearBuilt on the second line, house type on the 3rd line, and number of bedrooms whitespace and number of bathrooms on the 4th line. So each house instance stored on a data file uses 4 lines of the file.

Rules for attribute values of House instances

  • The address of a House cannot be an empty string nor a string of only whitespace characters, it must contain AT LEAST 2 letters or digits in any combination. If your setter method for the address receives an empty string or all whitespace only, or less than 2 letters or digits, use the default address string, "$$$$" for the address
  • The year built for a House cannot be greater than 2020, and cannot be less than 1600, use default of 2020 anytime a yearBuilt is out of range 1600-2020.
  • The setter for address MUST use the utils package's class MyUtils, static method named properFormat(), to format the house address properly before setting it. (The Java code for MyUtils class can be copied from Canvas under Assignment #3 Files, it must be put into a package, utils, which should be part of your project for assigment #3.)
  • Number of bedrooms may only be 0 to 100 inclusive, use 1 for default
  • Number of bathrooms may only be 0.0 to 100.0 inclusive, use 1.0 for default.

Make sure you provide a default constructor for the House class and one that receives parameters for all attributes (instance variables). In addition, you must provide a public String toString() method for the House class so that when you use a House object (instance) in a String context (for example in a print or println method) the instance is nicely formatted on 1 single line of output, no NEWLINE within it. We will be using this House class in the next few assignments and labs so make sure your class passes all given test cases and document it well. Here is a list of expected method declarations for the House class:

public class House { public static final String DEFAULT_ADDRESS = "$$$$"; public static final int DEFAULT_YEARBUILT = 2020; public House();// default constructor, creates a default House instance // name: DEFAULT_ADDRESS, year built: DEFAULT_YEARBUILT, house type: HouseType.PRIVATE, defaults: number of bedrooms: 1, number of bathrooms 1.0 public House(String aAddr, int aYear, HouseType aType, int aNumBedrooms, double aNumBathrooms); // creates House instance with given data, if any received data is invalid // puts default value into field public String toString(); // returns a nicely formatted String of this House on same line NO NEWLINE public String getAddress();// returns the address of this House public int getYearBuilt(); // returns the year built of this House public HouseType getHouseType(); // returns the house type of this House public int getNumBedreooms(); // returns the number of bedrooms public int getNumBathrooms(); // returns the number of bathrooms public void setAddress(String aAddres); //sets this House's address to received address AFTER calling MyUtils.properFormat() method on it -- remember no empty or all whitespace strings for addresses allowed uses default address if received address is invalid public void setYearBuilt(int aYearBuilt); //sets this House's year built to given value if in proper range or use default year built public void setHouseType(HouseType aType); //sets this House's type to received type public void setNumBedrooms (int aNumBedrooms); // sets this house's number of bedrooms or uses default if invalid should be between 0 and 100 inclusive. public void setNumBathrooms(double aNumBathrooms); //sets this house's number of bathrooms or uses default if invalid, should be between 0 and 100.0 inclusive } Write a second class, HouseUtilsImpl, that has a method to read 1 single House instance and a method to write 1 single House instance. This class, HouseUtilsImpl, has only 2 methods in it, below are the method signatures and method descriptions for the 2 methods you need to write inside of the HouseUtilsImpl class: public class HouseUtilsImpl { //receives: inFile - precondition: inFile is open and ready to read data from // file format: address {newline} year built {newline} type of house {newline} number of bedrooms {space} number of bathrooms {newline} //returns: a single populated House instance with data from inFile if all data is there, // returns null if no data found (end of input) for a house or if // any mismatch of data or missing expected data occurs public static House readFromScanner(Scanner inFile) //receives: outFile - precondition : outFile is open and ready to receive output // returns: nothing // task : received house is written to outFile in program readable format (so that it could be read back in later) public static void writeToFile(PrintWriter out, House house);

HERE IS THE MYUTILS PACKAGE

package utils;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Scanner;

import java.util.StringTokenizer;

public class MyUtils {

//receives: theName, a String to properly format

// returns: a String formatted with first character of each word in

uppercase if first char is a letter

// all letters after first letter in word in lowercase

// all extra spaces removed before and after each word

public static String properFormat(String theName)

{

String temp = "";

boolean atSpace=true; // so that first letter gets capitalized...

theName = theName.trim();

for(int i=0; i< theName.length(); i++)

{

if(Character.isWhitespace(theName.charAt(i)) && !atSpace)

{

atSpace=true;

temp += ' ';

}

else if (atSpace == true)

{

if(!Character.isWhitespace(theName.charAt(i)))

{

temp += Character.toUpperCase(theName.charAt(i));

atSpace = false;

}

}

else

{

temp += Character.toLowerCase(theName.charAt(i));

atSpace = false;

}

}// end for

return temp;

}

//receives: a String, data

//returns: the number of newlines in data

public static int numberLines(String data)

{

int count=0;

for(int i=0; i

{

if (data.charAt(i) ==' ')

count++;

}

return count;

}

// receives: a date as a GregorianCalendar instance

// returns: received date as a string in format mm/dd/yyyy

public static String dateToString(GregorianCalendar date)

{

String temp="";

int month = date.get(Calendar.MONTH);

month++; // add 1 due to zero-based months

int day = date.get(Calendar.DAY_OF_MONTH);

int year = date.get(Calendar.YEAR);

temp = month + "/" + day + "/" + year;

return temp;

}

// receives: theDate as a String in format mm/dd/yyyy

// pre: theDate is in format mm/dd/yyyy

// returns: received date as a correct GregorianCalendar object

public static GregorianCalendar stringToDate(String theDate)

{

StringTokenizer tokenizer = new StringTokenizer(theDate, "/");

String temp = tokenizer.nextToken().trim(); // grabs up to "/"

int month=0, day=1, year=2000; // default date values

try {

month = Integer.parseInt(temp);

month--; // zero-based months

temp = tokenizer.nextToken().trim();

day = Integer.parseInt(temp);

temp = tokenizer.nextToken().trim();

year = Integer.parseInt(temp);

}

catch(NumberFormatException e) {

System.out.println("error extracting date from: " + theDate + "

using default date 1/1/2000");

return new GregorianCalendar(2000, 0, 1); // default instance

returned

}

return new GregorianCalendar(year, month, day); // uses month/day/year

parsed out of String

}

//receives: a string to remove spaces from

// returns a string with all spaces removed from theId as received.

public static String stripSpaces(String theId) {

// TODO Auto-generated method stub

String temp="";

int count=0;

for(int i=0; i< theId.length(); i++)

{

if(!Character.isWhitespace(theId.charAt(i)))

{

temp += theId.charAt(i);

count ++;

if(count == 6)

break;

}

}

return temp;

}

//receives: aString to check chars in

// returns true if the string received has only letters or digits, false

otherwise

public static boolean isValid(String aString)

{

if(aString.length() == 0) // test if empty string

return false;

for(int i=0; i< aString.length(); i++)

{

if(!Character.isLetterOrDigit(aString.charAt(i)))

return false;

}

return true;

}

//receives: a descriptor to print with the current time stamp

// task: prints timeStamp to std output with date(yyyy-mm-dd) and time

(HH:mm:ss) and received descriptor

// descriptor is begins or ends or currently or some such indicator of

what the time is describing

public static void printTimeStamp(String descriptor)

{

String timeStamp = new SimpleDateFormat("yyyy-MM-dd

HH:mm:ss").format(new java.util.Date());

System.out.println("Execution " + descriptor + " at: " + timeStamp);

}

//receives: nothing

// returns name of student entered from keyboard

// proper formats name and returns it

public static String getNameFromStudent()

{

Scanner in = new Scanner(System.in);

System.out.print("Enter your name: " );

String s1 = in.nextLine();

s1 = utils.MyUtils.properFormat(s1).trim();

if(s1.equals(""))

s1 = "No Name Entered";

return s1;

}

}

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

Step: 3

blur-text-image

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

Price fixing was made illegal by the _____

Answered: 1 week ago