Create an abstract class Furniture. Inside this abstract class create a common m
ID: 3599790 • Letter: C
Question
Create an abstract class Furniture.
Inside this abstract class create a common method called getCompanyName(); Provide the same company name, Ashley Furniture for all types of Furniture.
Now create an abstract method called computePrice() that returns the price as an int.
Finally create two concrete furniture classes : Sofa & CoffeeTable. Extend both these classes to the Furniture class. Now provide an implementation for computeprice() by returning $2000 for sofa and $500 for CoffeeTable and by overriding the getCompanyName to return super.getCompanyName.
Explanation / Answer
Furniture.java
public abstract class Furniture {
//Declaring variable
public String name = "Ashley Furniture";
//This method will return the Company name
public String getCompanyName() {
return name;
}
//Declaring abstract class
public abstract int computePrice();
}
_______________
CoffeeTable.java
public class CoffeeTable extends Furniture {
@Override
public int computePrice() {
return 500;
}
@Override
public String getCompanyName() {
return super.getCompanyName();
}
}
_________________
Sofa.java
public class Sofa extends Furniture {
@Override
public int computePrice() {
return 2000;
}
@Override
public String getCompanyName() {
return super.getCompanyName();
}
}
______________
Test.java
public class Test {
public static void main(String[] args) {
//Creating references of Furniture class
Furniture s, ct;
//Creating an CoffeeTable class object
ct = new CoffeeTable();
//Displaying the info
System.out.println("Coffee Table Price :$" + ct.computePrice());
System.out.println("Name :" + ct.getCompanyName());
//Creating an Sofa class object
s = new Sofa();
//Displaying the info
System.out.println(" Sofa Price :$" + s.computePrice());
System.out.println("Name :" + s.getCompanyName());
}
}
________________
Output:
Coffee Table Price :$500
Name :Ashley Furniture
Sofa Price :$2000
Name :Ashley Furniture
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.