Question
Hi, I am just beginning to learn Java and I have a project in which I have to create a GUI for calculating GPA of
Hi, I am just beginning to learn Java and I have a project in which I have to create a GUI for calculating GPA of a semester. I am having trouble with passing an array of a pre-filled lists of course names (5 of them) into another method so I can display each value of the array into multiple labels. Please help me on this. I am including a portion of my codes where I am stuck so you can better understand the issue
// main class
public class PAssign9 extends Application {
// Create an array of labels for dispalying course names
Label[] courseNames = {new Label(), new Label(), new Label(), new Label(), new Label()};
// Create a Semester instance
Semester semester = new Semester();
// Print out each course name into separated labels within the array
for (int i = 0; i < courseNames.length; i++) {
courseNames[i].setText(semester.getCourseName());
}
}
// Semester class
class Semester {
private int numOfCourse = 5;
private String[] courseName; // Declare a String array to store the course names
public Semester() { // Convenient constructor this.courseName = new String[numOfCourses]; // Create new array of course names courseName[0] = "CSCI 1302"; courseName[1] = "ITEC 2000"; courseName[2] = "CSCI 2070"; courseName[3] = "ITEC 2530"; courseName[4] = "MATH 1161"; }
// Here I created the getCourseName() method so I can access it in the main class, and I was able to print the array values into the labels individually but it only prints the last value, which is courseName[4]
public String getCourseName() { String course = ""; if (course.equals(courseName[0])) { return courseName[0]; } else if (course.equals(courseName[1])) { return courseName[1]; } else if (course.equals(courseName[2])) { return courseName[2]; } else if (course.equals(courseName[3])) { return courseName[3]; } else { return courseName[4]; } }
}
Please suggest effient ways to make this work. Thank you and feel free to ask me if you have questions regarding this problem
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started