bmask
bmask

Reputation: 9

Holding an object into an array

I have two classes; Course and Student classes. One of the fields in the Student class is a course. When i run this program, I fail to hold a course object in the declared array. The code below will give more light where things are failing:

package school;
public class Course
{
    private String courseName;
    private String courseGrade;
    int counter;

    public Course()
    {
        courseName="";
        courseGrade="";
        counter  = 0;
    }
    public Course (String name,String grade)
    {
        courseName=name;
        courseGrade=grade;
        counter=0;
    }
    public String getCourseName()
    {
        return courseName;
    }
    public String getCourseGrade()
    {
        return courseGrade;
    }
    public void setCourse(String name,String grade)
    {
        courseName=name;
        courseGrade=grade;
        counter++;
    }

    @Override
    public String toString ()
    {
        return String.format("%-10s %-10s", courseName,courseGrade);
    }
}
package school;
public class Student
{
    private String firstName;
    private String lastName;
    private int counter;
    private Course [] myCourse;

    public Student ()
    {
        firstName="";
        lastName="";
        myCourse =new Course[2];
        for (int i=0;i<2;i++)
        {
            myCourse [i]= new Course ();
        }
        counter= 0;
    }
    public Student(String first,String last)
    {
        firstName=first;
        lastName=last;
        myCourse =new Course[2];
        for (int i=0;i<2;i++)
        {
            myCourse [i]= new Course ();
        }
        counter= 0;
    }
    public Student(String first,String last, Course [] newCourse)
    {
        firstName=first;
        lastName=last;
        myCourse= new Course [2];
        for (int i=0;i<2;i++)
        {
            myCourse [i]= new Course ();
        }
        counter++;
    }
    public String getFirstName()
    {
        return firstName;
    }
    public String getLastName()
    {
        return lastName;
    }
    public int getNumberOfCourses()
    {
        return counter;
    }
    public void setStudent(String first,String last, Course [] newCourse)
    {
        firstName=first;
        lastName=last;
        myCourse= new Course [2];
        for (int i=0;i<2;i++)
        {
            myCourse [i]= newCourse[i];
        }
        counter++;
    }
    public void setStudent(String first,String last)
    {
        firstName=first;
        lastName=last;
    }

    public void registerCourses(Course  newCourse)
    {
        for (int i=0;i<2;i++)
        {
            myCourse[i]=newCourse;
        }
        counter++;
    }
    public String toString()
    {
        String getString;
        getString =(firstName+" "+lastName);

        for(int i=0;i<2;i++)
        {
            getString=getString+String.format("%n %-10s",myCourse[i]);
        }
        return getString;
    }
}
package school;

import java.util.*;
public class Test
{
 static Scanner cin= new Scanner(System.in);
    public static void main(String[] args)
    {
        String first,last,name,grade;

        Student [] myStudent= new Student [2];
        Course [] myCourse= new Course [2];
        for (int i=0;i<2;i++)
        {
            myStudent[i] = new Student();
            System.out.println("enter name:");
            first=cin.next();
            System.out.println("Last Name:");
            last=cin.next();
            //  For loop within a for loop
            for (int j=0;j<2;j++)
            {
                myCourse [j]= new Course ();
                System.out.println("Course Name:");
                name=cin.next();
                System.out.println("Course Grade:");
                grade=cin.next();
             myCourse [j] = new Course(name,grade);
             myStudent[i].registerCourses(myCourse [j]);
            }
            myStudent [i]=new Student(first, last, myCourse);
        }
        for (int i=0;i<2;i++)
        {
            System.out.println(myStudent[i]);
        }
    }
}

the problem is in the second for loop which is unable to hold course object that i want it to. I want the program to output something similar to the following:

Student One
 MAT             A    
 PHY             B    
 BIO             C    
 CHE             A    
 LAN             A+   
 HIS             A    
Student Two
 COM             C    
 Statistics      A+   
 COM             C    
 Mathematics     D    
 Geography       A    
 Biology         C+   
Student Three
 Biology         C    
 Mathematics     D    
 LAN             A+   
 COM             B    
 Physics         B    
 Physics         B   

However I am unable to obtain that output. In fact i get the following output:

enter name:
Student
Last Name:
One
Course Name:
MAT
Course Grade:
A
Course Name:
COM
Course Grade:
C
enter name:
Student
Last Name:
Two
Course Name:
STA
Course Grade:
A
Course Name:
ENG
Course Grade:
B
Student One


Student Two

Upvotes: 0

Views: 129

Answers (3)

Darpan27
Darpan27

Reputation: 237

Check the registerCourses method inside Student class. It seems its overriding your result on a same index. You can do that with List and keep adding that.

Here is the one I updated Student Class.

    package com.stack;

import java.util.ArrayList;
import java.util.List;

public class Student
{
    private String firstName;
    private String lastName;
    private int counter;
    private List<Course> myCourse;

    public Student ()
    {
        firstName="";
        lastName="";
        myCourse =new ArrayList<Course>();

        counter= 0;
    }
    public Student(String first,String last)
    {
        firstName=first;
        lastName=last;
        myCourse =new ArrayList<Course>();

        counter= 0;
    }
    public Student(String first,String last, List<Course> newCourse)
    {
        firstName=first;
        lastName=last;
        myCourse= newCourse;

        counter++;
    }
    public String getFirstName()
    {
        return firstName;
    }
    public String getLastName()
    {
        return lastName;
    }
    public int getNumberOfCourses()
    {
        return counter;
    }
    public void setStudent(String first,String last, List<Course> newCourse)
    {
        firstName=first;
        lastName=last;
        myCourse= newCourse;

        counter++;
    }
    public void setStudent(String first,String last)
    {
        firstName=first;
        lastName=last;
    }

    public void registerCourses(Course  newCourse)
    {
        myCourse.add(newCourse);
        counter++;
    }
    public String toString()
    {
        String getString;
        getString =(firstName+" "+lastName);

        for(int i=0;i<2;i++)
        {
            getString=getString+String.format("%n %-10s",myCourse.get(i));
        }
        return getString;
    }
}

Test Class:

    package com.stack;

import java.util.*;
public class Test
{
 static Scanner cin= new Scanner(System.in);
    public static void main(String[] args)
    {
        String first,last,name,grade;

        Student [] myStudent= new Student [2];
        List<Course> myCourse= new ArrayList<Course>();
        for (int i=0;i<2;i++)
        {
            myStudent[i] = new Student();
            System.out.println("enter name:");
            first=cin.next();
            System.out.println("Last Name:");
            last=cin.next();
            //  For loop within a for loop
            for (int j=0;j<2;j++)
            {
                System.out.println("Course Name:");
                name=cin.next();
                System.out.println("Course Grade:");
                grade=cin.next();
                Course myC = new Course(name,grade);
             myCourse.add(myC);
             myStudent[i].registerCourses(myC);
            }
            myStudent [i]=new Student(first, last, myCourse);
        }
        for (int i=0;i<2;i++)
        {
            System.out.println(myStudent[i]);
        }
    }
}

Note: For 3 Students, you need to iterate loop until i<3 (not 2).

Upvotes: 0

MayTheSchwartzBeWithYou
MayTheSchwartzBeWithYou

Reputation: 1177

Since you have an undefined number of courses I would suggest using ArrayList and then you do

Also, think what you are doing here.

public void registerCourses(Course  newCourse)
{
    for (int i=0;i<2;i++)
    {
        myCourse[i]=newCourse;
    }
    counter++;
}

You are adding one course and then saying for each position for a course in my array add the new course I was given

You should either use an index, or use an arraylist, or linkedlist etc.

E.g.

public void registerCourses(Course  newCourse, int index)
{

        myCourse[index]=newCourse;

}

or

public void registerCourses(Course  newCourse)
{

        myCourse.push(newCourse); // that is given myCourse is an ArrayList<Course>

}

Upvotes: 0

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140256

you are destroying the first myStudent[i] data when performing a new on the same index at the end of your loop:

    for (int i=0;i<2;i++)
    {
        myStudent[i] = new Student();
        <some code>
        myStudent [i]=new Student(first, last, myCourse);

Upvotes: 6

Related Questions