Results 1 to 4 of 4
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Help with custom class array

    I am writing a program with 2 custom classes and a tester. The first class called
    CollegeCourse 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. I got that done fine, here's my code:
    Java Code:
    public class CollegeCourse
    {
    	 private String courseId;
    	 private int creditHours;
    	 private char grade;
    	
    	public CollegeCourse(String i, int h, char g)
    	{
    		this.courseId = i;
    		this.creditHours = h;
    		this.grade = g;
    	}
    	
    	public String courseId(){return this.courseId;}
    	public int creditHours(){return this.creditHours;}
    	public char grade(){return this.grade;}
    	
    	public void setcourseId(String i){this.courseId = i;}
    	public void setcreditHours(int h){this.creditHours = h;}
    	public void setgrade(char g){this.grade = g;}
    	
    }
    Here's my problem. My second class. I am supposed to Create a Student class containing an ID number and an array of five CollegeCourse objects. Then create a get() and set()
    methods for the student ID number also create a get() method that returns one of the
    students CollegeCourses; the method takes an integer argument and returns the
    CollegeCourse and that position (0 through 4). I must be messed up a little conceptually,
    because I thought this line:
    Java Code:
    private CollegeCourse[] courseArray = {"CSP 2011","CS 1010","CSI 1012","ECO 2022",
    "MTH 0907"};
    was my array of 5 CollegeCourse objects. The compiler is giving me an incompatible type
    error with that line.

    I am also getting an error right after the brackets for the array in my setter method for
    setCourseArray.

    here is the complete code for my student class:
    Java Code:
    public class Student
    {
    	private int studentId;
    	private CollegeCourse[] courseArray = {"CSP 2011","CS 1010","CSI 1012","ECO 2022","MTH 0907"};
    
    	
    	public Student(int s, CollegeCourse[] c)
    	{
    		this.studentId = s;
    		this.courseArray = c;	
    	}
    	
    	public int studentId(){return this.studentId;}
    	public CollegeCourse[] courseArray(){return this.courseArray;}
    	
    	public void setstudentId(int s){this.studentId = s;}
    	public void setcourseArray(CollegeCourse[]){this.courseArray = c;}
    }
    thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with custom class array

    Your courseArray variable is declared as an array of CollegeCourse objects but is initialized as an array of String, and this is not allowed. If you want to initialize the array with CollegeCourse objects, you'll need to call the constructor:

    Java Code:
    private CollegeCourse[] courseArray = {new CollegeCourse("CSP 2011", 4, 'A'), new CollegeCourse("CS 1010", 3, 'B') };
    If you think that the CollegeCourse objects should be declared without the grade char, then you'll need to give it a constructor that accepts just 2 parameters, a String and an int.

  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Help with custom class array

    To take the letter grade as input in my tester class could I do this, or something like this:
    Java Code:
    private CollegeCourse[] courseArray = {new CollegeCourse("CSP 2010", 4, CollegeCourse.setgrade())};
    I am only using one object in this example but in the actually program I will use the 5.
    thanks!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with custom class array

    Quote Originally Posted by mwr1976 View Post
    To take the letter grade as input in my tester class could I do this, or something like this:
    Java Code:
    private CollegeCourse[] courseArray = {new CollegeCourse("CSP 2010", 4, CollegeCourse.setgrade())};
    I am only using one object in this example but in the actually program I will use the 5.
    thanks!
    And when you try to compile this, what happens? ;)

Similar Threads

  1. Array of custom type
    By whytefish in forum New To Java
    Replies: 8
    Last Post: 04-17-2011, 06:09 PM
  2. JList with custom Array
    By sony144 in forum Advanced Java
    Replies: 1
    Last Post: 10-09-2010, 07:35 PM
  3. Custom class in an array?
    By bugmenot in forum New To Java
    Replies: 5
    Last Post: 04-15-2009, 01:10 AM
  4. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 03:12 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •