【问题标题】:writing a constructor in java用java写一个构造函数
【发布时间】:2017-04-24 14:05:27
【问题描述】:

这是我需要做的事情

编写一个构造函数,该构造函数接受四个字段的参数、每个字段的 mutator 方法和每个字段的访问器方法。此外,编写一个实例方法来计算该库存项目的总值。您可以通过将 numberOnHand 乘以价格来计算总值,并将该值作为双精度值返回。

这个程序大部分已经被淘汰了,但我不知道如何让它成为它应该的样子。

这是给我的。

    public PetStoreInventory(String s, String d, int units, double p) {
    //your code goes here
}

public void setDescription( String s){
    //your code goes here
}

public void setDepartment( String d){
  //your code goes here
}

public void setUnitsOnHand ( int units){
    //your code goes here
}

public void setPrice ( double p ){
    //your code goes here
}

public String getDescription(){
    return "";  //so it compiles, you must change this
}

public String getDepartment(){
  return "";  //so it compiles, you must change this
}

public int getUnitsOnHand(){
    return 0;   //so it compiles, you must change this
}

public double getPrice(){
    return 0.0; //so it compiles, you must change this
}

public double calcTotalValue(){
    return 0.0; //so it compiles, you must change this
}

这个输出来自的程序已经制作好了

【问题讨论】:

  • 您尝试了哪些方法,哪些方法不起作用? “这是我的作业,我该怎么做?”不是一个真正可以回答的问题。如果你想学习如何用 Java 编写代码,那么你应该从一些 Java 教程开始。
  • 填空即可。谷歌一下 setter 和 getter 是什么,你将完成 75% 的工作。
  • 首先创建您需要在表中存储前 4 列的四个字段。换句话说,你需要在类本身中存储 (String s, String d, int units, double p)。

标签: java constructor inventory


【解决方案1】:

您需要为四个字段添加实例变量,然后在代码中使用它们。因此,在构造函数中,您将设置这些实例变量的值(这些值通常会从另一个类发送到构造函数)。

private String description;
private String dept;
private int units;
private double price;

public PetStoreInventory(String s, String d, int units, double p) {
    this.description = s;
    this.dept = d;
    this.units = units;
    this.price = p;
}

public void setDescription( String s){
    // set instance var equal to the passed parameter
    this.description = s; 
}

public void setDepartment( String d){
    this.dept = d;
}

public void setUnitsOnHand ( int units){
    this.units = units;
}

public void setPrice ( double p ){
    this.price = p;
}

public String getDescription(){
    return description;  // return the instance var
}

public String getDepartment(){
   return dept; 
}

public int getUnitsOnHand(){
   return units;   
}

public double getPrice(){
   return price; 
}

public double calcTotalValue(){
   double totalVal = 0.0; //initalise a new variable

   /* the next line must calculate the total using the instance
      variables created at the beginning.
    */
   totalVal =?? //to be completed as it is an assignment

   return totalVal;
}

只有“totalVal = ??”这行需要完成。关注问题并使用实例变量,如果您需要更多帮助,请告诉我!

【讨论】:

    【解决方案2】:

    请注意,在您查看此解决方案并尝试自己解决问题后,我们建议您从错误中吸取教训并通过培训进行学习。 为了大家的理解,我尽量简化了

    public class PetStoreInventory {
    private String description;
    private String department;
    private int unitsOnHand;
    private double price;
    private double totalValue;
    public PetStoreInventory(String description, String department,int unitsOnHand, double price){
            this.description = description;
            this.department = department;
            this.unitsOnHand = unitsOnHand;
            this.price = price;
        }
    
        public double calculateTotalValue(){
            return unitsOnHand*price;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public String getDepartment() {
            return department;
        }
        public void setDepartment(String department) {
            this.department = department;
        }
        public int getUnitsOnHand() {
            return unitsOnHand;
        }
        public void setUnitsOnHand(int unitsOnHand) {
            this.unitsOnHand = unitsOnHand;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public double getTotalValue() {
            return totalValue;
        }
        public void setTotalValue(double totalValue) {
            this.totalValue = totalValue;
        }
    
        public static void main(String[] args){
            PetStoreInventory item1 = new PetStoreInventory("Jack Russel", "Dogs", 4, 359.95);
            String description = item1.getDescription();
            String department = item1.getDepartment();
            int unitsOnHand = item1.getUnitsOnHand();
            double price = item1.getPrice();
            double totalValue = item1.calculateTotalValue();
    
            System.out.println(description + "\t" + department + "\t" + unitsOnHand + "\t" +price  + "\t" + totalValue);
    
        }
    } 
    

    结果例如:

    Jack Russel    Dogs    4   359.95    1439.8
    

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 2010-09-22
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      相关资源
      最近更新 更多