Mike Meakin
Ranch Hand
Posts: 88
Hi I'm new here, I'm new to Java too. I have small problem which I'm sure someone here has the abilty to solve.- I need to write a small,simple programe to compute a final percentage. This is made up from the following: assignment1 + assignment2 = 25%, midterm exam=25% and final exam=50%. The results/marks for each were as follows 50,70,70 and 80 respectively. I don't need the main thread or anything just the working out part or a rough idea would be helpful. All I have to go on is the following test code:
System.out.println("The computed percentage is " + grade);

I'm sure someone can think of something, anything even a rough guide of where to start would be great! Many thanks and a happy new year!

marc weber
Sheriff
Posts: 11343
I'm assuming that your stuck on how to define the method, computePercentage().

You already know it should take 4 float values as arguments and return a single float value (the grade). This means it looks something like...

So what about the method body? You know how each of the 4 input values should be weighted. So ask yourself: "What do I need to do mathematically to derive (and return) a final grade from these 4 values?"
[ December 31, 2004: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Yes I have that part but its the next bit I need, I think you referd to it as the 'method body' sorry I really am new. It must be only 5 or 6 lines if you could tell me what they were I'd be very greatfull. I really am a 'noob'.

marc weber
Sheriff
Posts: 11343
Can you describe what you want to do? For example, "I need to take the first number and multiply, divide, add, subtract... Then I need to take that result and multiply, divide, add, subtract..."

Hint: Search the internet for a definition of "weighted mean."
[ December 30, 2004: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Hey Mark, you seem to know what your doing, you need to write a simple progam calculate this:

50+70=120
120/4 =30 /2 =15

70/4 = 17.5

80/2=40

then 15+17.5+40= 72.5

Michael Dunn
Ranch Hand
Posts: 4632
here's a recent(ish) thread that might give you a few ideas

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
...50+70=120
120/4 =30 /2 =15
70/4 = 17.5
80/2=40
then 15+17.5+40= 72.5...

Okay, so that's a calculation to produce your weighted mean. (Not exactly textbook, but it's correct and will work in this context.)

Now, how do you turn this into a method body? In other words, how would you code these calculations in Java -- using float variables a1, a2, m, and f for your input scores, then returning the result?

Try writing some code with this, and post what you come up with.
[ December 31, 2004: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Hey I'm doing that exact same assignment which you posted the link too. And I mean the exact same one. I already have the perfect code for working out the grade, which works once you have typed in the 'final' percentage.
But what I need the code for is the code to find the 'final' percentage.
Assuming that it is made from Assinment1+Asignment2= 25%, Midterm=25% and FinalExam=50%. The actual marks results gained for each were 50,70,70 and 80 respectively.

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
Hey I'm doing that exact same assignment which you posted the link too. And I mean the exact same one.

Yes, this one is rather common. I'm surprised that the numbers don't even change.

Of course, the assignment is designed for you to think through the problem -- not just find a solution posted on the internet. If you're really stuck, post some of the code you have so far (along with a description of where you think the problem might be), and I'm sure someone here can guide you in the right direction.
[ December 31, 2004: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Ok I'm gonna post what I have done so far, the code to find the final grade once the final percentage has been enterd. This is done on JBuilder, The Programe is called 'The Marks Calculator'

class MarksCalculator
{
public static void main(String args[])
{
}
{
char ch = ' ';
if(inv <50)
ch = 'F';
else if(inv < 60.0)
ch = 'D';
else if(inv < 70.0)
ch = 'C';
else if(inv < 80.0)
ch = 'B';
else if (inv < 100.0)
ch = 'A';
return ch;
}
}

------------------------------------------

Ok thats what I have done to find the grade, you enter the percentage, which in this case is 72.5, at the top whrn you execute, the out put given is:
------------------------------------------
Now I need to add a code to find the percentage. Given that it is made from Assignment1 + Assignment2+25%, Midterm=25% and Exam=50%. The actual results gained were 50,70,70 and 80. Which when worked out on paper does give the percentage of 72.5, Like this.... 50 + 70 = 120
120/4=30, /2=15. (assign1+2)

70/4 = 17.5 (midterm)

80/2 = 40 (fExam)

then 15 + 17.5 + 40 = 72.5!

So I need a code which works out this result when 50,70,70 and 80 are enterd.

marc weber
Sheriff
Posts: 11343
Does this help? You just need to complete the computePercentage method body. You already know you're passing in 4 float values (just like you passed a float to computeLetterGrade), and then returning a float (like you returned a char). And you've already outlined the calculations...

Note: What if inv equals 100 (rather than being less than 100)?
[ January 01, 2005: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Hey thanks Marc, Thats more what I'm looking for. I'll give it a try thanks alot!

Mike Meakin
Ranch Hand
Posts: 88
Hey Marc, Its Almost perfect but I am getting 3 errors: one at line 5- char ch ' '; for some reason it says another ; is required? but I put one in and it makes no difference.

Also it says 'illegal start at line 18' this is - float computePercentage(float a1, float a2 ........
I don't suppose you know what I am doing wrong ?
And what is the actual output I am supposed to get ?

marc weber
Sheriff
Posts: 11343
Can you post the exact code that is giving the errors? (My guess is that ch ' ' is missing the assignment equals sign, and that you have an illegal start because of a missing brace or something before the line giving the error; but I would need to see the code.)
[ January 02, 2005: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Ok Give me a few mins......

Mike Meakin
Ranch Hand
Posts: 88
class MarksCalculator

char ch ' '; <-error in this line
if(inv <50.0)
ch = 'F';
else if(inv < 60.0)
ch = 'D';
else if(inv < 70.0)
ch = 'C';
else if(inv < 80.0)
ch = 'B';
else if(inv < 100.0)
ch = 'A';
return ch;
}
float computePercentage(float a1, float a2, float m, float f,){
float weightAssigns = (a1 + a2)/4;
float weightMidterm = (m)/4
float weightExam = (a)/2;
}

public static void main(String args[]){

MarksCalculator m = new MarksCalculator();

float percentage = m.computePercentage(50f, 70.0f, 70.0f, 80.0f):

}
}
}

There is an error in line: float computePercentage(float a1, float a2......

[ January 02, 2005: Message edited by: Michael Munro ]
[ January 02, 2005: Message edited by: Michael Munro ]

marc weber
Sheriff
Posts: 11343

Mike Meakin
Ranch Hand
Posts: 88
Hey Marc, Just one more thing (sorry I must be as thick as 2 short planks)
What exactly is the return statement ?

is it 'return fl'?

Mike Meakin
Ranch Hand
Posts: 88
well I can get it to print out a grade for each component seperately. For example if I type: return a1;
It gives out a D
but i can't get it to do a1,a2,m,f

what should I put for the actual return code ?

marc weber
Sheriff
Posts: 11343
The "return" statement does two things: It ends the method, and it gives back -- returns -- whatever the method is supposed to return. If a method specifies a return type other than "void," then it must contain a return statement.

For example, your method computeLetterGrade returns a char with a return statement of "return ch;"

Your computePercentage method specifies a return type of "float," so you need to return a single value as a float. Hint: You expressed this value above as "15+17.5+40 = 72.5."
[ January 02, 2005: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Marc,So Should it be something like return (a1 + a2, m, f) ???

And then do I need a line of System.out aswell ?

marc weber
Sheriff
Posts: 11343
Don't be afraid to compile and run. You need to try inserting this code and test it. If you get compilation errors, then modify your code based on what the errors tell you. This is the best way to learn Java: Hands-on exploration and verification.

Consider that a return value is a single value. In this case, we want a float. But (a1 + a2, m, f) is three separate values. Ask yourself: What do you want the method computePercentage to do for you? Isn't there a single value that you want to get from this method?

System.out.println() simply displays a String as output at the Command Prompt. This has nothing to do with a method return.

Mike Meakin
Ranch Hand
Posts: 88
Hey Marc, if I put say (a1 + a2 + m + f) all i get is: The corresponding grade is: - with no grade ??? why does it not print a grade ? I need to find the correct code for this.........Iv'e been trying all day! it only prints a grade if i enter 1 component like a1 or a2, thats no good I need it to incude all 4.
Is it return float Percentage? something like that ?

As for the System.out, I was thinking I could have it say The compute Percentage is ' ' on the output........but I can do that later. Now I need to get this return expression right.
[ January 03, 2005: Message edited by: Michael Munro ]

marc weber
Sheriff
Posts: 11343
Again, what do you want the method computePercentage to do for you? What is the value you want to get back from this method?

If you return (a1 + a2 + m + f), you'll get back a "percentage" of 270.0. But you already know that you want 72.5 here. So what is the return statement that will give you 72.5?

(As for why it's not returning a grade, consider what happens when you pass a value of 270.0 into the computeLetterGrade method. This is not less than 50.0, nor is it less than 60.0, nor is it less than...)
[ January 03, 2005: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Yes of course it has to go through the calculatins before it gives 72.5 doesn't it. It maybe a wild gues but should it be like:
return (float Assigns, float Midterm, Float Exam,) ? or is it just like return computePercentage ? I've tried allsorts....

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
...50 + 70 = 120
120/4 = 30/2 =15
70/4 = 17.5
80/2 = 40
then 15 + 17.5 + 40 = 72.5...

Whoops... I see there's an error here that I didn't catch earlier. In the second line, we should be dividing by 2 to get the average assignment score, then dividing by 4 to weight this. So, the second line should be...

(120/2)/4 = 120/8 = 15

Therefore, in computePercentage, we should have...

float weightAssigns = (a1 + a2)/8;

(Another way to look at this is that both assignments together count for 1/4 of the grade. So each assignment individually counts for 1/8 of the grade. So we have 50/8 + 70/8 = 120/8.)
[ January 03, 2005: Message edited by: marc weber ]

marc weber
Sheriff
Posts: 11343
What values are you adding together to get 72.5? These are the values you need to add together to get your return value.

Mike Meakin
Ranch Hand
Posts: 88
Oh yeah I didn't see that...........I still need a return expression though, what could it be ?

Mike Meakin
Ranch Hand
Posts: 88
The code calculates this part already 15 + 17.5+ 40 = 72.5 , but I need the return expression for the calculatio to be completed, although it does not show the answers of 15 ,17.5,40 the return expression should add them together .
so would it be: return (a1 + a2/8 + m/4 + f/2);
Something like that ?

Do I need to add f's to the equasion or floats to each one or somthing like that, it doesn't seem to be picking up all the figures?

[ January 03, 2005: Message edited by: Michael Munro ]

[ January 03, 2005: Message edited by: Michael Munro ]
[ January 03, 2005: Message edited by: Michael Munro ]

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
...so would it be: return (a1 + a2/8 + m/4 + f/2);

Well...
a1 + a2/8 + m/4 + f/2
= 50 + 70/8 + 70/4 + 80/2
= 50 + 8.75 + 17.5 + 40
= 116.25

So again, no grade is returned because this is greater than 100. But all you need to do is divide the first assignment score by 8 as well. Then...
a1/8 + a2/8 + m/4 + f/2
= 50/8 + 70/8 + 70/4 + 80/2
= 6.25 + 8.75 + 17.5 + 40
= 72.5

So yes, the return statement is simply:
return (a1/8 + a2/8 + m/4 + f/2);

Or, in other words...
return (weightAssigns + weightMidterm + weightExam);

But if you're going to do it all in one step on the return line, then you can eliminate those intermediate values of weightXXX, and the entire computePercentage method becomes as simple as...

[ January 03, 2005: Message edited by: marc weber ]

Mike Meakin
Ranch Hand
Posts: 88
Man so I did get it right, just not the exact words........well with just a bit of help from Marc, "the legend" Webber!!!
Thanks marc you must be an expert!

But hey, I still have more to do,!!!
How much would it take to print out a second line of:The percentage is: 72.5
[ January 03, 2005: Message edited by: Michael Munro ]

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
...How much would it take to print out a second line of: The percentage is: 72.5...

You can add as many lines of "System.out.println(...);" as you like.

One other thing you need to take care of: What if the percentage is equal to 100? The way it's coded now, a grade of 'A' will only be returned if the percentage is less than 100.

Mike Meakin
Ranch Hand
Posts: 88
well the it could be <101.0 .........maybe

Like System.out.println("The corresponding percentage is: " + percentage);

Wow! It Works!!!
I'm a genius!
[ January 03, 2005: Message edited by: Michael Munro ]

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
...it could be <101.0...

That would work (assuming that an incorrect value of 100.99 or something isn't passed). It might be easier to read if this were "less than or equal to." That is, if(inv <= 100.0).

Mike Meakin
Ranch Hand
Posts: 88
Hey Marc, I think the next and final part is more simple, it maybe that I have to write a code to add up the scores (50 + 70 + 70 + 80) to get the actual mark and then a System.out......... I'm not sure yet I'll get back later on this evening with another post. Hey Marc I really have learned alot more about this Java stuff since coming to the Moose Salloon and I understand it better too, This is the best Java website I have found! Do you know any other good ones ? I found the 'Cookienest' which seems empty and Programmersheaven.com

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
... This is the best Java website I have found! Do you know any other good ones? ...

http://www.coderanch.com/t/398124/java/java/which-site-you-often-visit

Mike Meakin
Ranch Hand
Posts: 88
Ok the next part is more complicated, it is done in the 'keyboard' class.
This is the description:
The getMark method asks the user to enter two numbers: The first number is the full mark of an assignment, which must be an integer than 0 and the second number is the actual mark obtained by a student. This number is also an integer, which is greater than or equal to 0 and less than or equal to the full mark. Then the program calculates the percentage and returns it as a float (maximum of 100 minimum of 0). If the user enterd a value that is not in the given range, the program should output an error message and ask the user to re-enter it. The output of a test run of the method is shown below. NOTE: The error messages for incorrect input should be identical to those in the example below. This method will use the 'Keyboard Class'.

Ok I'll get the Keyboard class running first, I can do that. Then I can use this test code:
float mark;
mark = MarksCalculator.getMark();
System.out.println("The grade enterd was: " + mark);

The output should be like this:

The maximum grade must be greater than zero
The maximum grade must be greater than zero
Please enter a number less than: 22
-------------------
I'm going to get the 'keyboard class sorted - will be back soon!

marc weber
Sheriff
Posts: 11343
Originally posted by Michael Munro:
...will be back soon!

For the next phase, I suggest you begin a new topic with a title that describes what you're having trouble with. Explain what you've tried, and be as specific as possible about what your question is. Post your code, along with any error messages you're getting.

As I said above, don't be afraid to compile and run. Start small. Add a line or two of code, then compile and run. If it works, add another line or two. If you get compilation errors, then you know where the problem is. Modify your code based on what the errors tell you. This is the best way to learn Java: Hands-on exploration and verification.