I have been working on an assignment and i am stuck at here. Basically i have 1 class which defines all functions and members.
And another class to initialize and manipulate objects.
Here is my first class code.
public class cyryxStudent_association { String studentID, studentName, studentCourse_level, studentTitle; int course_completed_year; static double registration_fee; double activity_fee; double total_amt; //Default constructor cyryxStudent_association () { studentID = "Null"; studentName = "Null"; studentCourse_level = "Null"; studentTitle = "Null"; course_completed_year = 0; } //Parameterized Constructor cyryxStudent_association (String id, String name, String course_level, String title, int ccy) { this.studentID = id; this.studentName = name; this.studentCourse_level = course_level; this.studentTitle = title; this.course_completed_year = ccy; } //Getters public String getStudentID () { return studentID; } public String getStudentName () { return studentName; } public String getStudentCourse_level () { return studentCourse_level; } public String getStudentTitle () { return studentTitle; } public int getCourse_completed_year () { return course_completed_year; } public double getRegistration_fee () { return registration_fee; } public double getActivity_fee () { return findActivity_fee(registration_fee); } public double getTotal_amt () { return total_amt(registration_fee, activity_fee); } //Setters public void setStudentID (String id) { studentID = id; } public void setStudentName (String name) { studentName = name; } public void setStudentCourse_level (String course_level) { studentCourse_level = course_level; } public void setStudentTitle (String title) { studentTitle = title; } public void setCourse_completed_year (int ccy) { course_completed_year = ccy; } //Find registration fee method public static double findRegistration_fee (String course_level) { if (course_level.equalsIgnoreCase("Certificate")) { registration_fee = 75; } else if (course_level.equalsIgnoreCase("Diploma")) { registration_fee = 100; } else if (course_level.equalsIgnoreCase("Degree")) { registration_fee = 150; } else if (course_level.equalsIgnoreCase("Master")) { registration_fee = 200; } return registration_fee; } //Find activity method public static double findActivity_fee (double registration_fee) { return registration_fee * 0.25; } //Find total amount public static double total_amt (double registration_fee, double activity_fee) { return registration_fee + activity_fee; } //To string method public String toString () { return "ID: "+getStudentID()+"nName: "+getStudentName()+"nCourse Level: "+getStudentCourse_level()+"nTitle: "+getStudentTitle()+"nCourse Completed Year: "+getCourse_completed_year()+"nRegistration Fee: "+getRegistration_fee()+"nActivity Fee: "+getActivity_fee()+"nTotal Amount: "+getTotal_amt (); } }
And here is my second class code.
import java.util.Scanner; public class test_cyryxStudent_association { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int num, i; System.out.println("Welcome!"); System.out.println("nEnter the number of students: "); num = sc.nextInt(); sc.nextLine(); cyryxStudent_association Std[] = new cyryxStudent_association[num]; for (i = 0; i < Std.length; i++) { System.out.println("nEnter ID: "); Std[i].setStudentID(sc.nextLine()); System.out.println("Enter Name: "); Std[i].setStudentName(sc.nextLine()); System.out.println("Enter Course Level [Certificate, Diploma, Degree, Master]: "); Std[i].setStudentCourse_level(sc.nextLine()); System.out.println("Enter Title: "); Std[i].setStudentTitle(sc.nextLine()); Std[i].getRegistration_fee(); Std[i].getActivity_fee(); Std[i].getTotal_amt(); } for (i = 0; i < Std.length; i++) { System.out.println("nStudent " + i + 1 + " Information"); System.out.println("==================================="); Std[i].toString(); } sc.close(); } }
I get an error when values in the for loop. Can someone help me? I’m pretty new to programming and studying java for 2 months now. What am i doing wrong?
Here is my objectives.
- Create an array of objects and get user input for number of objects to be manipulated.
- Read and display array of object values.
Thank you!
Advertisement
Answer
You have to initialize the objects of your array. After the line:
cyryxStudent_association Std[] = new cyryxStudent_association[num];
do a for loop like:
for(i = 0; i<std.length; i++){ std[i] = new cyryxStudent_association(); }