SlideShare a Scribd company logo
You are given a specification for some Java classes as follows.
A building has a number of floors, and a number of windows.
A house is a building.
A garage is a building.
A room has a length, width, a floor covering, and a number of closets.
You can never create an instance of a building, but every object that is a building must have a
method that calculates the floor space, i.e., the Building class is abstract, and has an abstract
method.
A house has a number of bathrooms, and an array of Rooms.
A house has a method that returns the average size of the Rooms.
A Garage can hold some number of cars, and it may have a cement floor or a gravel floor. A
garage has a length and a width. (Don’t use the Room class as a member of the Garage class.)
Object
/ 
Building Room
/ 
House Garage
2. Implement the specification.
You must use the following mechanisms correctly:
· Inheritance – is a
· Composition – has a
· Constructor methods
· Accessor / mutator methods = getters, setters
· Arrays of objects
· Passing arrays of objects to methods
· Abstract classes and methods
· Access modifiers – public private
o might not need protected, a you should never use
· toString() methods
· the super keyword
· method overriding
· interfaces
· generics are introduced
· polymorphism using inheritance, and interfaces
Include a test class that has a main method. This test program will make instances of your classes
and output the text representation of the objects using the toString() methods. There should be
enough comments in the output so that it is easy to see which classes are being tested.
In your test class, create an ArrayList. Add a couple of houses and garages to your list. Loop
through the list, using the enhanced for loop. Print out the objects using the toString methods.
(The toString methods should return a String object containing all of the instance variables’
values.)
Additional Requirements:
Only methods used for testing will call the System.out.println() method. The purpose of the
classes is to store and manipulate data. Input and output is not included in the data storage and
manipulation classes.
A house will calculate its floor space by looping through the array of rooms, and accumulating
the floor space. Don't worry about the space used by a closet, you can assume that it is included
in the size of the room.
Work incrementally. Start by making a Room class, and testing it. Then make the Building and
Garage classes, test them. The make the house class, and test it.
The constructor for the house class has an array of rooms as a parameter.
The building class is the only class that stores and manipulates the number of windows, and
number of floors.
All garage objects have exactly one floor. Perhaps the Building class should have an overloaded
constructor to accommodate this.
Interface
Add an interface to your package. Call the interface MLSListable. That means that a class that
implements this interface is a property that can be listed for sale. This interface will have only
one method called getMLSListing, and it will return a nicely formatted string about the property
for sale.
- The House class will implement this interface.
- In the Test class add a static method that has one parameter with a data type of
MLSListable. Demonstrate that you can pass a house to that method, but a Garage, and a Room
is not MLSListable so it won’t compile.
==========================================================
- There are some interfaces already provided for you in the Java API. Implement the
Comparable interface for your Room class. compareTo returns the difference between 2 objects
as an int.
There are 2 flavors of the Comparable interface. One uses ‘generics’ and include the type of the
objects that can be compared
class Room implements Comparable
There is an advantage to using this version of Comparable. Look up the 2 versions of
Comparable, and describe why you would use this newer one.
- Override the equals method in the Room class.
- Notice the relationship between the equals method and the compareTo method. If your code
indicates that two room objects are equal, but compareTo returns a non-zero value, there is a
contradiction. Similarly, if compareTo indicates that two objects have a difference of 0, the
equals method must return true for those 2 objects.
Also notice that a.compareTo(b) == -b.compareTo(a) must always be true.
Solution
The Room Class:
public class Room implements Comparable {
private int length;
private int width;
private String floorCovering;
private int numberOfClosets;
public Room(int length, int width, String floorCovering, int numberOfClosets) {
this.length = length;
this.width = width;
this.floorCovering = floorCovering;
this.numberOfClosets = numberOfClosets;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getFloorCovering() {
return floorCovering;
}
public void setFloorCovering(String floorCovering) {
this.floorCovering = floorCovering;
}
public int getNumberOfClosets() {
return numberOfClosets;
}
public void setNumberOfClosets(int numberOfClosets) {
this.numberOfClosets = numberOfClosets;
}
public String toString() {
String result = " ttLength: " + this.getLength() + " Width: " + this.getWidth() + " Closets:
"
+ this.getNumberOfClosets() + " Floor Covering: " + this.getFloorCovering();
return result;
}
public int compareTo(Room arg0) {
if (this.getLength() * this.getWidth() < arg0.getLength() * arg0.getWidth()) {
return -1;
} else if (this.getLength() * this.getWidth() > arg0.getLength() * arg0.getWidth()) {
return 1;
} else {
return 0;
}
}
public boolean equals(Object obj) {
boolean result = false;
if (!(obj instanceof Room)) {
return false;
}
if (this.getLength() * this.getWidth() == ((Room) obj).getLength() * ((Room) obj).getWidth()) {
result = true;
}
return result;
}
}
Building Class:
// The class is abstract so that it can't be instantiated
public abstract class Building {
// Number of floors in the building
private int numberOfFloors;
// Number of Windows in the building
private int numberOfWindows;
// The required Abstract Method
public abstract int floor_space();
// Getter and Setters for the class
public int getNumberOfFloors() {
return numberOfFloors;
}
public void setNumberOfFloors(int numberOfFloors) {
this.numberOfFloors = numberOfFloors;
}
public int getNumberOfWindows() {
return numberOfWindows;
}
public void setNumberOfWindows(int numberOfWindows) {
this.numberOfWindows = numberOfWindows;
}
// String Representation for the class
public String toString() {
String result = "I am a Building!";
return result;
}
}
Garage Class:
public class Garage extends Building {
private int numberOfCars;
private String floorCovering;
private int length;
private int width;
public Garage(int numberOfWindows, int length, int width, int numberOfCars, String
floorCovering) {
super.setNumberOfFloors(1);
super.setNumberOfWindows(numberOfWindows);
this.length = length;
this.width = width;
this.numberOfCars = numberOfCars;
this.floorCovering = floorCovering;
}
public int floor_space(){
return this.length*this.width;
}
public int getNumberOfCars() {
return numberOfCars;
}
public void setNumberOfCars(int numberOfCars) {
this.numberOfCars = numberOfCars;
}
public String getFloorCovering() {
return floorCovering;
}
public void setFloorCovering(String floorCovering) {
this.floorCovering = floorCovering;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String toString() {
String result = "I am a Garage!" + " tLength: " + this.getLength() + " tWidth: " +
this.getWidth()
+ " tFloor Covering: " + this.getFloorCovering() + " tNumber of Cars: " +
this.getNumberOfCars()
+ " ";
return result;
}
}
House Class:
public class House extends Building implements MLSListable {
private Room[] rooms;
private int numberOfBathrooms;
public House(Room[] rooms, int numberOfBathrooms, int numberOfWindows, int
numberOfFloors) {
super.setNumberOfFloors(numberOfFloors);
super.setNumberOfWindows(numberOfWindows);
this.numberOfBathrooms = numberOfBathrooms;
this.rooms = rooms;
}
public String genRoomInfo() {
String result = "";
for (int i = 0; i < rooms.length; i++) {
result += rooms[i].toString();
}
return result;
}
public Room[] getRooms() {
return rooms;
}
public void setRooms(Room[] rooms) {
this.rooms = rooms;
}
public int getNumberOfBathrooms() {
return numberOfBathrooms;
}
public void setNumberOfBathrooms(int numberOfBathrooms) {
this.numberOfBathrooms = numberOfBathrooms;
}
// Abstract MEthod from the Building Class
public int floor_space(){
int result;
int sum = 0;
for (int i = 0; i < this.rooms.length; i++) {
sum += rooms[i].getLength() * rooms[i].getWidth();
}
return sum;
}
public int getAvgRoomSize() {
int result;
int sum = 0;
for (int i = 0; i < this.rooms.length; i++) {
sum += rooms[i].getLength() * rooms[i].getWidth();
}
result = sum / this.rooms.length;
return result;
}
public String toString() {
String result = "I am a House!" + " tAverage Room Size: " + this.getAvgRoomSize() + "
tBathrooms: "
+ this.getNumberOfBathrooms() + " tFloors: " + this.getNumberOfFloors() + "
tWindows: "
+ this.getNumberOfWindows() + " tRooms: " + this.rooms.length + this.genRoomInfo() + "
";
return result;
}
public String getMLSListing() {
return "MLSListing - " + this.toString();
}
}
Then Finally the Test Class:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList buildingsArray = new ArrayList();
Room room1 = new Room(10, 30, "Marble", 1);
Room room2 = new Room(30, 50, "Marble", 1);
Room room3 = new Room(40, 20, "Wood", 2);
Room room4 = new Room(10, 50, "Wood", 4);
Room[] rooms11 = { room1, room2 };
Room[] rooms22 = { room3, room4 };
House house1 = new House(rooms1, 4, 3, 2);
House house2 = new House(rooms2, 1, 2, 1);
Garage garage1 = new Garage(1, 5, 5, 2, "Cement");
Garage garage2 = new Garage(1, 6, 6, 3, "Gravel");
buildingsArray.add(house1);
buildingsArray.add(house2);
buildingsArray.add(garage1);
buildingsArray.add(garage2);
for (Building aBuilding : buildingsArray) {
if (aBuilding instanceof House) {
housePassMethod((House)aBuilding);
} else {
System.out.println(aBuilding.toString());
}
}
System.out.println("Room1 compared to Room2: " + room1.compareTo(room2));
if (room1.equals(room2)) {
System.out.println("Room1 is equal to Room2 in size.");
} else {
System.out.println("Sizes differ.");
}
}
public static void housePassMethod(MLSListable house) {
System.out.println(house.getMLSListing());
}
public interface MLSListable {
public String getMLSListing();
}
}

More Related Content

Similar to You are given a specification for some Java classes as follows.  A.pdf

Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
Abner Chih Yi Huang
 
Java Generics
Java GenericsJava Generics
Java Generics
jeslie
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.ppt
BhuvanaR13
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
MLG College of Learning, Inc
 
Should be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdfShould be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdf
fashionscollect
 
Intro to programing with java-lecture 3
Intro to programing with java-lecture 3Intro to programing with java-lecture 3
Intro to programing with java-lecture 3
Mohamed Essam
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textareamyrajendra
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
liminescence
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
aromanets
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
Tiểu Hổ
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
 
Lab5
Lab5Lab5
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0tutorialsruby
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0tutorialsruby
 
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfSuggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
ssuser58be4b1
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
Ralph Johnson
 

Similar to You are given a specification for some Java classes as follows.  A.pdf (20)

Collections
CollectionsCollections
Collections
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
Java Generics
Java GenericsJava Generics
Java Generics
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.ppt
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Should be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdfShould be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdf
 
Intro to programing with java-lecture 3
Intro to programing with java-lecture 3Intro to programing with java-lecture 3
Intro to programing with java-lecture 3
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
Lab5
Lab5Lab5
Lab5
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfSuggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
 

More from eyebolloptics

How does law enforcement, courts and corrections work to complemen.pdf
How does law enforcement, courts and corrections work to complemen.pdfHow does law enforcement, courts and corrections work to complemen.pdf
How does law enforcement, courts and corrections work to complemen.pdf
eyebolloptics
 
Check all that apply to different sets of genes that are homologous..pdf
Check all that apply to different sets of genes that are homologous..pdfCheck all that apply to different sets of genes that are homologous..pdf
Check all that apply to different sets of genes that are homologous..pdf
eyebolloptics
 
For the following matrices, determine a cot of basis vectors for the.pdf
For the following matrices, determine a cot of basis vectors for  the.pdfFor the following matrices, determine a cot of basis vectors for  the.pdf
For the following matrices, determine a cot of basis vectors for the.pdf
eyebolloptics
 
Exploring Organizational Culture Research the ITT Tech Virtual Lib.pdf
Exploring Organizational Culture Research the ITT Tech Virtual Lib.pdfExploring Organizational Culture Research the ITT Tech Virtual Lib.pdf
Exploring Organizational Culture Research the ITT Tech Virtual Lib.pdf
eyebolloptics
 
Can you help me write these functions in C I do not need the main f.pdf
Can you help me write these functions in C I do not need the main f.pdfCan you help me write these functions in C I do not need the main f.pdf
Can you help me write these functions in C I do not need the main f.pdf
eyebolloptics
 
Drag the term or statement to the correct column SolutionC.pdf
Drag the term or statement to the correct column  SolutionC.pdfDrag the term or statement to the correct column  SolutionC.pdf
Drag the term or statement to the correct column SolutionC.pdf
eyebolloptics
 
Define the labor relations processSolutionThe labor relations.pdf
Define the labor relations processSolutionThe labor relations.pdfDefine the labor relations processSolutionThe labor relations.pdf
Define the labor relations processSolutionThe labor relations.pdf
eyebolloptics
 
Accounting 5205Solution Depreciation aims to recognize in the.pdf
Accounting 5205Solution Depreciation aims to recognize in the.pdfAccounting 5205Solution Depreciation aims to recognize in the.pdf
Accounting 5205Solution Depreciation aims to recognize in the.pdf
eyebolloptics
 
Aiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdf
Aiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdfAiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdf
Aiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdf
eyebolloptics
 
an encryption scheme distributes randomly over the Ascii characters .pdf
an encryption scheme distributes randomly over the Ascii characters .pdfan encryption scheme distributes randomly over the Ascii characters .pdf
an encryption scheme distributes randomly over the Ascii characters .pdf
eyebolloptics
 
A class has 40 students aged 17 to 34. What is the probabilty that a.pdf
A class has 40 students aged 17 to 34. What is the probabilty that a.pdfA class has 40 students aged 17 to 34. What is the probabilty that a.pdf
A class has 40 students aged 17 to 34. What is the probabilty that a.pdf
eyebolloptics
 
A microbial geneticist isolates a new mutation in E. coli and wishes.pdf
A microbial geneticist isolates a new mutation in E. coli and wishes.pdfA microbial geneticist isolates a new mutation in E. coli and wishes.pdf
A microbial geneticist isolates a new mutation in E. coli and wishes.pdf
eyebolloptics
 
30. The vouching of recorded payables to supporting documentation wil.pdf
30. The vouching of recorded payables to supporting documentation wil.pdf30. The vouching of recorded payables to supporting documentation wil.pdf
30. The vouching of recorded payables to supporting documentation wil.pdf
eyebolloptics
 
3. Observe that every cation was initially part of a nitrate compound.pdf
3. Observe that every cation was initially part of a nitrate compound.pdf3. Observe that every cation was initially part of a nitrate compound.pdf
3. Observe that every cation was initially part of a nitrate compound.pdf
eyebolloptics
 
1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf
1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf
1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf
eyebolloptics
 
At what point does commercialization start to create inequality Ple.pdf
At what point does commercialization start to create inequality Ple.pdfAt what point does commercialization start to create inequality Ple.pdf
At what point does commercialization start to create inequality Ple.pdf
eyebolloptics
 
A benefit of an activity received by people not participating in the.pdf
A benefit of an activity received by people not participating in the.pdfA benefit of an activity received by people not participating in the.pdf
A benefit of an activity received by people not participating in the.pdf
eyebolloptics
 
8. Would the following cell have a spontaneous reaction Explain. .pdf
8. Would the following cell have a spontaneous reaction Explain. .pdf8. Would the following cell have a spontaneous reaction Explain. .pdf
8. Would the following cell have a spontaneous reaction Explain. .pdf
eyebolloptics
 
Write a command to find out how many users in etcpasswd have the l.pdf
Write a command to find out how many users in etcpasswd have the l.pdfWrite a command to find out how many users in etcpasswd have the l.pdf
Write a command to find out how many users in etcpasswd have the l.pdf
eyebolloptics
 
Write a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdfWrite a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdf
eyebolloptics
 

More from eyebolloptics (20)

How does law enforcement, courts and corrections work to complemen.pdf
How does law enforcement, courts and corrections work to complemen.pdfHow does law enforcement, courts and corrections work to complemen.pdf
How does law enforcement, courts and corrections work to complemen.pdf
 
Check all that apply to different sets of genes that are homologous..pdf
Check all that apply to different sets of genes that are homologous..pdfCheck all that apply to different sets of genes that are homologous..pdf
Check all that apply to different sets of genes that are homologous..pdf
 
For the following matrices, determine a cot of basis vectors for the.pdf
For the following matrices, determine a cot of basis vectors for  the.pdfFor the following matrices, determine a cot of basis vectors for  the.pdf
For the following matrices, determine a cot of basis vectors for the.pdf
 
Exploring Organizational Culture Research the ITT Tech Virtual Lib.pdf
Exploring Organizational Culture Research the ITT Tech Virtual Lib.pdfExploring Organizational Culture Research the ITT Tech Virtual Lib.pdf
Exploring Organizational Culture Research the ITT Tech Virtual Lib.pdf
 
Can you help me write these functions in C I do not need the main f.pdf
Can you help me write these functions in C I do not need the main f.pdfCan you help me write these functions in C I do not need the main f.pdf
Can you help me write these functions in C I do not need the main f.pdf
 
Drag the term or statement to the correct column SolutionC.pdf
Drag the term or statement to the correct column  SolutionC.pdfDrag the term or statement to the correct column  SolutionC.pdf
Drag the term or statement to the correct column SolutionC.pdf
 
Define the labor relations processSolutionThe labor relations.pdf
Define the labor relations processSolutionThe labor relations.pdfDefine the labor relations processSolutionThe labor relations.pdf
Define the labor relations processSolutionThe labor relations.pdf
 
Accounting 5205Solution Depreciation aims to recognize in the.pdf
Accounting 5205Solution Depreciation aims to recognize in the.pdfAccounting 5205Solution Depreciation aims to recognize in the.pdf
Accounting 5205Solution Depreciation aims to recognize in the.pdf
 
Aiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdf
Aiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdfAiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdf
Aiom for the ADT List. C++ Explain what this pseudocode is doing.a.pdf
 
an encryption scheme distributes randomly over the Ascii characters .pdf
an encryption scheme distributes randomly over the Ascii characters .pdfan encryption scheme distributes randomly over the Ascii characters .pdf
an encryption scheme distributes randomly over the Ascii characters .pdf
 
A class has 40 students aged 17 to 34. What is the probabilty that a.pdf
A class has 40 students aged 17 to 34. What is the probabilty that a.pdfA class has 40 students aged 17 to 34. What is the probabilty that a.pdf
A class has 40 students aged 17 to 34. What is the probabilty that a.pdf
 
A microbial geneticist isolates a new mutation in E. coli and wishes.pdf
A microbial geneticist isolates a new mutation in E. coli and wishes.pdfA microbial geneticist isolates a new mutation in E. coli and wishes.pdf
A microbial geneticist isolates a new mutation in E. coli and wishes.pdf
 
30. The vouching of recorded payables to supporting documentation wil.pdf
30. The vouching of recorded payables to supporting documentation wil.pdf30. The vouching of recorded payables to supporting documentation wil.pdf
30. The vouching of recorded payables to supporting documentation wil.pdf
 
3. Observe that every cation was initially part of a nitrate compound.pdf
3. Observe that every cation was initially part of a nitrate compound.pdf3. Observe that every cation was initially part of a nitrate compound.pdf
3. Observe that every cation was initially part of a nitrate compound.pdf
 
1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf
1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf
1)Please explain the commands ifconfig, ping, traceroute, netstat, d.pdf
 
At what point does commercialization start to create inequality Ple.pdf
At what point does commercialization start to create inequality Ple.pdfAt what point does commercialization start to create inequality Ple.pdf
At what point does commercialization start to create inequality Ple.pdf
 
A benefit of an activity received by people not participating in the.pdf
A benefit of an activity received by people not participating in the.pdfA benefit of an activity received by people not participating in the.pdf
A benefit of an activity received by people not participating in the.pdf
 
8. Would the following cell have a spontaneous reaction Explain. .pdf
8. Would the following cell have a spontaneous reaction Explain. .pdf8. Would the following cell have a spontaneous reaction Explain. .pdf
8. Would the following cell have a spontaneous reaction Explain. .pdf
 
Write a command to find out how many users in etcpasswd have the l.pdf
Write a command to find out how many users in etcpasswd have the l.pdfWrite a command to find out how many users in etcpasswd have the l.pdf
Write a command to find out how many users in etcpasswd have the l.pdf
 
Write a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdfWrite a program that mimics the operations of several vending machin.pdf
Write a program that mimics the operations of several vending machin.pdf
 

Recently uploaded

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 

Recently uploaded (20)

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 

You are given a specification for some Java classes as follows.  A.pdf

  • 1. You are given a specification for some Java classes as follows. A building has a number of floors, and a number of windows. A house is a building. A garage is a building. A room has a length, width, a floor covering, and a number of closets. You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space, i.e., the Building class is abstract, and has an abstract method. A house has a number of bathrooms, and an array of Rooms. A house has a method that returns the average size of the Rooms. A Garage can hold some number of cars, and it may have a cement floor or a gravel floor. A garage has a length and a width. (Don’t use the Room class as a member of the Garage class.) Object / Building Room / House Garage 2. Implement the specification. You must use the following mechanisms correctly: · Inheritance – is a · Composition – has a · Constructor methods · Accessor / mutator methods = getters, setters · Arrays of objects · Passing arrays of objects to methods · Abstract classes and methods · Access modifiers – public private o might not need protected, a you should never use · toString() methods · the super keyword · method overriding · interfaces · generics are introduced · polymorphism using inheritance, and interfaces Include a test class that has a main method. This test program will make instances of your classes
  • 2. and output the text representation of the objects using the toString() methods. There should be enough comments in the output so that it is easy to see which classes are being tested. In your test class, create an ArrayList. Add a couple of houses and garages to your list. Loop through the list, using the enhanced for loop. Print out the objects using the toString methods. (The toString methods should return a String object containing all of the instance variables’ values.) Additional Requirements: Only methods used for testing will call the System.out.println() method. The purpose of the classes is to store and manipulate data. Input and output is not included in the data storage and manipulation classes. A house will calculate its floor space by looping through the array of rooms, and accumulating the floor space. Don't worry about the space used by a closet, you can assume that it is included in the size of the room. Work incrementally. Start by making a Room class, and testing it. Then make the Building and Garage classes, test them. The make the house class, and test it. The constructor for the house class has an array of rooms as a parameter. The building class is the only class that stores and manipulates the number of windows, and number of floors. All garage objects have exactly one floor. Perhaps the Building class should have an overloaded constructor to accommodate this. Interface Add an interface to your package. Call the interface MLSListable. That means that a class that implements this interface is a property that can be listed for sale. This interface will have only one method called getMLSListing, and it will return a nicely formatted string about the property for sale. - The House class will implement this interface. - In the Test class add a static method that has one parameter with a data type of
  • 3. MLSListable. Demonstrate that you can pass a house to that method, but a Garage, and a Room is not MLSListable so it won’t compile. ========================================================== - There are some interfaces already provided for you in the Java API. Implement the Comparable interface for your Room class. compareTo returns the difference between 2 objects as an int. There are 2 flavors of the Comparable interface. One uses ‘generics’ and include the type of the objects that can be compared class Room implements Comparable There is an advantage to using this version of Comparable. Look up the 2 versions of Comparable, and describe why you would use this newer one. - Override the equals method in the Room class. - Notice the relationship between the equals method and the compareTo method. If your code indicates that two room objects are equal, but compareTo returns a non-zero value, there is a contradiction. Similarly, if compareTo indicates that two objects have a difference of 0, the equals method must return true for those 2 objects. Also notice that a.compareTo(b) == -b.compareTo(a) must always be true. Solution The Room Class: public class Room implements Comparable { private int length; private int width; private String floorCovering; private int numberOfClosets; public Room(int length, int width, String floorCovering, int numberOfClosets) { this.length = length; this.width = width;
  • 4. this.floorCovering = floorCovering; this.numberOfClosets = numberOfClosets; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public String getFloorCovering() { return floorCovering; } public void setFloorCovering(String floorCovering) { this.floorCovering = floorCovering; } public int getNumberOfClosets() { return numberOfClosets; } public void setNumberOfClosets(int numberOfClosets) { this.numberOfClosets = numberOfClosets; } public String toString() { String result = " ttLength: " + this.getLength() + " Width: " + this.getWidth() + " Closets: " + this.getNumberOfClosets() + " Floor Covering: " + this.getFloorCovering(); return result; } public int compareTo(Room arg0) { if (this.getLength() * this.getWidth() < arg0.getLength() * arg0.getWidth()) { return -1;
  • 5. } else if (this.getLength() * this.getWidth() > arg0.getLength() * arg0.getWidth()) { return 1; } else { return 0; } } public boolean equals(Object obj) { boolean result = false; if (!(obj instanceof Room)) { return false; } if (this.getLength() * this.getWidth() == ((Room) obj).getLength() * ((Room) obj).getWidth()) { result = true; } return result; } } Building Class: // The class is abstract so that it can't be instantiated public abstract class Building { // Number of floors in the building private int numberOfFloors; // Number of Windows in the building private int numberOfWindows; // The required Abstract Method public abstract int floor_space(); // Getter and Setters for the class public int getNumberOfFloors() { return numberOfFloors; } public void setNumberOfFloors(int numberOfFloors) { this.numberOfFloors = numberOfFloors;
  • 6. } public int getNumberOfWindows() { return numberOfWindows; } public void setNumberOfWindows(int numberOfWindows) { this.numberOfWindows = numberOfWindows; } // String Representation for the class public String toString() { String result = "I am a Building!"; return result; } } Garage Class: public class Garage extends Building { private int numberOfCars; private String floorCovering; private int length; private int width; public Garage(int numberOfWindows, int length, int width, int numberOfCars, String floorCovering) { super.setNumberOfFloors(1); super.setNumberOfWindows(numberOfWindows); this.length = length; this.width = width; this.numberOfCars = numberOfCars; this.floorCovering = floorCovering; } public int floor_space(){ return this.length*this.width; } public int getNumberOfCars() { return numberOfCars; } public void setNumberOfCars(int numberOfCars) {
  • 7. this.numberOfCars = numberOfCars; } public String getFloorCovering() { return floorCovering; } public void setFloorCovering(String floorCovering) { this.floorCovering = floorCovering; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public String toString() { String result = "I am a Garage!" + " tLength: " + this.getLength() + " tWidth: " + this.getWidth() + " tFloor Covering: " + this.getFloorCovering() + " tNumber of Cars: " + this.getNumberOfCars() + " "; return result; } } House Class: public class House extends Building implements MLSListable { private Room[] rooms; private int numberOfBathrooms; public House(Room[] rooms, int numberOfBathrooms, int numberOfWindows, int numberOfFloors) {
  • 8. super.setNumberOfFloors(numberOfFloors); super.setNumberOfWindows(numberOfWindows); this.numberOfBathrooms = numberOfBathrooms; this.rooms = rooms; } public String genRoomInfo() { String result = ""; for (int i = 0; i < rooms.length; i++) { result += rooms[i].toString(); } return result; } public Room[] getRooms() { return rooms; } public void setRooms(Room[] rooms) { this.rooms = rooms; } public int getNumberOfBathrooms() { return numberOfBathrooms; } public void setNumberOfBathrooms(int numberOfBathrooms) { this.numberOfBathrooms = numberOfBathrooms; } // Abstract MEthod from the Building Class public int floor_space(){ int result; int sum = 0; for (int i = 0; i < this.rooms.length; i++) { sum += rooms[i].getLength() * rooms[i].getWidth(); } return sum; } public int getAvgRoomSize() { int result;
  • 9. int sum = 0; for (int i = 0; i < this.rooms.length; i++) { sum += rooms[i].getLength() * rooms[i].getWidth(); } result = sum / this.rooms.length; return result; } public String toString() { String result = "I am a House!" + " tAverage Room Size: " + this.getAvgRoomSize() + " tBathrooms: " + this.getNumberOfBathrooms() + " tFloors: " + this.getNumberOfFloors() + " tWindows: " + this.getNumberOfWindows() + " tRooms: " + this.rooms.length + this.genRoomInfo() + " "; return result; } public String getMLSListing() { return "MLSListing - " + this.toString(); } } Then Finally the Test Class: import java.util.ArrayList; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList buildingsArray = new ArrayList(); Room room1 = new Room(10, 30, "Marble", 1); Room room2 = new Room(30, 50, "Marble", 1); Room room3 = new Room(40, 20, "Wood", 2); Room room4 = new Room(10, 50, "Wood", 4);
  • 10. Room[] rooms11 = { room1, room2 }; Room[] rooms22 = { room3, room4 }; House house1 = new House(rooms1, 4, 3, 2); House house2 = new House(rooms2, 1, 2, 1); Garage garage1 = new Garage(1, 5, 5, 2, "Cement"); Garage garage2 = new Garage(1, 6, 6, 3, "Gravel"); buildingsArray.add(house1); buildingsArray.add(house2); buildingsArray.add(garage1); buildingsArray.add(garage2); for (Building aBuilding : buildingsArray) { if (aBuilding instanceof House) { housePassMethod((House)aBuilding); } else { System.out.println(aBuilding.toString()); } } System.out.println("Room1 compared to Room2: " + room1.compareTo(room2)); if (room1.equals(room2)) { System.out.println("Room1 is equal to Room2 in size."); } else { System.out.println("Sizes differ."); } } public static void housePassMethod(MLSListable house) {