【问题标题】:Constructors giving me an headache Java让我头疼的构造函数 Java
【发布时间】:2016-01-27 21:21:18
【问题描述】:

好吧,我卡在 4 和 5 还没有开始 6。不寻找任何人来做他们只是想在完成 4 和 5 时得到一些帮助,并可能对到目前为止的代码提供任何反馈。

  1. 创建实例变量(属性) 为您的班级创建至少 3 个私有实例字段(属性) 您的字段必须至少使用 3 种不同的数据类型
  2. 创建getter(访问器)和setter(修改器)方法 为每个实例变量创建一个 getter(访问器)方法 为每个实例变量创建一个 setter(mutator)方法
  3. 创建一个显示数据的方法 创建一个名为 display 的方法,它只打印出对象的所有实例变量的值
  4. 创建 2 个构造函数 创建一个默认构造函数(无参数),将所有实例变量分配给默认值 创建一个参数化构造函数,将所有实例变量作为参数,并将实例变量设置为参数提供的值
  5. 测试你的程序 创建一个名为 Demo.java 的类。此类将包含您的主要方法 使用默认构造函数创建类的实例。 调用你所有的对象集方法来为你的对象赋值 调用对象显示方法,打印出它的值 使用参数化构造函数创建您的类的另一个实例 调用对象的显示方法,打印出它的值

    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;
         }  
    }
    

【问题讨论】:

标签: java methods constructor instance-variables displayobject


【解决方案1】:
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;
    }  
}

给你。我已将您的布尔私有成员 heat 重命名为 isHot。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2012-06-10
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多