Hi, I am just beginning to learn Java and I have a project in which I have to cr
ID: 3816262 • Letter: H
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 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
Explanation / Answer
Answer:
I am not sure if this is what you are trying to achieve. Send the course names and print on the label.
The issue with your code is you have initialized String course = ""; in getCourseName() function. Then immediately you are checking if the course value matches with any of the array values in courseName. Instead what you could do is pass the index to this getCourseName(int arInd) function as parameter. Then use that index value to pass the appropriate array index value. I have provided the working code below:
pAssign9.java:
package passign9;
import java.awt.Frame;
import java.awt.Label;
//import javafx.application.Application;
public class PAssign9 {
public static void main(String[] args) {
// TODO code application logic here
int count = 100;
Frame f= new Frame("Label Example");
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(i));
courseNames[i].setBounds(50,count, 100,30);
count = count + 50;
f.add(courseNames[i]);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
}
Semester.java:
package passign9;
class Semester {
private int numOfCourses = 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";
}
public String getCourseName(int arInde) {
return courseName[arInde];
}
}
Note: There is also another way. You can return the String array in getCourseName() function. The passed array will the coursenames. In the PAssign9 class you can then loop through the returned value from getCourseName() function and display them in the label.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.