gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:48:37 25 4
gpt4 key购买 nike

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

AssignmentCreator Class
This class will use the Assignment class to create an 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.
Using the getter methods, display the assignment’s information, as shown below. The output that should be produced by this class is shown below in the sample execution section.

我的初始作业类文件:

/**
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);"等将创建并显示存储的内容,但这并不完全是我被指示要做的。

最佳答案

Using the getter methods, display the assignment’s information, as shown below.

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

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

关于java - 使用带有用户输入的参数构造函数从类创建对象的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com