0
public class CollegeCourse
{

   private String courseID;
   private int creditHours;
   private char grade;

   public String getCourse()
   {
      return courseID;
   }

   public int getCredits()
   {
      return creditHours;
   }

   public char getGrade()
   {
      return grade;
   }

   public void setCourse(String cid)
   {
      courseID=cid;
   }

   public void setCredits(int hours)
   {
      creditHours=hours;
   }
   public void setGrade(char g)
   {
      grade=g;
   }
}


public class Student
{

   private int studentID;
   private CollegeCourse[] classes=new CollegeCourse[5];

   public int getID()
   {
      return studentID;
   }
   public void setStudentID(int s)
   {
      studentID=s;
   }

   public CollegeCourse getCourses(int x)
   {
      CollegeCourse course=classes[x];
      return course;
   }

   public void setObject(CollegeCourse obj,int x)
   {
      classes[x]=obj;
   }
}








import java.util.Scanner;

public class InputGrades
{
   public static void main(String[] args)
   {
      String course;
      int credits;
      char grade;
      Student[] grades=new Student[10];
      Scanner input=new Scanner(System.in);

      for (int x=0;x<1;x++)
      {  
         grades[x]=new Student();
         System.out.println("Enter ID for student #"+(x+1));
         grades[x].setStudentID(input.nextInt());
         for(int y=0;y<5;y++)
         {
            CollegeCourse one=new CollegeCourse();

            input.nextLine();

            System.out.println("Enter course ID for class number "+y); 
            one.setCourse(input.nextLine());

            System.out.println("Enter credits for "+y);
            one.setCredits(input.nextInt());

            input.nextLine();
            System.out.println("Enter a grade for "+y);
            one.setGrade(input.nextLine().charAt(0));

            grades[x].setObject(one, y);
         }
      }

      for (int i=0;i<1;i++)
      {
         for (int j=0;j<5;j++)
         {
            System.out.println("Course: "+grades[i].getCourses(j));
           //Cant figure out how to print this
         }
      }
   }
}

It will print the student ID number and that is it. It then prints out a bunch of garbage for the remaining courses displayed under the "j" for loop. I cant seem to figure it out... Any help would be appreciated

2
  • 2
    "A bunch of garbage" is not specific enough for anyone to be able to easily help you. What exactly gets printed when you print "Course: "+grades[i].getCourses(j)? Commented Oct 29, 2014 at 2:26
  • You might be missing a toString() or something, but as furkle said, we need a bit more info. Commented Oct 29, 2014 at 2:28

2 Answers 2

1

You are printing the reference to the CollegeCourse object at the specified index.

You can either write a toString() method for CollegeCourse to print out the attributes you are interested in OR you can alter your existing line:

System.out.println("Course: "+grades[i].getCourses(j).getCourse());
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Yes i was printing the reference, but i didnt know how to dereference. Thank you so much!
0

I'll bet that "bunch of garbage" is the object reference for the array or the object it contains.

Your object needs to override toString() and the container needs to loop over the contents.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.