A factory produces three types of machines in different specifications: printers
ID: 3906910 • Letter: A
Question
A factory produces three types of machines in different specifications: printers, scanners and smartphones. Create an abstract class called Machine which includes a machine name price, weight and a description. Include set and get methods for each one of these fields. The methods which set the machine's price and weight are abstract. Create three Machine child classes and name them: Printer, Scanner and Smartphone. The three machines must have prices and weights within a certain range as the table below shows Price Range Machine Printer Weight Range Less than 50 lbs$40-$200 inclusive Default: 25 lbs 2 lbs - 25 lbs Default: 10 lbs 0.25 lbs 0.5 lbs $220-$350 inclusive Default: 0.3 lbs Default: $90 $60-$300 inclusive Default: $80 Scanner Smartphone Default: $200 You are expected to check on the ranges in your set methods. If the value is not within the range, it will automatically be set to the default value Write an application named MachineTest that creates an array of ten objects. It should show how objects of each category. For each machine, display the machine name, price, weight and a description. Save your files as Machine.java, Printer.java, Scanner.java, Smartphone.java, and TestMachine.java your program works for different Below is an output and it should look like this (NOTE: You lose points if the output is in a different format)Explanation / Answer
Machine.java
public abstract class Machine {
//Declaring variables
String name;
double price;
double weight;
String description;
//Setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
//Creating abstract methods
public abstract void setPrice(double price);
public abstract void setWeight(double weight);
//toString() method is used to display the contents of an object
@Override
public String toString() {
return "Name: " + name + " Description: " + description+" Price: " + price + " Weight: " + weight;
}
}
________________
Printer.java
public class Printer extends Machine {
//Parameterized constructor
Printer( String name,double price,double weight,String description)
{
setName(name);
setDescription(description);
setWeight(weight);
setPrice(price);
}
//Setters
@Override
public void setPrice(double p) {
if(p>=40 && p<=200)
price=p;
else
price=90;
}
@Override
public void setWeight(double w) {
if(w<50)
weight=w;
else
weight=25;
}
}
_________________
Scanner.java
public class Scanner extends Machine {
//Parameterized constructor
Scanner( String name,double price,double weight,String description)
{
setName(name);
setDescription(description);
setWeight(weight);
setPrice(price);
}
//Setters
@Override
public void setPrice(double p) {
if(p>=60 && p<=300)
price=p;
else
price=80;
}
@Override
public void setWeight(double w) {
if(w>=2 && w<=25)
weight=w;
else
weight=10;
}
}
_________________
Smartphone.java
public class Smartphone extends Machine {
//Parameterized constructor
Smartphone( String name,double price,double weight,String description)
{
setName(name);
setDescription(description);
setWeight(weight);
setPrice(price);
}
//Setters
@Override
public void setPrice(double p) {
if(p>=220 && p<=350)
price=p;
else
price=200;
}
@Override
public void setWeight(double w) {
if(w>=0.25 && w<=0.5)
weight=w;
else
weight=0.3;
}
}
_________________
TestMachine.java
public class TestMachine {
public static void main(String[] args) {
//Creating an Machine array which holds 10 objects
Machine m[]={new Printer("Printer A",199.0,30.0,"This is a laser Printer"),
new Printer("Printer B",40.0,1.5,"This is a an inkjet Printer"),
new Printer("Printer C",70.0,6.0,"This is a slow inkjet Printer"),
new Printer("Printer D",50.0,7.0,"This is a fast inkjet Printer"),
new Scanner("Scanner A",70.0,4.0,"This is black and white scanner"),
new Scanner("Scanner B",80.0,20.0,"This is a color scanner"),
new Scanner("Scanner C",250.0,20.0,"This scanner is both color and black/white"),
new Smartphone("Smartphone A",230.0,0.4,"This Smartphone has Marshmallow operating system"),
new Smartphone("Smartphone B",240.0,0.3,"This Smartphone has Kitkat operating system"),
new Smartphone("Smartphone C",235.0,0.45,"This Smartphone has Jelly Bean operating system")};
//Displaying the Contents of each object
for(int i=0;i<m.length;i++)
{
System.out.println("Machine: "+(i+1));
System.out.println(m[i].toString()+" ");
}
}
}
__________________
Output:
Machine: 1
Name: Printer A
Description: This is a laser Printer
Price: 199.0
Weight: 30.0
Machine: 2
Name: Printer B
Description: This is a an inkjet Printer
Price: 40.0
Weight: 1.5
Machine: 3
Name: Printer C
Description: This is a slow inkjet Printer
Price: 70.0
Weight: 6.0
Machine: 4
Name: Printer D
Description: This is a fast inkjet Printer
Price: 50.0
Weight: 7.0
Machine: 5
Name: Scanner A
Description: This is black and white scanner
Price: 70.0
Weight: 4.0
Machine: 6
Name: Scanner B
Description: This is a color scanner
Price: 80.0
Weight: 20.0
Machine: 7
Name: Scanner C
Description: This scanner is both color and black/white
Price: 250.0
Weight: 20.0
Machine: 8
Name: Smartphone A
Description: This Smartphone has Marshmallow operating system
Price: 230.0
Weight: 0.4
Machine: 9
Name: Smartphone B
Description: This Smartphone has Kitkat operating system
Price: 240.0
Weight: 0.3
Machine: 10
Name: Smartphone C
Description: This Smartphone has Jelly Bean operating system
Price: 235.0
Weight: 0.45
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.