1,105,229 Community Members

java class help

Member Avatar
sheushen
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
	}
}
Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

yes, you can create a method in your GPA class which take the array as parameter and then print it, but that is very bad OO design.

Member Avatar
sheushen
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i currently have a problem at this part. when i enter any of those character the program crashes. how do i solve it?

for (int i=0; i<5; ++i)
        {
            System.out.print ("Enter result for subject "+(i+1)+" (D,M,P,F): ");
            result = sc.next().toUpperCase().charAt(0);
            if (result == 'D')
                gp[i] = 4;
            else if (result == 'M')
                gp[i] = 3;
            else if (result == 'P')
                gp[i] = 2;
            else if (result == 'F')
                gp[i] = 0;
Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

ok, what is 'result'?
in your first code it was an array, and now you're trying to compare it as being a single char.

if it's still an array, that means it's actually an object which you are treating as a primitive (a single char) then there's your problem.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: