• 01-24-2010, 11:33 PM
Christ90
Hi, i have a problem. I am making a program that i want to calculate an average grade on a student depending on the grades that student gets.. A student will recieve multiple grades during a semester and all will be letter grades.. what I want to make is a method that can calculate the average grade from two variables... one to be int numberOfGrades and the other to be char gradeSum.. if a student gets 3 A's one semester the numberOfGrades would be 3 and the gradeSum will be 'A' + 'A' + 'A'.... How do i let the machine calculate the average of these three grades? Thanks for replies :)
• 01-24-2010, 11:53 PM
Fubarable
It could sort work by adding chars casted to ints, but it's wrong-headed and will be prone to errors. My recommendation is that you not do this, that you treat numbers as numbers, including the sum. Create a method that converts a letter grade to a number, and then add those numbers. My preference would be to use doubles here not ints so your GPA is not limited to whole numbers.
• 01-24-2010, 11:57 PM
Christ90
I have gotten a school assignement that says i have to do it by chars and ints... therefore i cant convert them to ints.. could be a backup sollution but for me but to get a full score i cant do that.. Maybe you could give me an example of how you would cast it?
• 01-25-2010, 12:32 AM
Fubarable
Quote:

Originally Posted by Christ90
I have gotten a school assignement that says i have to do it by chars and ints... therefore i cant convert them to ints.. could be a backup sollution but for me but to get a full score i cant do that.. Maybe you could give me an example of how you would cast it?

No, because it would be wrong. Please print your actual assignment, as I think you're misinterpreting it.
• 01-25-2010, 12:35 AM
Christ90
ssssssssssssssssssssss
• 01-25-2010, 01:13 AM
Lil_Aziz1
This is a pretty good idea to keep track of ones grade. I want to keep track of all my classes. I'm anticipating I can do it like this:

Have an abstract class called Subject.
Code:

```public abstract class Subject {                 private double[] tests;         private double[] quizzes;         private double[] homework;                 public void addTest(double grade) {                 //implementation here         }         public void addQuiz(double grade) {                 //implementation here         }         public void addHW(double grade) {                 //implementation here         }         public abstract void calcGrade();                 public static String convertToLetterGrade(double grade) {                 //implementation here         } }```
Classes like English, Math, Science, etc. would extend the Subject class. What do you guys think?
• 01-25-2010, 01:21 AM
Christ90
That was the same as Fubarable said i think... As I said to him this would be a good backup but to get the assignment correct i will have to use char all the way through or as much as possible.. i know how to do it if i convert after input and before output.. but i wonder if anyone can show me how to do the math with char's... Thanks for the answer though :)
• 01-25-2010, 01:24 AM
Fubarable
I'm still not sure this is correct, but have you tried simply adding the chars?
• 01-25-2010, 01:28 AM
Lil_Aziz1
Fubarable, would the abstract class posted in #6 be a good idea?
• 01-25-2010, 01:28 AM
Christ90
Yeah.. it is possible to add the chars but not sure how to get the answers to also be chars from A-F.. It seems java is using ASCII code or something for chars...
• 01-25-2010, 01:30 AM
Christ90
Quote:

Originally Posted by Lil_Aziz1
Fubarable, would the abstract class posted in #6 be a good idea?

I think it is an good idea to do it that way Lil_Aziz1... but the thing is that my teacher has said a way to do and if its not that way it is not possible to get a full score.. Dont know why but that is the way it is :P
• 01-25-2010, 01:41 AM
Fubarable
Quote:

Originally Posted by Christ90
Yeah.. it is possible to add the chars but not sure how to get the answers to also be chars from A-F.. It seems java is using ASCII code or something for chars...

That's where you cast as a char:
Code:

```    char sum = 'A' + 'B' + 'C' + 'B';     System.out.println((char)(sum/4));```
• 01-25-2010, 01:45 AM
Fubarable
Quote:

Originally Posted by Lil_Aziz1
Fubarable, would the abstract class posted in #6 be a good idea?

Looks fine, though perhaps better is to use Lists rather than arrays, or even an interface rather than abstract class so that the fields can be whatever works best.
• 01-25-2010, 01:46 AM
Christ90
Quote:

Originally Posted by Fubarable
That's where you cast as a char:
Code:

```    char sum = 'A' + 'B' + 'C' + 'B';     System.out.println((char)(sum/4));```

That should work:D will try to put it into the code now :) hope it works :D
• 01-25-2010, 01:59 AM
Christ90
It worked perfectly now:) Thank you very much Fubaruble :)