公共類WaterHeater {需要一種使用方法
//public fields set to private
private double water = 1;
private double kilowatts= 2;
private double joules = 3600;
private double temp = 70;
private double jkg = 4200;
private double energy;
private double time;
//Constructor method
public WaterHeater (double water, double kilowatts, double joules, double temp, double jkg) {
}
//Accessors and mutators
//Accessor for Water
public double getWater() {
return water;
}
public void setWater(int water) {
this.water = water;
}
//Accessor for Kilowatts
public double getKilowatts() {
return kilowatts;
}
public void setKilowatts(int kilowatts) {
this.kilowatts = kilowatts;
}
//Accessor for Temperature
public double getTemp() {
return temp;
}
public void setTemp(int temp) {
this.temp = temp;
}
//Method for Energy used
public double getEnergy() {
energy = water*jkg*temp;
return energy;
}
public void setEnergy() {
this.energy = energy;
}
//Method for Time to boil
public double getTime() {
time = energy/kilowatts;
return time;
}
public void setTime() {
this.time = time;
}
}
公共類燒水壺延伸WaterHeater {
public Kettle(double water, double kilowatts, double joules, double temp, double jkg) {
super(water, kilowatts, joules, temp, jkg);
}
public static void main(String args[])
{
userInput kettleinput = new userInput();
System.out.println("\nEnergy used: ");
System.out.println("Time to boil: ");
}
}
公共類userInput {
的溶液public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double getWater;
// These must be initialised
String strWater = null;
int intWater = 0;
System.out.print("Enter amount of water used: ");
System.out.flush();
// read string value from keyboard
try {
strWater = in.readLine();
}
catch (IOException ioe) {
// ignore exception
}
// convert it to integer
try {
intWater = Integer.parseInt(strWater);
}
catch (NumberFormatException nfe) {
System.out.println("Whoops: " + nfe.toString());
System.exit(1);
}
double getKilowatts;
// These must be initialised
String strKilowatts = null;
int intKilowatts = 0;
System.out.print("Enter amount of Kilowatts used: ");
System.out.flush();
// read string value from keyboard
try {
strKilowatts = in.readLine();
}
catch (IOException ioe) {
// ignore exception
}
// convert it to integer
try {
intKilowatts = Integer.parseInt(strKilowatts);
}
catch (NumberFormatException nfe) {
System.out.println("Whoops: " + nfe.toString());
System.exit(1);
}
double getTemp;
// These must be initialised
String strTemp = null;
int intTemp = 0;
System.out.print("Enter the temperature of water raised by: ");
System.out.flush();
// read string value from keyboard
try {
strTemp = in.readLine();
}
catch (IOException ioe) {
// ignore exception
}
// convert it to integer
try {
intTemp = Integer.parseInt(strTemp);
}
catch (NumberFormatException nfe) {
System.out.println("Whoops: " + nfe.toString());
System.exit(1);
}
}
}
對不起,長代碼。找到解決方案來顯示用戶輸入的結果時遇到問題。我有在WaterHeater中創建的方法,並且我想用它們來計算用戶輸入的水,Kowowatts和Temp時使用的能量和煮沸的時間。方法已經完成,我只是找不到一種方法來使用它們。所以當Kettle類運行時,用戶輸入Water,Kilowatts和Temp,並且它會給出結果。任何幫助appriciated。