这是我的第一个编程任务的测试人员类,我认为该类正确地测试了getter和setter,但我想知道是否有使用return方法的更好方法。我还是一个新手,因为这是我第一次编程。拥有如此多的打印行几乎感觉不适当,但是有没有更好的退货方式? (至少对于Java初学者而言,并不是一件非常复杂的事情)
/**
A class to test the Assignment class
*/
public class AssignmentTester
{
/**
Main method used to start program
@param args the command line arguments for the program
*/
public static void main(String[] args)
{
System.out.println("TESTING NO-ARGUMENT CONSTRUCTOR AND GETTERS \n=========================================== \n");
Assignment noArgGetterTest = new Assignment();
System.out.println(noArgGetterTest.getTitle() + "\nExpected: Assignment 1 \n");
System.out.println(noArgGetterTest.getDueDate() + "\nExpected: 01/01/2019 \n");
System.out.println(noArgGetterTest.getMaxPoints() + "\nExpected: 10.0 \n");
System.out.println(noArgGetterTest.getCategory() + "\nExpected: Programming Assignments \n \n");
System.out.println("Testing Setters \n=============== \n");
Assignment setterTest = new Assignment();
setterTest.setTitle("CodeLab 1");
System.out.println(setterTest.getTitle() + "\nExpected: CodeLab 1 \n");
setterTest.setDueDate("02/09/2019");
System.out.println(setterTest.getDueDate() + "\nExpected: 02/09/2019 \n");
setterTest.setMaxPoints(5.0);
System.out.println(setterTest.getMaxPoints() + "\nExpected: 5.0 \n");
setterTest.setCategory("CodeLab, Quizzes, ICE");
System.out.println(setterTest.getCategory() + "\nExpected: CodeLab, Quizzes, ICE \n \n");
System.out.println("Testing Argument Constructor and Getters \n======================================== \n");
Assignment getterTest = new Assignment("Quiz 3.1", "03/13/2019", 2.0, "CodeLab, Quizzes, ICE");
System.out.println(getterTest.getTitle() + "\nExpected: Quiz 3.1 \n");
System.out.println(getterTest.getDueDate() + "\nExpected: 03/13/2019 \n");
System.out.println(getterTest.getMaxPoints() + "\nExpected: 2.0 \n");
System.out.println(getterTest.getCategory() + "\nExpected: CodeLab, Quizzes, ICE");
}
}
我的第一类文件,用于创建用于创建分配的参数和参数:
/**
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;
}
}
答案 0 :(得分:0)
// I have done the same problem with using the return keyword, its awesome to use //return.
// but you should know "method" before using return keyword. Basically, you will know //later that the "method" helps you to save writing the same code. Cheers:)
public class AssignmentTester {
public static Assignment noArgumentConstructorAndGetters() {
System.out.println("TESTING NO-ARGUMENT CONSTRUCTOR AND GETTERS \n=========================================== \n");
Assignment noArgGetterTest = new Assignment();
return noArgGetterTest;
}
public static void printMethod(Assignment ass) {
System.out.println(ass.getTitle());
System.out.println(ass.getDueDate());
System.out.println(ass.getMaxPoints());
System.out.println(ass.getCategory());
}
public static Assignment testingSetters() {
System.out.println("Testing Setters \n=============== \n");
Assignment setterTest = new Assignment();
setterTest.setTitle("CodeLab 1");
setterTest.setDueDate("02/09/2019");
setterTest.setMaxPoints(5.0);
setterTest.setCategory("CodeLab, Quizzes, ICE");
return setterTest;
}
public static Assignment testingArgumentsAndConstructors() {
System.out.println("Testing Argument Constructor and Getters \n======================================== \n");
Assignment getterTest = new Assignment("Quiz 3.1", "03/13/2019", 2.0, "CodeLab, Quizzes, ICE");
return getterTest;
}
public static void main(String[] args) {
printMethod(noArgumentConstructorAndGetters());
printMethod(testingSetters());
printMethod(testingArgumentsAndConstructors());
}
}