Java Question: This requires four files, three data files and one executable fil
ID: 3670717 • Letter: J
Question
Java Question:
This requires four files, three data files and one executable file.
Class Furniture
implements Comparable<Furniture>
has private data members String item and double price
has a parameterized constructor
has a getPrice() method
has a toString() method
has an abstract method with this signature: public abstract String returnPurpose();
Class Seating
this is a concrete class that inherits from class Furniture
has a private int data member for seating capacity
has a parameterized constructor
has a toString() method
implements the returnPurpose() method
implements Comparable with a compareTo() method that can sort by price
Class Storage
this is a concrete class that inherits from class Furniture
has a private String data member to hold what is stored
has a private boolean data member to indicate if a storage object is electrically powered
has a parameterized constructor
has a toString() method
implements the returnPurpose() method
implements Comparable with a compareTo() method that can sort by price
Class TestFurniture (all code in main method)
create about six objects, some Storage and some Seating. See Sample Output.
make a Furniture[ ] array using the "array initializer" to hold all of these objects.
use a loop to process the array and verify proper object creation.
sort the array using Arrays.sort().
repeat the loop to display the objects again, now ordered by price.
SAMPLE OUTPUT
Before ordering by price
Furniture item: recliner, price $1200.0, seating capacity: 1. Used for sitting
Furniture item: refrigerator, price $1400.0. Powered: true. Used to store food
Furniture item: love seat, price $850.0, seating capacity: 2. Used for sitting
Furniture item: dresser, price $650.0. Powered: false. Used to store clothing
Furniture item: sofa, price $3000.0, seating capacity: 3. Used for sitting
Furniture item: stool, price $140.0, seating capacity: 1. Used for sitting
After ordering by price
Furniture item: stool, price $140.0, seating capacity: 1. Used for sitting
Furniture item: dresser, price $650.0. Powered: false. Used to store clothing
Furniture item: love seat, price $850.0, seating capacity: 2. Used for sitting
Furniture item: recliner, price $1200.0, seating capacity: 1. Used for sitting
Furniture item: refrigerator, price $1400.0. Powered: true. Used to store food
Furniture item: sofa, price $3000.0, seating capacity: 3. Used for sitting
Explanation / Answer
Let us first define the furniture class. This class implements Comparable interface. The Comparable interface, contains one method, called compareTo. This method is used to put the objects using an order. The method returns a negative integer, zero, or a positive integer depending upon whether one object is less than, equal or greater than the other object.
Furniture Class:
import java.util.*;
abstract class Furniture implements Comparable<Furniture>
{
private String item;
private double price;
Furniture(String name, double pr)
{
item=name;
price=pr;
}
public String getName()
{
return item;
}
public double getPrice()
{
return price;
}
public String toString()
{
return item;
}
public abstract String returnPurpose();
}
Class Seating:
class Seating extends Furniture
{
// instance variables - replace the example below with your own
private int seating_capacity;
public Seating(int cap)
{
Super();
this.seating_capacity=cap;
}
public String toString()
{
return item;
}
public String returnPurpose()
{
return "can seat "+seating_capacity+"people";
}
public int compareTo(Furniture f1)
{
Furniture f1=(Furniture)obj;
if(price==f1.price)
return 0;
else if(price>f1.price)
return 1;
else
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.