使用带有用户输入的参数构造函数从类创建对象的困惑

时间:2019-01-28 05:16:33

标签: java

这是我的第一次编程任务,所以我显然是新手。我分配的标准是创建一个包含4个变量的分配类。然后,我要制作一个程序,接受用户对这些变量的输入,然后使用先前指定的getter方法创建一个对象。

  

AssignmentCreator类
  此类将使用Assignment类创建作业。
  首先,要求用户输入作业的标题,类别,最高分和截止日期。
  使用自变量构造函数和用户输入来创建Assignment类的对象。
  使用getter方法,显示作业的信息,如下所示。   该类应产生的输出在下面的示例执行部分中显示。

我的初始Assignment类文件:

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

这是我目前遇到的问题:

/**  
   imports scanner used for user import  
*/  
import java.util.Scanner;   

public class AssignmentCreator  
{  
   public static void main(String[] args)   

   {  
      Scanner S = new Scanner(System.in);

      System.out.print("Create Assignment \n=================\n\n");

      System.out.print("Enter title: ");
      String t = S.nextLine();

      System.out.print("Enter category: ");
      String c = S.nextLine();

      System.out.print("Enter max points: ");
      double m = S.nextDouble();

      System.out.print("Enter due date (MM/DD/YYYY): ");
      String d = S.nextLine();

      Assignment create = new Assignment(t, c, m, d);
      System.out.print("Title of the assignment is: " + create.getTitle());
      System.out.print("category of the assignment is: " + create.getCategory());
      System.out.print("max points of the assignment is: " + create.getMaxPoints());
      System.out.print("due date of the assignment is: " + create.getDueDate());

现在,我知道这正确地要求输入并将其存储到4个变量中,但是我对如何按照上述说明进行操作感到困惑。

我也了解“ System.out.println(“ |类别|” + c);“等将创建并显示存储的内容,但这并不完全是我的指示。

1 个答案:

答案 0 :(得分:1)

  

使用getter方法,显示分配信息,如下所示。

我不知道所需的输出格式是什么,但我假设您被要求通过执行类似的操作来打印Assignment实例的字段值

System.out.print("Title of the assignment is: " + create.getTitle());