Java 构造函数让我头疼
好吧,我被困在4号和5号上了,6号还没开始呢。不寻找任何人做他们只是想得到一些帮助完成4和5,并可能给出任何反馈的代码到目前为止Java 构造函数让我头疼,java,methods,constructor,instance-variables,displayobject,Java,Methods,Constructor,Instance Variables,Displayobject,好吧,我被困在4号和5号上了,6号还没开始呢。不寻找任何人做他们只是想得到一些帮助完成4和5,并可能给出任何反馈的代码到目前为止 创建实例变量(属性) 为类创建至少3个PrivateInstance字段(属性) 必须为字段使用至少3种不同的数据类型 创建getter(accessor)和setter(mutator)方法 为每个实例变量创建一个getter(accessor)方法 为每个实例变量创建一个setter(mutator)方法 创建一个显示数据的方法 创建一个名为display的方法,
public class Coffee {
//Instance Variables
private double sugar;
private int milk;
private boolean heat;
//Constructor
public Coffee (double id, int dairy, boolean temp )
{
sugar = id;
milk = dairy;
heat = temp;
}
// (setter)
public void setSugar(double id){
sugar = id;
}
public void setMilk(int dairy){
milk = dairy;
}
public void setSize(boolean temp){
heat = temp;
}
//(getter)
public double getSugar(){
return sugar;
}
public int getMilk(){
return milk;
}
public boolean temp(){
return heat;
}
//Method to display data, (need to work on this)
static void display()
{
System.out.println("You added +sugar+ tablespoons of sugar to your coffee");
System.out.println("You have +dairyin your coffee");
System.out.println("That's a +size+ ounce cup");
}
//Default Constructor (need help with setting heat to a default)
public Coffee() {
sugar = 0;
milk = 0;
heat = 0;
}
}
给你。我已将您的布尔私人成员heat重命名为isHot 我投票结束这个问题,因为SO不是一个众包作业的网站,“我想要帮助”也不是一个编程问题。
public class Coffee {
//Instance Variables
private double sugar;
private int milk;
private boolean isHot; // renamed heat to isHot
private int size;
// Constructor
public Coffee (double id, int dairy, boolean temp ) {
sugar = id;
milk = dairy;
heat = temp;
}
// (setter)
public void setSugar(double id) {
this.sugar = id;
}
public void setMilk(int dairy) {
this.milk = dairy;
}
public void setSize(int size) {
this.size = size;
}
public void setHeat(boolean isHot) {
this.isHot = isHot;
}
//(getter)
public double getSugar() {
return this.sugar;
}
public int getMilk() {
return this.milk;
}
public boolean checkIfHot() {
return this.isHot;
}
public int getSize() {
return this.size;
}
// Method to display data, (need to work on this)
static void display() {
System.out.println("You added " + getSugar() + " tablespoons of sugar to your coffee");
System.out.println("You have " + getMilk() + " in your coffee");
System.out.println("That's a " + getSize() + " ounce cup");
System.out.println("Is the cup hot? " + checkIfHot());
}
// Default Constructor (need help with setting heat to a default)
public Coffee() {
sugar = 0.0;
milk = 0;
heat = false;
size = 0;
}
}