如何使用参数Java测试构造函数

时间:2019-01-27 23:28:06

标签: java

这是我的第一次编程任务,所以我显然是新手。我对如何正确使用参数测试构造器感到困惑

第一个文件:

/**
   Describes an assignment's title, due date, total points value, and category
*/

public class Assignment 
{
   private String title;     //Title of assignment
   private String dueDate;   //Due date of assignment
   private double maxPoints; //Max points of assignment
   private String category;  //Category of assignment
   
   /**
      Initialize instance variables for assignment project (no argument-constructor)
   */ 
   public Assignment()  
   {
      title = "Assignment 1";
      dueDate = "01/01/2019";
      maxPoints = 10.0;
      category = "Programming Assignments";
   }
   
   /** 
      Initialize instance variables for the assignment project (argument constructor)
      @param t title of assignment
      @param d due date of assignment
      @param m max points for the assignment
      @param c category of assignment
   */ 
   public Assignment(String t, String d, double m,String c)   
   {
      title = t; 
      dueDate = d;
      maxPoints = m;
      category = c;
   }
   
   /**
      Sets the value of title
      @param t title of assignment
   */
   public void setTitle(String t)
   {
      title = t; 
   }
   
   /**
      Sets the value of dueDate
      @param d due date of assignment
   */
   public void setDueDate(String d)
   {
      dueDate = d;
   }
   
   /**
      Sets value of maxPoints
      @param m max points of assignment
   */
   public void setMaxPoints(double m)
   {
      maxPoints = m;
   }
   
   /**
      Sets the value of category
      @param c category of assignment
   */
   public void setCategory(String c)
   {
      category = c;
   }
      
   /**
      Returns the value of title
      @return title of assingment
   */
   public String getTitle()
   {
      return title;
   }
   
   /**
      Returns the value of dueDate
      @return due date of assignment
   */
   public String getDueDate()
   {
      return dueDate;
   }
   
   /**
      Returns the value of maxPoints
      @return max points of assignment
   */
   public double getMaxPoints()
   {
      return maxPoints;
   }
   
   /**
      Returns the value of category
      @return category of assingmen
   */
   public String getCategory()
   {
      return category;
   }
}

我的测试班:

/**
   A class to test the Assignment class
*/
public class AssignmentTester
{ 
   public static void main(String[] args)
   {
      Assignment ArgGetterTest = new Assignment("Quiz 3.1");
      System.out.println(ArgGetterTest.getTitle());
      System.out.print("Expected: Quiz 3.1");
   }
}
      

我不断收到错误消息“找不到适用于Assignment(String)的合适的构造函数”,但我认为我已使用以下命令将构造函数声明为字符串:

public Assignment(String t, String d, double m,String c)     
    {    
        title = t;   
        dueDate = d;  
        maxPoints = m;  
        category = c;  
    }  

我到底在做什么错?

1 个答案:

答案 0 :(得分:0)

您的构造函数要求四个不同的参数,而您只给了一个。您需要一次给所有4个参数。像new Assignment("Assignment 7", "02/01/2019", 50.0, "Math Assignments");