Reputation: 25
So, I'm working on a coding assignment and I've reached an impasse. Here is the feedback I received from my professor and afterwards, I'll attach the code I've been working on and further explain the issue:
Thank you for reaching out. It looks good! The only thing I would suggest is adding high level comments throughout your code and testing the set() methods on the Team objects in your MainTeamClass.java program to ensure that the set() mutator methods work as intended. Call the set() method with a test value and then call the get() method to see if it is the same value.
Here is a copy of the code:
public class Team
{
private String team;
private int points;
private double average;
public Team()
{
team = " Default";
}
public Team(String startTeam, int startPoints, double startAverage)
{
team = startTeam;
points = startPoints;
average = startAverage;
}
public String getTeam()
{
return team;
}
public int getPoints()
{
return points;
}
public double getAverage()
{
return average;
}
public void setTeam(String newTeam)
{
team = newTeam;
}
public void setPoints(int newPoints)
{
if (newPoints >= 0)
{
points = newPoints;
}
}
public void setAverage(double newAverage)
{
if (newAverage >= 0.0)
{
average = newAverage;
}
}
}
And the Main class:
public class MainTeamClass { public static void main(String[] args)
{
Team sanfran = new Team();
String sanfranTeam = sanfran.getTeam();
int sanfranPoints = sanfran.getPoints();
double sanfranAverage = sanfran.getAverage();
System.out.println(" The team name is:" + sanfran.getTeam()
+ "\n The number of points earned equals " + sanfran.getPoints()
+ "\n The average season score is:" + sanfran.getAverage());
Team cowboys = new Team("Dallas Cowboys", 36, 43.5);
String cowboysTeam = cowboys.getTeam();
int cowboysPoints = cowboys.getPoints();
double cowboysAverage = cowboys.getAverage();
System.out.println("\n The team name is: " + cowboys.getTeam()
+ "\n The number of points earned equals " + cowboys.getPoints()
+ "\n The average season score is: " + cowboys.getAverage()); } }
Based on this code, how would I go about testing the set() method and the get() method? Can someone explain how to do so and also provide an example? It wasn't in the chapter for this week in class and I've searched the web for some assistance but to no avail, so I would appreciate as much feedback as possible. Thank you in advance!
Upvotes: 0
Views: 692
Reputation: 4859
You can different methods, one of them assertion
and run using java -ea <program_name>
enable assertion
public class MainTeamClass {
public static void main(String[] args) {
Team cowboys = new Team();
cowboys.setTeam("Dallas Cowboys");
String cowboysTeam = cowboys.getTeam();
assert cowboysTeam == "Dallas Cowboys" : "cowboysTeam Not valid";
cowboys.setPoints(36);
int cowboysPoints = cowboys.getPoints();
assert cowboysPoints == 36 : "cowboysPoints Not valid";
cowboys.setAverage(43.5);
double cowboysAverage = cowboys.getAverage();
assert cowboysAverage == 43.5 : "cowboysAverage Not valid";
}
}
Upvotes: 0
Reputation: 434
"Call the set() method with a test value and then call the get() method to see if it is the same value.": means that the the value passed to the set
method is the same value that has to be returned from the get
method, something like this:
Team team = new Team ();
String test = "stringForTest";
team.setTeam(test);
if(!team.getTeam().equals(test)) {
System.out.println("Problem with set/get method!");
}
Upvotes: 1