Hey guys,

I'm working on an entry level java programming assignment and I'm running into some trouble. I've attached 3 java files and will quote the problem below. If anyone can point me into the right direction I would really appreciate it. Thanks!

I'm fairly sure the code needs to be placed into the StudentGrades.java file towards the bottom where the 3 comments are...hope this helps

" Modify the attached grade book program. Change the attached grade book program so it
a. accepts the names of students in a loop until the word “DONE” is entered, and then
b. in a second loop accepts the names of homework assignments until the word “DONE” is entered, and
c. in a third loop (a nested loop) read scores for each of the homework assignments for each of the students.

Please use the format of the prompts as shown in the example run below. Please understand how the code works before starting to make changes – your code must use the StudentGrades and Assignments classes, but you can make changes to the StudentGrades and Assignments class (add new methods and/or instance variables) if necessary.

Sample run of the program:
Enter student name, or DONE if finished: Joe
Enter student name, or DONE if finished: Jane
Enter student name, or DONE if finished: Amy
Enter student name, or DONE if finished: DONE
Enter assignment name, or DONE if finished: Homework 1
Enter assignment name, or DONE if finished: Quiz 1
Enter assignment name, or DONE if finished: DONE
Enter score for Joe Homework 1: 10
Enter score for Joe Quiz 1: 5
Enter score for Jane Homework 1: 10
Enter score for Jane Quiz 1: 10
Enter score for Amy Homework 1: 9
Enter score for Amy Quiz 1: 10
Joe
Homework 1: 10.0
Quiz 1: 5.0
Total score: 15.0 Average: 12.5

Jane
Homework 1: 10.0
Quiz 1: 10.0
Total score: 20.0 Average: 10.

Amy
Homework 1: 9.0
Quiz 1: 10.0
Total score: 19.0 Average: 9.5"

Attachments
/**
 * Keep the assignment name and score for an assignment.
 * @author ghelmer
 *
 */
public class Assignment {
	private double score;
	private String name;
	
	/**
	 * Create the new assignment.
	 * @param name - Name of the assignment
	 * @param score - Score earned for the assignment
	 */
	public Assignment(String name, double score) {
		this.name = name;
		this.score = score;
	}
	
	/**
	 * Get the assignment's score.
	 * @return score
	 */
	public double getScore() {
		return score;
	}
	
	/**
	 * Get the assignment's name.
	 * @return name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Return a string representing the assignment and score.
	 */
	public String toString() {
		return name + ": " + score;
	}
}
import java.util.ArrayList;

public class GradeKeeper {
	public static void main(String[] args) {
		// Create the list of students
		ArrayList<StudentGrades> students = new ArrayList<StudentGrades>();
		students.add(new StudentGrades("Frank Avalon"));
		students.add(new StudentGrades("Joe Cool"));
		students.add(new StudentGrades("Jane Doe"));
		
		// Create the list of assignment names
		ArrayList<String> assignmentNames = new ArrayList<String>();
		assignmentNames.add("HW1");
		assignmentNames.add("HW2");
		assignmentNames.add("HW3");

		// Set each student's assignments and scores
		int assignmentIndex = 0;
		students.get(0).recordGrade(assignmentNames.get(assignmentIndex), 80);
		students.get(1).recordGrade(assignmentNames.get(assignmentIndex), 92);
		students.get(2).recordGrade(assignmentNames.get(assignmentIndex), 82);
		assignmentIndex++;

		students.get(0).recordGrade(assignmentNames.get(assignmentIndex), 85);
		students.get(1).recordGrade(assignmentNames.get(assignmentIndex), 91);
		students.get(2).recordGrade(assignmentNames.get(assignmentIndex), 84);
		assignmentIndex++;

		students.get(0).recordGrade(assignmentNames.get(assignmentIndex), 86);
		students.get(1).recordGrade(assignmentNames.get(assignmentIndex), 89);
		students.get(2).recordGrade(assignmentNames.get(assignmentIndex), 75);

		for (int i = 0; i < students.size(); i++) {
			System.out.println(students.get(i).toString());
		}
	}

}
/**
 * StudentGrades: Keep all the assignments for a student, up to MAX_GRADES assignments.
 * 
 * @author ghelmer
 *
 */
public class StudentGrades {
  private String name;
  public static final int MAX_GRADES = 20;
  private Assignment[] assignments;
  private int numAssignments;
  
  /**
   * Create a new grade record for the named student.
   * @param name - Name for student
   */
  public StudentGrades(String name) {
	  this.name = name;
	  numAssignments = 0;
	  assignments = new Assignment[MAX_GRADES];
  }
  
  /**
   * Record a grade for the student.  Only MAX_GRADES assignments will
   * be recorded.
   * @param assignmentName - Name for the assignment
   * @param score - Score given for the assignment
   */
  public void recordGrade(String assignmentName, double score)
  {
	  if (numAssignments < MAX_GRADES) {
		  assignments[numAssignments] = new Assignment(assignmentName, score);
		  numAssignments++;
	  } else {
		  System.err.println("Student " + name + ": Could not record grade for assignment " + assignmentName + ": Too many assignments");
	  }
  }

  /**
   * Get the total score for the student.
   * @return Total
   */
  public double getTotalScore() {
	  double totalScore = 0;
	  int i;
	  for (i = 0; i < numAssignments; i++)
		  totalScore += assignments[i].getScore();
	  return totalScore;
  }
  
  /**
   * Get the number of assignments assigned to this student.
   * @return Number of assignments
   */
  public double getNumAssignments() {
	  return numAssignments;
  }

  /**
   * Get the average grade for this student.
   * @return computed average score
   */
  public double getAveScore() {
	  if (numAssignments == 0) return 0;
	  return getTotalScore() / numAssignments;
  }
  
  /**
   * Get a String representing this student and all the assignments.
   */
  public String toString() {
	  int i;
	  String r = "";
	  
	  // Put the name into the string.
	  r = r + name + "\n";
	  
	  // Put each assignment into the string.
	  for (i = 0; i < numAssignments; i++) {
		  r = r + "  " + assignments[i].toString() + "\n";
	  }
	  
	  if (numAssignments > 0) {
		  // Put the total and average score into the string.
		  r = r + "Total score: " + getTotalScore() + " Average: " +
		  	String.format("%.2f", getAveScore()) + "\n";
	  } else {
		  r = r + "No assignments recorded\n";
	  }
	  return r;
  }
  
}

Please post your code in the thread so all can view easily.

Can you explain what your specific problems are?

Here's the code...I'm struggling getting started with all 3 loops. My tutor canceled on me this week so looking for some help wherever I can find it. Thanks!

public class Assignment {
	private double score;
	private String name;
	
	/**
	 * Create the new assignment.
	 * @param name - Name of the assignment
	 * @param score - Score earned for the assignment
	 */
	public Assignment(String name, double score) {
		this.name = name;
		this.score = score;
	}
	
	/**
	 * Get the assignment's score.
	 * @return score
	 */
	public double getScore() {
		return score;
	}
	
	/**
	 * Get the assignment's name.
	 * @return name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Return a string representing the assignment and score.
	 */
	public String toString() {
		return name + ": " + score;
	}
}
*/
public class StudentGrades {
  private String name;
  public static final int MAX_GRADES = 20;
  private Assignment[] assignments;
  private int numAssignments;
  
  /**
   * Create a new grade record for the named student.
   * @param name - Name for student
   */
  public StudentGrades(String name) {
	  this.name = name;
	  numAssignments = 0;
	  assignments = new Assignment[MAX_GRADES];
  }
  
  /**
   * Record a grade for the student.  Only MAX_GRADES assignments will
   * be recorded.
   * @param assignmentName - Name for the assignment
   * @param score - Score given for the assignment
   */
  public void recordGrade(String assignmentName, double score)
  {
	  if (numAssignments < MAX_GRADES) {
		  assignments[numAssignments] = new Assignment(assignmentName, score);
		  numAssignments++;
	  } else {
		  System.err.println("Student " + name + ": Could not record grade for assignment " + assignmentName + ": Too many assignments");
	  }
  }

  /**
   * Get the total score for the student.
   * @return Total
   */
  public double getTotalScore() {
	  double totalScore = 0;
	  int i;
	  for (i = 0; i < numAssignments; i++)
		  totalScore += assignments[i].getScore();
	  return totalScore;
  }
  
  /**
   * Get the number of assignments assigned to this student.
   * @return Number of assignments
   */
  public double getNumAssignments() {
	  return numAssignments;
  }

  /**
   * Get the average grade for this student.
   * @return computed average score
   */
  public double getAveScore() {
	  if (numAssignments == 0) return 0;
	  return getTotalScore() / numAssignments;
  }
  
  /**
   * Get a String representing this student and all the assignments.
   */
  public String toString() {
	  int i;
	  String r = "";
	  
	  // Put the name into the string.
	  r = r + name + "\n";
	  
	  // Put each assignment into the string.
	  for (i = 0; i < numAssignments; i++) {
		  r = r + "  " + assignments[i].toString() + "\n";
	  }
	  
	  if (numAssignments > 0) {
		  // Put the total and average score into the string.
		  r = r + "Total score: " + getTotalScore() + " Average: " +
		  	String.format("%.2f", getAveScore()) + "\n";
	  } else {
		  r = r + "No assignments recorded\n";
	  }
	  return r;
  }
  
}
import java.util.ArrayList;

public class GradeKeeper {
	public static void main(String[] args) {
		// Create the list of students
		ArrayList<StudentGrades> students = new ArrayList<StudentGrades>();
		students.add(new StudentGrades("Frank Avalon"));
		students.add(new StudentGrades("Joe Cool"));
		students.add(new StudentGrades("Jane Doe"));
		
		// Create the list of assignment names
		ArrayList<String> assignmentNames = new ArrayList<String>();
		assignmentNames.add("HW1");
		assignmentNames.add("HW2");
		assignmentNames.add("HW3");

		// Set each student's assignments and scores
		int assignmentIndex = 0;
		students.get(0).recordGrade(assignmentNames.get(assignmentIndex), 80);
		students.get(1).recordGrade(assignmentNames.get(assignmentIndex), 92);
		students.get(2).recordGrade(assignmentNames.get(assignmentIndex), 82);
		assignmentIndex++;

		students.get(0).recordGrade(assignmentNames.get(assignmentIndex), 85);
		students.get(1).recordGrade(assignmentNames.get(assignmentIndex), 91);
		students.get(2).recordGrade(assignmentNames.get(assignmentIndex), 84);
		assignmentIndex++;

		students.get(0).recordGrade(assignmentNames.get(assignmentIndex), 86);
		students.get(1).recordGrade(assignmentNames.get(assignmentIndex), 89);
		students.get(2).recordGrade(assignmentNames.get(assignmentIndex), 75);

		for (int i = 0; i < students.size(); i++) {
			System.out.println(students.get(i).toString());
		}
	}

}

I'm struggling getting started with all 3 loops

Pick one of the loops and explain your problem.

Edited 5 Years Ago by NormR1: n/a

Okay, I need to write a loop that will be inserted into toString in the StudentGrades class. This loops needs to accept student names until the word "Done" is entered.

a loop that will be inserted into toString in the StudentGrades class. This loops needs to accept student names until the word "Done" is entered.

What have you gotten done so far?

Is the code in the loop reading from a user that is entering names?


What I'd suggest is that you write a small simple program to work on how to do this one loop.
Get the logic worked out and then copy that logic into the assignment class and then go on to the next problem.

Edited 5 Years Ago by NormR1: n/a

Yes it's reading from a user. I don't have anything so far, not sure which type of loop to use. Any suggestions? Appreciate any help you can give :)

Since you don't know how many names the user will enter, use a while loop that continues until the user enters the name that will cause the loop to end.


What I'd suggest is that you write a small simple program to work on how to do this one loop.
Get the logic worked out and then copy that logic into the assignment class and then go on to the next problem.

Okay I will work on that. Thank you for your help :)

Edited 5 Years Ago by mfletch: n/a

This article has been dead for over six months. Start a new discussion instead.