Write a Java application that meets the following specifications. Make sure your
ID: 3550253 • Letter: W
Question
Write a Java application that meets the following specifications. Make sure your application compiles and runs correctly. Turn in the .java files in the same CSNS link as the rest of the exam. 17. Write an application that creates and tracks values for houses on a street. Some of the houses are haunted (contain ghosts), and it is particularly important to keep track of which ones. Start with the Simulator and House classes given below. You will need to write a Street class and a StreetDriver class. Street should contain: -a String variable that holds the name of the street -an array list of Houses -a constructor that takes the name of the street as a parameter and instatiates the list -a method called buildHouse() which takes a House as an argument and adds it to the list -a method called demolishHouse() which takes an int as an argument and, if there is a House in the list with that index, removes it from the list. -a method called tourStreet() that shows the name of the street and lists the houses using House's toString() method. Here are a few lines of sample output: Maple Street has the following houses: House at number 1 has 2330 square feet of space Haunted house at number 2 has 2101 square feet of space Haunted house at number 3 has 3092 square feet of space StreetDriver should -Instantiate the Street -Use the simulator to create an array of 20 house sizes in square feet. The smallest valid value for a house is 900 square feet and the largest is 4000 square feet. The average size is 1900 square feet and the standard deviation is 750 sf. -Use the simulator to create an array of 20 booleans, each of which indicates whether a house is haunted. Only 10% of the houses are haunted. -Use the values from the arrays to create Houses and add them to the array list. For each House, use the corresponding values in each array to supply data for House's constructor (that is, the first House should have the square footage shown in the first value of the square footage array, etc.) Supply all values passed as arguments to the Simulator from variables defined in your tester class. For example, your tester class should have a variable called streetName, which represents the name of the Street. DO NOT hard-code these values in your method calls. For example, pass as an argument streetName, not "Maple St." -Run the tourStreet() method for the Street, then demolish the third house in the list and run tourStreet() again. Here is the Simulator class, which is somewhat different from the one used in the lecture example: package simulator; import java.util.Random; public class Simulator { private static double[] nums; public double[] getGaussianData(double mean, double std, int count, double min, double max) { Random r = new Random(); nums = new double[count]; double randDoub; for (int counter = 0; counter < nums.length; counter++){ randDoub = r.nextGaussian() * std + mean; // it's pretty hard to set limits for the values in a good way, so here is a hacky way. if(randDoub > max) randDoub = max; if(randDoub < min) randDoub = min; nums[counter] = randDoub; } return nums; } public double[] getLinearData(int count, double min, double max) { // it would be better to make sure max < min first, but I am not implementing this in this example Random r = new Random(); nums = new double[count]; double randDoub; for (int counter = 0; counter < nums.length; counter++){ randDoub = r.nextDouble() * (max - min) + min; nums[counter] = randDoub; } return nums; } public boolean[] getBooleans(int count, double proportionTrue){ Random r = new Random(); boolean[] vals = new boolean[count]; for(int counter = 0; counter < vals.length; counter++) vals[counter]=(Math.abs(r.nextDouble())<= proportionTrue); return vals; } } Here is the House class: package simulator; public class House { private int number; private boolean haunted; private double squareFeet; public House(int numIn, boolean hauntedIn, double squareFeetIn){ number = numIn; haunted = hauntedIn; squareFeet = squareFeetIn; } public String toString(){ return (haunted?"Haunted house":"House") + " at number " + number + " has " + (int) squareFeet + " square feet of space"; } }
Explanation / Answer
Here is the code, at the end there is the output::
package simulator;
import java.util.ArrayList;
public class Street {
private String streetName;
private ArrayList<House> houseList;
public Street(String streetName){
this.streetName = streetName;
houseList = new ArrayList<House>();
}
public void buildHouse(House house){
houseList.add(house);
}
public boolean demolishHouse(int index){
if(index>(houseList.size()-1)){
System.out.println("Index is greater than houseList size");
return false;
}
else if(houseList.get(index)==null){
System.out.println("No house at index "+index);
return false;
}
else{
houseList.remove(index);
return true;
}
}
public void tourStreet(){
System.out.println(streetName+" has following houses:");
for(House house: houseList){
System.out.println(house.toString());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.