简体   繁体   English

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

[英]Confusion with creating an object from a class using an argument constructor with user input

This is my first programming assignment, so I'm obviously a novice.这是我的第一个编程任务,所以我显然是个新手。 The criteria of my assignment is to create an assignment class that takes in 4 variables.我的作业标准是创建一个接受 4 个变量的作业类。 Then I am to make a program that accepts user's input for those variable, and then creates an objects, using previously ascribed getter methods.然后我要制作一个程序,该程序接受用户对这些变量的输入,然后使用先前归因的 getter 方法创建一个对象。

AssignmentCreator Class赋值创建者类
This class will use the Assignment class to create an assignment.这个类将使用Assignment 类来创建一个作业。
First, ask the user to enter the title, category, max points, and due date for the assignment.首先,要求用户输入作业的标题、类别、最高分和截止日期。
Create an object of the Assignment class using the argument constructor and the user's input.使用参数构造函数和用户输入创建 Assignment 类的对象。
Using the getter methods, display the assignment's information, as shown below.使用 getter 方法,显示分配的信息,如下所示。 The output that should be produced by this class is shown below in the sample execution section.此类应生成的输出如下面的示例执行部分所示。

My initial Assignment class file:我的初始作业类文件:

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

This is where I am currently stuck:这是我目前卡住的地方:

/**  
   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());

Now, I know that this correctly asks for input and stores it to 4 variables, but I'm confused as to how to do as the instructions above say.现在,我知道这正确地要求输入并将其存储到 4 个变量,但我对如何按照上面的说明进行操作感到困惑。

I also understand how "System.out.println("| Category | " + c);"我也明白 "System.out.println("| Category | " + c);" etc. would create and show what was stored, but thats not exactly what I was instructed to do.等会创建并显示存储的内容,但这并不是我被指示要做的。

Using the getter methods, display the assignment's information, as shown below.使用 getter 方法,显示分配的信息,如下所示。

I don't know what the desired format of the output is, but I would assume that you're being asked to print the field values of the Assignment instance by doing something like我不知道所需的输出格式是什么,但我假设您被要求通过执行类似的操作来打印 Assignment 实例的字段值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM