[英]Confusion with creating an object from a class using an argument constructor with user input
这是我的第一个编程任务,所以我显然是个新手。 我的作业标准是创建一个接受 4 个变量的作业类。 然后我要制作一个程序,该程序接受用户对这些变量的输入,然后使用先前归因的 getter 方法创建一个对象。
赋值创建者类
这个类将使用Assignment 类来创建一个作业。
首先,要求用户输入作业的标题、类别、最高分和截止日期。
使用参数构造函数和用户输入创建 Assignment 类的对象。
使用 getter 方法,显示分配的信息,如下所示。 此类应生成的输出如下面的示例执行部分所示。
我的初始作业类文件:
/**
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("| Category | " + c);" 等会创建并显示存储的内容,但这并不是我被指示要做的。
使用 getter 方法,显示分配的信息,如下所示。
我不知道所需的输出格式是什么,但我假设您被要求通过执行类似的操作来打印 Assignment 实例的字段值
System.out.print("Title of the assignment is: " + create.getTitle());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.