cccstudent cccstudent - 1 month ago 10
Java Question

Java passing class array into array

I am a student and looking for help with an assignment. Here is the task: Create a CollegeCourse class. The class contains fields for the course ID (for example, “CIS 210”), credit hours (for example, 3), and a letter grade (for example, ‘A’).

Include get() and set()methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get() and set() method for the Student ID number. Also create a get() method that returns one of the Student’s CollegeCourses; the method takes an integer argument and returns the CollegeCourse in that position (0 through 4). Next, create a set() method that sets the value of one of the Student’s CollegeCourses; the method takes two arguments—a CollegeCourse and an integer representing the CollegeCourse’s position (0 through 4).

I am getting runtime errors on the second for loop where I am trying to get the data into the course array. It is asking for both the CourseID and Hours in the same line and regardless of what I respond with it I am getting an error, it almost seems like it is trying to get all the arrays variables at the same time. Here is my code which includes three classes. Any help to send me in the right direction is appreciated as I have spent a ton of time already researching to resolve.

public class CollegeCourse {

private String courseId;
private int creditHours;
private char grade;
public CollegeCourse(String id, int hours, char grade)
{
courseId=id;
creditHours = hours;
this.grade = grade;
}

public void setCourseId(String id)
{
courseId = id;//Assign course id to local variable
}

public String getCourseId()
{
return courseId;//Provide access to course id
}

public void setHours(int hours)
{
creditHours = hours;//Assign course id to local variable
}

public int getHours()
{
return creditHours;//Provide access to course id
}

public void setGrade(char grade)
{
this.grade = grade;//Assign course id to local variable
}

public char getGrade()
{
return grade;//Provide access to course id
}
}


Student Class

public class Student {

final int NUM_COURSES = 5;

private int studentId;
private CollegeCourse courseAdd;//Declares a course object
private CollegeCourse[] courses = new CollegeCourse[NUM_COURSES];

//constructor using user input
public Student(int studentId)
{
this.studentId=studentId;
}

public void setStudentId(int id)
{
studentId = id;//Assign course id to local variable
}

public int getStudentId()
{
return studentId;//Provide access to course id
}

public void setCourse(int index, CollegeCourse course)
{
courses[index] = course;
}

public CollegeCourse getCourse(int index)
{
return courses[index];
//do I need code to return the courseId hours, grade
}
}


InputGrades Class

import java.util.Scanner;
public class InputGrades {

public static void main(String[] args) {

final int NUM_STUDENTS = 2;
final int NUM_COURSES = 3;

Student[] students = new Student[NUM_STUDENTS];
int s;//subscript to display the students
int c;//subscript to display courses
int stId;
int csIndex;
String courseId = "";
int hours = 0;
//String gradeInput;
char grade = 'z';
CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly

Scanner input = new Scanner(System.in);

for(s = 0; s<NUM_STUDENTS; ++s)
{
students[s] = new Student(s);
System.out.print("Enter ID for student #" + (s+1) + ":");
stId = input.nextInt();
input.nextLine();
students[s].setStudentId(stId);
for(c=0; c < NUM_COURSES; ++c)
{
csIndex=c;
System.out.print("Enter course ID #" + (c+1) + ":");
courseId = input.nextLine();
course.setCourseId(courseId);
System.out.print("Enter hours:");
hours = input.nextInt();
input.nextLine();
course.setHours(hours);
String enteredGrade = "";
while(enteredGrade.length()!=1) {
System.out.print("Enter grade:");
enteredGrade = input.nextLine();
if(enteredGrade.length()==1) {
grade = enteredGrade.charAt(0);
} else {
System.out.println("Type only one character!");
}
}
course.setGrade(grade);
students[s].setCourse(csIndex, course);
}
}

for(s = 0; s<NUM_STUDENTS; ++s)
{
System.out.print("\nStudent# " +
students[s].getStudentId());
System.out.println();
for(c=0;c<NUM_COURSES;++c)
System.out.print(students[s].getCourse(c) + " ");
System.out.println();
}
}

}

Answer

After input.nextInt() you need to add one more input.nextLine(); and than you can read grade.

            System.out.print("Enter hours:");
            hours = input.nextInt();
            input.nextLine();
            course.setHours(hours);

Why it is needed? See this question: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

You should add a very simple length validation when you input the grade:

            String enteredGrade = "";
            while(enteredGrade.length()!=1) {
                System.out.print("Enter grade:");
                enteredGrade = input.nextLine();
                if(enteredGrade.length()==1) {
                    grade = enteredGrade.charAt(0);
                } else {
                    System.out.println("Type only one character!");
                }
            }

so the full main class code:

import java.util.Scanner;

/**
 * Created by dkis on 2016.10.22..
 */
public class App {
    public static void main(String[] args) {

        final int NUM_STUDENTS = 10;
        final int NUM_COURSES = 5;

        Student[] students = new Student[NUM_STUDENTS];
        //String name;
        int s;//subscript  to display the students
        int c;//subscript to display courses
        int stId;
        int csIndex;
        String courseId = "";
        int hours = 0;
        char grade = 'z';
        CollegeCourse course = new CollegeCourse(courseId,hours, grade);//not sure if I am handling this correctly

        Scanner input = new Scanner(System.in);

        for(s = 0; s<NUM_STUDENTS; ++s)
        {
            students[s] = new Student(s);
            System.out.print("Enter ID for student #" + s+1 + ":");
            stId = input.nextInt();
            input.nextLine();
            students[s].setStudentId(stId);
            for(c=0; c < NUM_COURSES; ++c)
            {
                //CollegeCourse course = students[s].getCourse(c);
                csIndex=c;
                System.out.print("Enter course ID#" + c+1 + ":");
                courseId = input.nextLine();
                course.setCourseId(courseId);
                System.out.print("Enter hours:");
                hours = input.nextInt();
                input.nextLine();
                course.setHours(hours);

                String enteredGrade = "";
                while(enteredGrade.length()!=1) {
                    System.out.print("Enter grade:");
                    enteredGrade = input.nextLine();
                    if(enteredGrade.length()==1) {
                        grade = enteredGrade.charAt(0);
                    } else {
                        System.out.println("Type only one character!");
                    }
                }
                course.setGrade(grade);
                students[s].setCourse(csIndex, course);
            }
        }

        for(s = 0; s<NUM_STUDENTS; ++s)
        {
            System.out.print("\nStudent# " +
                    students[s].getStudentId());
            for(c=0;c<NUM_COURSES;++c)
                System.out.print(students[s].getCourse(c) + " ");
            System.out.println();
        }
    }
}
Comments