Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment will be in a new Java project, Assignment #3 - Houses In the assignment you will create 3 packages, the first package will

This assignment will be in a new Java project, Assignment #3 - Houses In the assignment you will create 3 packages, the first package will be asg3, and it will have the House class and the HouseUtilsImpl class, and the enumerated type HouseType described below. The second package, test, will have the test methods that test your 2 classes, and any other test classes that might be used. The third package, utils, will contain the class MyUtils

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.
  • 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);

}

Once you have written the 3 classes described above, you will use my test class, HouseTest.java, that will test your House and HouseUtilsImpl classes to see that they follow the specifications described above. In addition you will need the input files, houset6.txt, houseEmpty.txt, house1.txt used by my HouseTest class in testing.

The data files, house6.txt, houseEmpty.txt, houseError.txt, house1.txt should be copied into the top level project folder for Assignment #3, they all can be dragged and dropped to the very top level of project Assignment 3 - House folder in Eclipse.

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

HERE ARE ALL THE REQUIRED ADDITIONAL FILES TO TEST houseError.txt junk more junk lots of junk even more junk XXX $$$ 000 kkk 9999 333idiei houseEmpty.txt house 6.txt 400 WEST AVE. 2010 Vacation 3 2.5 2:05 4 85 N. Main StREET 2020 RES 5100 Elm Pass 1900 Private 2 1.0 7900 east 23rd 1990 renta 1an Bain South 2005 vacation 43.5 38 Canyon rd. 2014 PRIVATE 5 3.5 house1.txt 5100 North MAN 2018 RENtal MyUtils java package utils; impartava.text SimpleDateFormat: import java.util.Calendar: import java.util.GrerurianCalendar: importava.util.Scanner: import java.util.StringTokenizer; public class My Utils //receives: theName, a String to properly format returns: a String formatted with first character of each word in uppercase I first charis a letter all letters after first letter in wurd in lowercase all extra spaces removed before and after each word public static String properFarmst String theName) String temp = "; boolean atspace=true; // so that first Ictter gets capitalized... theName-theName.trimit: farlinti-, is the Name length if Character.iswhitespace(the Name.charAt(&& latSpace) ; at Space temp +="; else if atspace true) if !Character is Whitespace(the Name.charAttil) tempt Character lo uppercaseftheNaire char ALDI atspace = false; temp += Character to LowerCaseitheName.charAt: atspace tale, 2:05 4 Wend for return terno: Mireceives: a String, data Hreturns: the number of newlines in data public static int numberlinesString data int count=0; forfint i=Ciscata.length; i++ 'n" if (data.charAt( count return count, I receives: 2 date as a GregorianCalendar instance dreturns received date as a string in format mm/dd/ public static String cateToStrine GregorianCalendar datel Sting temp": int month-date.getCalendar.MONTH) month-+://add l uue to zero-based months int day=date.cet Calendar DAY OF MONTH: int year=date. Calendar.YARI; tema-month +/+day+/+ year, return teiro: I/ receives: theDate as a String in format mm/dd/ h pre: theCate is in format min/ad/ Il returns: received date as a correct GregorianCalendar object public static GregorianCalendar stringToDate/String theDatel StringTokenizer tokenizer -ren StringTokenizer(theDate, 7): " String temp - tokenizer.nextToken() trim: 1/ stebs up int month=0, day=1. year=2000; // default date values marth - Integer.parseIntitemp): month // ceru-based months temp = tokenizer.nextToken(ltrim clay = Integer.parseint templi temo tokenizer.nextToken(.rim! year - Integer.parselritempi: catchi Number FormatException Salehau Biarloerror extracting date from: " theDate" usir default date 1/1/2000'); return new GregorianCalendar/2000, 0.11: // default instance returned return ren GregorianCalenderyear, month, dayl: // uses month/cay year parsed out of String receives: a string to remove space from I returns a string with all spaces removed from theld as received public static String stripspacesiString theld) [ // TODO Auto-generated methad stub String temp": int count=0 forint i=0; is theld.length; i++) il Character is Whitespace .chard temp + thelecharAttil: count + icourt -- 61 break 2:05 4 receives: aString to check chars in { returns true if the string recalved has only letters or digits, false otherwise public statk boolean is VaidString Strine! if astring.length) // testif empty string return false; Partinti-, i astring length; i++) if character.isletterOr DigitaString.charAt(111 return false; return true; H Mireceives: a descriptor to print with the current timestamp 1/ task: prints timestamp to std output with date add and time oues and received descritor Cescriptor is bepirs or ends or currently or some such ine cator of what the time is describing public static void prin tirrastamaString descriptor u d HHE String timestamp = new SimpleDateFormat ').formatinew ava.util.Dateil: Sateetrit Execution"- descriptor at timestamp: receives: nothing // returns name of student entered from keyboard 1/proper formats name and returns it public statke String petNameFromStudent Scanner in = new Scanner System.in; System.out.printl"Enter your name: String siin.nextLine si-utils. MyUtils.properfurratisl) trimi: if si.equals("B1 51"No Name Entered": return si, HERE ARE ALL THE REQUIRED ADDITIONAL FILES TO TEST houseError.txt junk more junk lots of junk even more junk XXX $$$ 000 kkk 9999 333idiei houseEmpty.txt house 6.txt 400 WEST AVE. 2010 Vacation 3 2.5 2:05 4 85 N. Main StREET 2020 RES 5100 Elm Pass 1900 Private 2 1.0 7900 east 23rd 1990 renta 1an Bain South 2005 vacation 43.5 38 Canyon rd. 2014 PRIVATE 5 3.5 house1.txt 5100 North MAN 2018 RENtal MyUtils java package utils; impartava.text SimpleDateFormat: import java.util.Calendar: import java.util.GrerurianCalendar: importava.util.Scanner: import java.util.StringTokenizer; public class My Utils //receives: theName, a String to properly format returns: a String formatted with first character of each word in uppercase I first charis a letter all letters after first letter in wurd in lowercase all extra spaces removed before and after each word public static String properFarmst String theName) String temp = "; boolean atspace=true; // so that first Ictter gets capitalized... theName-theName.trimit: farlinti-, is the Name length if Character.iswhitespace(the Name.charAt(&& latSpace) ; at Space temp +="; else if atspace true) if !Character is Whitespace(the Name.charAttil) tempt Character lo uppercaseftheNaire char ALDI atspace = false; temp += Character to LowerCaseitheName.charAt: atspace tale, 2:05 4 Wend for return terno: Mireceives: a String, data Hreturns: the number of newlines in data public static int numberlinesString data int count=0; forfint i=Ciscata.length; i++ 'n" if (data.charAt( count return count, I receives: 2 date as a GregorianCalendar instance dreturns received date as a string in format mm/dd/ public static String cateToStrine GregorianCalendar datel Sting temp": int month-date.getCalendar.MONTH) month-+://add l uue to zero-based months int day=date.cet Calendar DAY OF MONTH: int year=date. Calendar.YARI; tema-month +/+day+/+ year, return teiro: I/ receives: theDate as a String in format mm/dd/ h pre: theCate is in format min/ad/ Il returns: received date as a correct GregorianCalendar object public static GregorianCalendar stringToDate/String theDatel StringTokenizer tokenizer -ren StringTokenizer(theDate, 7): " String temp - tokenizer.nextToken() trim: 1/ stebs up int month=0, day=1. year=2000; // default date values marth - Integer.parseIntitemp): month // ceru-based months temp = tokenizer.nextToken(ltrim clay = Integer.parseint templi temo tokenizer.nextToken(.rim! year - Integer.parselritempi: catchi Number FormatException Salehau Biarloerror extracting date from: " theDate" usir default date 1/1/2000'); return new GregorianCalendar/2000, 0.11: // default instance returned return ren GregorianCalenderyear, month, dayl: // uses month/cay year parsed out of String receives: a string to remove space from I returns a string with all spaces removed from theld as received public static String stripspacesiString theld) [ // TODO Auto-generated methad stub String temp": int count=0 forint i=0; is theld.length; i++) il Character is Whitespace .chard temp + thelecharAttil: count + icourt -- 61 break 2:05 4 receives: aString to check chars in { returns true if the string recalved has only letters or digits, false otherwise public statk boolean is VaidString Strine! if astring.length) // testif empty string return false; Partinti-, i astring length; i++) if character.isletterOr DigitaString.charAt(111 return false; return true; H Mireceives: a descriptor to print with the current timestamp 1/ task: prints timestamp to std output with date add and time oues and received descritor Cescriptor is bepirs or ends or currently or some such ine cator of what the time is describing public static void prin tirrastamaString descriptor u d HHE String timestamp = new SimpleDateFormat ').formatinew ava.util.Dateil: Sateetrit Execution"- descriptor at timestamp: receives: nothing // returns name of student entered from keyboard 1/proper formats name and returns it public statke String petNameFromStudent Scanner in = new Scanner System.in; System.out.printl"Enter your name: String siin.nextLine si-utils. MyUtils.properfurratisl) trimi: if si.equals("B1 51"No Name Entered": return si

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

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

Did you print a proof to view color and image consistency?

Answered: 1 week ago