This is my first programming assignment. This is a second file that is intended to test the setters and getters (both constructors and no-argument constructors). How can I clean this up (curly brackets or combining similar methods/lines) to make it look more legible and understandable? I'll be adding comments later.
/**
A class to test the Assignment class
*/
public class AssignmentTester
{
public static void main(String[] args)
{
Assignment getterTest = new Assignment();
System.out.println("TESTING NO-ARGUMENT CONSTRUCTOR AND GETTERS");
System.out.println("=========================================== \n");
System.out.println(getterTest.getTitle());
System.out.println("Expected: Assignment 1 \n");
System.out.println(getterTest.getDueDate());
System.out.println("Expected: 01/01/2019 \n");
System.out.println(getterTest.getMaxPoints());
System.out.println("Expected: 10.0 \n");
System.out.println(getterTest.getCategory());
System.out.println("Expected: Programming Assignments \n \n");
System.out.println("Testing Setters");
System.out.println("=============== \n");
Assignment setterTest = new Assignment();
setterTest.setTitle("CodeLab 12");
System.out.println(setterTest.getTitle());
System.out.println("Expected: CodeLab 1 \n");
setterTest.setDueDate("02/09/2019");
System.out.println(setterTest.getDueDate());
System.out.println("Expected: 02/09/2019 \n");
setterTest.setMaxPoints(5.0);
System.out.println(setterTest.getDueDate());
System.out.println("expected: 5.0 \n");
setterTest.setCategory("CodeLab, Quizzes, ICE");
System.out.println(setterTest.getCategory());
System.out.println("Expected: CodeLab, Quizzes, ICE \n \n");
}
}
If interested, here is my first Java file. Please let me know if I can improve on this in any way:
/**
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;
}
}
-
\$\begingroup\$ I'd recommend evening out your indentation. A lot of seasoned programmers have developed an intuitive sense about what code is and isn't a problem. Things like haphazard indentation can trigger that sense, and so we look at your code and feel like there has to be a problem somewhere. This can be an extra factor if someone is grading your work by hand and trying to rush. If they want to average under a minute per student, they may not take the time to realize it was a false alarm - and they may not care. They may be trying to keep it under 10 seconds. \$\endgroup\$Ed Grimm– Ed Grimm2019年01月28日 04:12:57 +00:00Commented Jan 28, 2019 at 4:12
1 Answer 1
The go to unit testing framework in Java is JUnit and all major IDE tools have builtin support for writing test cases.
However, for very simple purposes and learning purpose, Java JDK has the assert
keyword that can be used to test assumptions and also do unit tests. in both cases (JUnit and assert
) the rule is that failed tests throw exceptions that may have customized messages.
an example of usage of assert
:
assert getterTest.getTitle().equals("Assignment 1") : "wrong title default value"
note that assertions need to be explicitly enabled at runtime:
Enabling and Disabling Assertions By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions.
To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch.
-
\$\begingroup\$ Java assertions are not a testing framework, and using them in that fashion is decidedly non-standard and will confuse other developers. The intent of the assertion framework is to provide weak Design-By-Contract support. You can read more about assertions at docs.oracle.com/javase/8/docs/technotes/guides/language/… \$\endgroup\$Eric Stein– Eric Stein2019年01月28日 20:11:06 +00:00Commented Jan 28, 2019 at 20:11
-
\$\begingroup\$ JUnit uses the same terminology (assertions) and throws the exact same exception when a test fails (
AssertionError
) and it is decidedly the unit testing framework in the Java world, while java assertions are not? fooled me \$\endgroup\$Sharon Ben Asher– Sharon Ben Asher2019年01月28日 20:30:31 +00:00Commented Jan 28, 2019 at 20:30