Results 1 to 5 of 5
Thread: Create a Get/set for an array
- 05-03-2010, 04:48 AM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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;
}
}
- 05-03-2010, 05:14 AM #2
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
Ron, in java there is only on return per method.
should readJava Code:public CollegeCourse[] getCCourse(int arraypos){ return cCourse[arraypos].getCourseID(); return cCourse[arraypos].getCreditHours(); return cCourse[arraypos].getGrade();
the method heading states that it is going to return an object of type CollegeCourseJava Code:public CollegeCourse getCCourse(int arraypos){ return cCourse[arraypos]; }
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.
- 05-04-2010, 01:05 PM #3
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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
- 05-04-2010, 06:20 PM #4
Member
- Join Date
- May 2010
- Posts
- 9
- Rep Power
- 0
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");
- 05-04-2010, 09:39 PM #5
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Constructed, now how to use.
OK so the updated Student code is:
Now I need to create an application that will allow a user to input 10 students each with 5 courses.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){ } }
Here is my attempt:
Points I am unclear on.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){ } }
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
-
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
how create array graphically
By sunolinu in forum AWT / SwingReplies: 3Last Post: 02-12-2010, 07:34 AM -
How to create an array of objects
By redmaverick in forum New To JavaReplies: 7Last Post: 10-19-2009, 02:14 AM -
Create an array of Vectors
By nwboy74 in forum Advanced JavaReplies: 1Last Post: 11-12-2008, 08:00 AM -
How do I create an Array of Images
By wco5002 in forum New To JavaReplies: 3Last Post: 03-21-2008, 03:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks