in java Write a program with an interface containing two methods one called Elec
ID: 3830889 • Letter: I
Question
in java
Explanation / Answer
/**
*
* @author Sam
*/
public class EnergyConsumption implements EneryInterface {
@Override
public double ElectPower(double current, double voltage) {
return current * voltage; //return product of the variables
}
@Override
public double ElectEnergy(double current, double voltage, double hours) {
return current * voltage * hours/1000; //return product of three parameters divided by 1000
}
public static void main(String[] args) {
EnergyConsumption energyConsumption = new EnergyConsumption(); //object required since the methods are not static
double current = 2.0;
double voltage = 120.0;
double power = energyConsumption.ElectPower(current, voltage); //used for calculation
System.out.println("Power consumtion is " + power + "W"); //print with unit
double hours = 1000;
double energy = energyConsumption.ElectEnergy(current, voltage, hours); //used for calculation
System.out.println("Enery consumtion: " +energy+"kWh"); //print with unit
}
}
interface EneryInterface {
double ElectPower(double current, double voltage); //as defined in the question
double ElectEnergy(double current, double voltage, double hours);
}
I tried my best to keep the code as simple as possible. I have commented few lines of the code to make things easy for you. If you think you need more information, or facing any problem with the code, please feel free to comment below. I shall be glad to help you with the code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.