公共课程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: ");
}
}
public class 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中创建的方法,我想用它们来计算当用户进入Water,Kilowatts和Temp时使用的能量和煮沸的时间。方法已经完成,我只是找不到使用它们的方法。因此,当Kettle类运行时,用户输入Water,Kilowatts和Temp,它将给出结果。任何帮助都是适当的。
答案 0 :(得分:2)
我会更改以下内容: 将您的代码从userInput main方法移动到构造函数中。然后你需要使用的所有变量,如intWater和intKilowatts,我将成为成员变量。然后我会提供公共访问方法。
然后你的Kettle类主方法需要实例化一个新的水壶并传递用户输入类的值。然后,您可以从继承自该热水器类的水壶类中获取所需的值,并提供所需的输出方法。
答案 1 :(得分:1)
首先,你需要更好地解释自己。我真的不明白你真正需要什么,但这是我的尝试。
<强> WaterHeater 强>
private static final
)。水壶&amp; userInput 强>
main
的静态函数。这是非法的。我建议你将后一个函数中的所有代码移到第一个函数中。请深呼吸,集中注意力,更好地解释您的需求和已有的内容。始终尝试显示至少编译的代码。