Results 1 to 5 of 5
  1. #1
    mechdesignron is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Create a Get/set for an array

    I am a new to programming and this is for a class, there may be a better way to accomplish the task but I do want to understand what is being taught.

    I am to create a class CollegeCourse, with three fields Course ID, credit Hours and Grade. & create get & set for each field; OK did that.
    Then I am to create a class Student that has 2 fields, student ID and an array of 5 CollegeCourse objects. I need to create a get method that takes the array position (int) and returns the appropriate college course. also I need to create a set method that takes arguments to populate a course (this would include the array position) Below is my attempt at the problem.(I only tried to set the array position and one field and thats not working)

    Thank you for any assistance.
    Ron

    public class CollegeCourse{
    private String courseID;
    private int creditHours;
    private char grade;

    CollegeCourse(String id, int hrs, char grade){
    id = courseID;
    hrs = creditHours;
    }

    public String getCourseID(){
    return courseID;
    }
    public void setCourseID(String id){
    courseID = id;
    }
    public int getCreditHours(){
    return creditHours;
    }
    public void setCreditHours(int hrs){
    creditHours = hrs;
    }
    public char getGrade(){
    return grade;
    }
    public void setGrade(char grade){
    }
    }

    public class Student{
    private int studentID, arraypos;
    private CollegeCourse[] cCourse = new CollegeCourse[5];
    String id;

    Student(int SID, CollegeCourse[] cse){
    studentID = SID;
    cCourse = cse;
    }

    public int getStudentID(){
    return studentID;
    }
    public void setStudentID(int SID){
    studentID = SID;
    }
    public CollegeCourse[] getCCourse(int arraypos){
    return cCourse[arraypos].getCourseID();
    return cCourse[arraypos].getCreditHours();
    return cCourse[arraypos].getGrade();
    }
    public CollegeCourse[] setCCourse(int arraypos, String id){
    cCourse[arraypos].setCourseID(id)=id;
    }
    }

  2. #2
    gbtimmon is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Ron, in java there is only on return per method.
    Java Code:
    public CollegeCourse[] getCCourse(int arraypos){
    return cCourse[arraypos].getCourseID();
    return cCourse[arraypos].getCreditHours();
    return cCourse[arraypos].getGrade();
    should read
    Java Code:
    public CollegeCourse getCCourse(int arraypos){
    return cCourse[arraypos];
    }
    the method heading states that it is going to return an object of type CollegeCourse

    public CollegeCourse getCollegeCourse(int arrayPos) {}
    [visibility][return type][method name]([parameters]){}

    having it return CollegeCourse[] will make it return an entire array of CollegeCourse which you don't want, since your trying to get a specific spot in the array, which is only one CollegeCourse.

    the return CollegeCourse(); method will return the object address of the object you want.

    so in the class you can call getCollegeCourse(); from which you can then assign a varible...

    CollegeCourse firstCourse = Student.getCollegeCourse(1);

    or to directly call any CollegeCourse related methods,

    System.out.println(student1.getCollegeCourse(1).ge tCreditHours());

    this will print only the number of hours for the first course in student1's CollegeCourse array. Simple right?
    Last edited by gbtimmon; 05-03-2010 at 05:17 AM.

  3. #3
    mechdesignron is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Need the set method

    How do you create a set method. In the assignment it asks to create a set method that takes two arguments an integer that specifies the position and the value of the college course.

    Thank you

  4. #4
    gbtimmon is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default

    Well, if you were working with the class student and you wanted to add a CollegeCourse to his CollegeCourse[] array, you would need to pass an entire instance of the class CollegeCourse into a setCollegeCourse method.

    thus in class student

    public void setCollegeCourse(int position, CollegeCourse newCourse){
    //assign your variables here, should be one line.
    }

    what if you wanted to edit a single attribute of one of Student's courses through student?

    student.getCourse(1).setSomething("blah blah");

  5. #5
    mechdesignron is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Constructed, now how to use.

    OK so the updated Student code is:
    Java Code:
    public class Student{
    	private int studentID, arraypos;
    	private CollegeCourse[] cCourse = new CollegeCourse[5];
    	String id;
    	
    	Student(int SID, CollegeCourse[] cse){
    		studentID = SID;
    		cCourse = cse;
    	}
    		
    	public int getStudentID(){
    		return studentID;
    	}
    	public void setStudentID(int SID){
    		studentID = SID;
    	}
    	public CollegeCourse getCCourse(int arraypos){
    		return cCourse[arraypos];
    	}
    	public void setCollegeCourse(int arraypos, CollegeCourse cCourse){
    	}
    }
    Now I need to create an application that will allow a user to input 10 students each with 5 courses.

    Here is my attempt:
    Java Code:
    import javax.swing.JOptionPane;
    
    class InputGrades{
    	public static void main (String args[]){
    	Student nowStudent;
    	nowStudent = getStudentData();
    	displayStudent(nowStudent);
    }
    	
    public static Student getStudentData(){
    		
    	String courseid, id, sid, hours, gradestring;
    	int creditHours, i, SID, n, hrs;
    	char grade;
    	Student instudent = new Student();
    	
    	for(n = 0; n < 10; ++n){
    
    	
    	sid = JOptionPane.showInputDialog(null, "Enter the student ID for student "+ (n+1), JOptionPane.INFORMATION_MESSAGE);
    	SID = Integer.parseInt(id);
    	instudent.setStudentID(SID);
    	
    		for(i = 0; i < cCourse.length; ++i){
    		
    			instudent.setCollegeCourse(i, cCourse);
    			
    			id = JOptionPane.showInputDialog(null, "Enter the course ID for course " + (i+1)+ " for student "+ (n+1), JOptionPane.INFORMATION_MESSAGE);
    			
    			instudent.getcCourse(i).setCourseID(id);
    		
    			hours = JOptionPane.showInputDialog(null, "Enter the course hours for course " + (i+1)+ " for student "+ (n+1), JOptionPane.INFORMATION_MESSAGE);
    			hrs = Integer.parseInt(hours);
    			instudent.getcCourse(i).setCreditHours(hrs);
    		
    			gradestring = JOptionPane.showInputDialog(null, "Enter the grade for course " + (i+1)+ " for student "+ (n+1), JOptionPane.INFORMATION_MESSAGE);
    			grade = Char.parseChar(gradestring);
    			instudent.getcCourse(i).setGrade(grade);
    			
    			return instudent;
    		}
    	}
    	}
    public static void displayStudent(Student instudent){
    }
    }
    Points I am unclear on.
    If my set constructor is setting variables to the entire object, how do I enter values?

    If I am creating 10 instances of student do I need my program to create 10 unique names of the new object? How. Should I create the new students()/ new CollegeCourse[] in a loop?

    This is very frustrating I can find lots of examples where the array is pre-populated then the application pulls the instances but there is very little with examples on working with/populating objects in an array.

    Again Thank you for any assistance.

Similar Threads

  1. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  2. how create array graphically
    By sunolinu in forum AWT / Swing
    Replies: 3
    Last Post: 02-12-2010, 07:34 AM
  3. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 02:14 AM
  4. Create an array of Vectors
    By nwboy74 in forum Advanced Java
    Replies: 1
    Last Post: 11-12-2008, 08:00 AM
  5. How do I create an Array of Images
    By wco5002 in forum New To Java
    Replies: 3
    Last Post: 03-21-2008, 03:45 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
  •