In: Computer Science
this a continuation of my previous question.
answer with Java programming language and Netbeans idk
1, 2, 3, 4)
CODE
class Device {
private String serialNumber, color, manufacturer;
private double outputPower;
public Device () {
serialNumber = "";
color = "";
manufacturer = "";
outputPower = 0.0;
}
public Device(String serialNumber, String color, String manufacturer, double outputPower) {
this.serialNumber = serialNumber;
this.color = color;
this.manufacturer = manufacturer;
this.outputPower = outputPower;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public double getOutputPower() {
return outputPower;
}
public void setOutputPower(double outputPower) {
this.outputPower = outputPower;
}
@Override
public String toString() {
return "Device [serialNumber=" + serialNumber + ", color=" + color + ", manufacturer=" + manufacturer
+ ", outputPower=" + outputPower + "]";
}
}
interface AutomationInterface {
enum Power {
ON,
OFF
}
float RFpowerRatio(int inputPower);
double powerUsage(int hours, int costPerKWH);
}
class Television extends Device {
private String type;
private int screenSize;
public Television(String type, int screenSize) {
super();
this.type = type;
this.screenSize = screenSize;
}
public Television(String serialNumber, String color, String manufacturer, double outputPower, String type,
int screenSize) {
super(serialNumber, color, manufacturer, outputPower);
this.type = type;
this.screenSize = screenSize;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getScreenSize() {
return screenSize;
}
public void setScreenSize(int screenSize) {
this.screenSize = screenSize;
}
@Override
public String toString() {
return "Television [type=" + type + ", screenSize=" + screenSize + "]";
}
}
class ElectricOven extends Device{
private int numberOfPlates;
private boolean containsGrill;
public ElectricOven(int numberOfPlates, boolean containsGrill) {
super();
this.numberOfPlates = numberOfPlates;
this.containsGrill = containsGrill;
}
public ElectricOven(String serialNumber, String color, String manufacturer, double outputPower, int numberOfPlates,
boolean containsGrill) {
super(serialNumber, color, manufacturer, outputPower);
this.numberOfPlates = numberOfPlates;
this.containsGrill = containsGrill;
}
public int getNumberOfPlates() {
return numberOfPlates;
}
public void setNumberOfPlates(int numberOfPlates) {
this.numberOfPlates = numberOfPlates;
}
public boolean isContainsGrill() {
return containsGrill;
}
public void setContainsGrill(boolean containsGrill) {
this.containsGrill = containsGrill;
}
@Override
public String toString() {
return "ElectricOven [numberOfPlates=" + numberOfPlates + ", containsGrill=" + containsGrill + "]";
}
}
class LEDBulb extends Device {
private String plugType;
public LEDBulb(String plugType) {
super();
this.plugType = plugType;
}
public LEDBulb(String serialNumber, String color, String manufacturer, double outputPower, String plugType) {
super(serialNumber, color, manufacturer, outputPower);
this.plugType = plugType;
}
public String getPlugType() {
return plugType;
}
public void setPlugType(String plugType) {
this.plugType = plugType;
}
@Override
public String toString() {
return "LEDBulb [plugType=" + plugType + "]";
}
}
5. Implement the AutomationInterface in all 3 classes defined in
step 3 and
hence adding implementation to the methods outlined in step 2.
(a) RF Power ratio is the ratio between output power and input
power. In
electronics devices with negative RF are considered a hazard and
positive
RF as safe. Hence
RF =
POWERout
POWERin
(b) Power usage calculates the total cost of power used by the
device. The
output power needs to be converted to kilowatts (divide by 1000)
before
doing the calculation below:
PewerUsage = outputPower hours costP erKWH
Faculty of Computing and Informatics, Department of Computer
Science
OOP521S: Individual Lab
6. In the driver class create an arraylist of type Device, and add
3 TVs, 2 Ovens,
and 3 Bulbs whereby each of those instances make reference to
Device. Note
that this is demonstration of polymorphism.
7. Print out each of the objects using a custom toString() method
in each of those
classes.
8. Make use of RF ratio to printout all devices that are a hazard
to use.
9. Sort the Device array based on the PowerUsage, starting with the
lowest consumption
first and the highest consumption last.
//Please change the input in Main class according to your testing criteria
//Java code
public class Device implements AutomationInterface { private String serialNumber, color, manufacturer; private double outputPower; public Device () { serialNumber = ""; color = ""; manufacturer = ""; outputPower = 0.0; } @Override public double powerUsage(int hours, int costPerKWH) { double powerUsage = (outputPower*hours*costPerKWH)/1000; return powerUsage; } @Override public float RFpowerRatio(int inputPower) { float RF = (float) (outputPower/inputPower); RF = (float) Math.log10(RF); return RF; } public Device(String serialNumber, String color, String manufacturer, double outputPower) { this.serialNumber = serialNumber; this.color = color; this.manufacturer = manufacturer; this.outputPower = outputPower; } public String getSerialNumber() { return serialNumber; } public void setSerialNumber(String serialNumber) { this.serialNumber = serialNumber; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public double getOutputPower() { return outputPower; } public void setOutputPower(double outputPower) { this.outputPower = outputPower; } @Override public String toString() { return "Device [serialNumber=" + serialNumber + ", color=" + color + ", manufacturer=" + manufacturer + ", outputPower=" + outputPower + "]"; } }
//=========================================================
public interface AutomationInterface { enum Power { ON, OFF } float RFpowerRatio(int inputPower); double powerUsage(int hours, int costPerKWH); }
//======================================================
public class Television extends Device implements AutomationInterface{ private String type; private int screenSize; public Television(String type, int screenSize) { super(); this.type = type; this.screenSize = screenSize; } public Television(String serialNumber, String color, String manufacturer, double outputPower, String type, int screenSize) { super(serialNumber, color, manufacturer, outputPower); this.type = type; this.screenSize = screenSize; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getScreenSize() { return screenSize; } public void setScreenSize(int screenSize) { this.screenSize = screenSize; } @Override public String toString() { return "Television [type=" + type + ", screenSize=" + screenSize + "]"; } /** * RF Power ratio is the ratio between output power and input power. In * electronics devices with negative RF are considered a hazard and positive * RF as safe. Hence * RF = * POWERout * POWERin * @param inputPower * @return */ @Override public float RFpowerRatio(int inputPower) { float RF = (float) (getOutputPower()/inputPower); RF = (float) Math.log10(RF); return RF; } /** * Power usage calculates the total cost of power used by the device. The * output power needs to be converted to kilowatts (divide by 1000) before * doing the calculation below: * @param hours * @param costPerKWH * @return */ @Override public double powerUsage(int hours, int costPerKWH) { //PewerUsage = outputPower hours costP erKWH double powerUsage = (getOutputPower()*hours*costPerKWH)/1000; return powerUsage; } }
//=================================================
public class ElectricOven extends Device implements AutomationInterface{ private int numberOfPlates; private boolean containsGrill; public ElectricOven(int numberOfPlates, boolean containsGrill) { super(); this.numberOfPlates = numberOfPlates; this.containsGrill = containsGrill; } public ElectricOven(String serialNumber, String color, String manufacturer, double outputPower, int numberOfPlates, boolean containsGrill) { super(serialNumber, color, manufacturer, outputPower); this.numberOfPlates = numberOfPlates; this.containsGrill = containsGrill; } public int getNumberOfPlates() { return numberOfPlates; } public void setNumberOfPlates(int numberOfPlates) { this.numberOfPlates = numberOfPlates; } public boolean isContainsGrill() { return containsGrill; } public void setContainsGrill(boolean containsGrill) { this.containsGrill = containsGrill; } @Override public String toString() { return "ElectricOven [numberOfPlates=" + numberOfPlates + ", containsGrill=" + containsGrill + "]"; } @Override public float RFpowerRatio(int inputPower) { float RF = (float) (getOutputPower()/inputPower); RF = (float) Math.log10(RF); return RF; } @Override public double powerUsage(int hours, int costPerKWH) { double powerUsage = (getOutputPower()*hours*costPerKWH)/1000; return powerUsage; } }
//==========================================
public class LEDBulb extends Device implements AutomationInterface { private String plugType; public LEDBulb(String plugType) { super(); this.plugType = plugType; } public LEDBulb(String serialNumber, String color, String manufacturer, double outputPower, String plugType) { super(serialNumber, color, manufacturer, outputPower); this.plugType = plugType; } public String getPlugType() { return plugType; } public void setPlugType(String plugType) { this.plugType = plugType; } @Override public String toString() { return "LEDBulb [plugType=" + plugType + "]"; } @Override public float RFpowerRatio(int inputPower) { float RF = (float) (getOutputPower()/inputPower); RF = (float) Math.log10(RF); return RF; } @Override public double powerUsage(int hours, int costPerKWH) { double powerUsage = (getOutputPower()*hours*costPerKWH)/1000; return powerUsage; } }
//=======================================
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Main { public static void main(String[] args) { ArrayList<Device> devices = new ArrayList<>(); //Add objects to arrayList /** * add 3 TVs, 2 Ovens, * and 3 Bulb */ devices.add(new Television("TV1001","Black","Samsung",230,"Electronic",19)); devices.add(new Television("TV1002","Grey","Micromax",450,"Electronic",29)); devices.add(new Television("TV1002","White","Sony",350,"Electronic",55)); //Ovens devices.add(new ElectricOven("Oven1001","Silver","Philips",350,2,false)); devices.add(new ElectricOven("Oven1002","White","Samsung",450,3,true)); //Bulbs devices.add(new LEDBulb("LED1001","White","Philips",230,"Traditional")); devices.add(new LEDBulb("LED1002","Silver","Syska",230,"Traditional")); devices.add(new LEDBulb("LED1003","Green","Crompton",230,"Traditional")); /** * Print out each of the objects using a custom toString() method in each of those */ System.out.println("Devices Info: \n"); for (Device d:devices ) { System.out.println(d); } /** * use of RF ratio to printout all devices that are a hazard to use. */ System.out.println("\nall devices that are a hazard to use"); for (Device d:devices ) { // negative RF are considered a hazard if(d.RFpowerRatio(2000)<0) System.out.println(d); } /** * Sort the Device array based on the PowerUsage, starting with the lowest consumption * first and the highest consumption last. */ Collections.sort(devices, new Comparator<Device>() { @Override public int compare(Device o1, Device o2) { if(o1.powerUsage(20,350)>o2.powerUsage(20,350)) return 1; else if(o1.powerUsage(20,350)<o2.powerUsage(20,350)) return -1; else return 0; } }); System.out.println("\nAfter sort: "); for (Device d:devices ) { System.out.println(d); } } }
//Output
//if you need any help regarding this solution .......... please leave a comment ........... thanks