Hi, is there any way to return the array from the Checkgpa class back into the Gpa class for an output? I am still new in this topic.
public class Gpa {
private String name;
private char result;
private double gradep;
private int num, sem, ch;
public Gpa (String na, char r, double gp, int n, int s, int c) {
setGpa (na,r,gp,n,s,c);
}
public void setGpa (String na, char r, double gp, int n, int s, int c) {
name = na;
result = r;
gradep = gp;
num = n;
sem = s;
ch = c;
}
public String getName() {
return name;
}
public char getResult() {
return result;
}
public double getGradep() {
return gradep;
}
public int getNum() {
return num;
}
public int getSem() {
return sem;
}
public int getCh() {
return ch;
}
public int countTch() {
int tch=0;
tch += ch;
return tch;
}
public double countGpa() {
double gpa = ch*gradep/countTch();
return gpa;
}
public String display() {
String s = "Student name: "+name+"\n";
s += "Student number: "+num+"\n";
s += "Semester: "+sem+"\n";
s += "Result: "+result+"\n";
s += "Total credit hour: "+countTch()+"\n";
s += "GPA: "+countGpa()+"\n";
s += "\n\n";
return s;
}
}
import java.util.Scanner;
public class Checkgpa {
public static void main (String args[]) {
Scanner sc = new Scanner (System.in);
char result[] = new char[5];
int ch[] = new int[5];
String name;
int num, sem;
double gp;
System.out.print("Enter student name: ");
name = sc.nextLine();
System.out.print("Enter student number: ");
num = Integer.parseInt(sc.nextLine());
System.out.print("Enter current semester: ");
sem = Integer.parseInt(sc.nextLine());
for (int i=0; i<5; ++i)
{
System.out.print ("Enter result for subject"+(i+1)+" D,M,P,F");
result[i] = sc.next().toUpperCase().charAt(0);
if (result[i] == 'D')
gp = 4;
else if (result[i] == 'M')
gp = 3;
else if (result[i] == 'P')
gp = 2;
else if (result[i] == 'F')
gp = 0;
System.out.print ("Enter credit hour for subject"+(i+1)+" 2,3,4");
ch[i] = Integer.parseInt(sc.nextLine());
}
Gpa g = new Gpa();
g.setGpa (name, result[], num, sem, ch[]);
String out = g.display();
System.out.print (out);
}
}