SlideShare a Scribd company logo
1 of 11
Download to read offline
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

Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptBhuvanaR13
 
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 .pdffashionscollect
 
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 3Mohamed 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 unitliminescence
 
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.pdfaromanets
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Tiểu Hổ
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
 
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.pdfssuser58be4b1
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyRalph 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.pdfeyebolloptics
 
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..pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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 .pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 
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. .pdfeyebolloptics
 
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.pdfeyebolloptics
 
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.pdfeyebolloptics
 

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

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfcupulin
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MysoreMuleSoftMeetup
 

Recently uploaded (20)

ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 

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) {