We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

java class help

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);
	}
}
2
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
sheushen
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

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;
sheushen
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0662 seconds using 2.7MB