Its been a while since I have had Java..I am getting stuck tying to write a program that will add up grades and give and average...and throw an exception when a grade below 0 is added or above 100....can anyone spin me in the write directions?
throw an exception in code
Collapse
X
-
Tags: None
-
Are the grades going to be entered through the console?Originally posted by DlangdamanIts been a while since I have had Java..I am getting stuck tying to write a program that will add up grades and give and average...and throw an exception when a grade below 0 is added or above 100....can anyone spin me in the write directions?
What do you have so far? -
im not sure..i would like them to be added through a small applet but I have no idea how to do that anymore...so if the console is the easiest way ....I have not yet started to actually put this together...i am looking for some direction on how to pull this off in the cleanest yet most basic wayComment
-
Most basic way is to do that through the console.Originally posted by Dlangdamanim not sure..i would like them to be added through a small applet but I have no idea how to do that anymore...so if the console is the easiest way ....I have not yet started to actually put this together...i am looking for some direction on how to pull this off in the cleanest yet most basic way
Have a look at the java.util.Scann er class then try some code and post if you get any problems.Comment
-
here is what i have working....
[CODE=java] import javax.swing.JOp tionPane;
public class Grades
{
public Grades()
{
float grades [] = new float[5];
float ave, sum = 0.0f;
String students[] = {"Tim", "Mary", "Jen","Dere k", "John"};
String sgrades, sResult;
int i;
for(i = 0; i < grades.length; ++i)
{
sgrades = JOptionPane.sho wInputDialog(nu ll,
"Enter Students Grade: ", students[i],1);
grades[i] = Float.parseFloa t(sgrades);
}
for(i = 0;i < grades.length; ++i)
{
sum += grades[i];
}
ave = sum / 5.0f;
sResult = "The Class average is " + Float.toString( ave);
JOptionPane.sho wMessageDialog( null,sResult);
}
public static void main(String args[])
{
new Grades();
System.exit(0);
}
}
[/CODE]
what i need is an exception to happen if some one enters a grade above 100 or below zero...how would I do that?Comment
-
See how the code looks better under code tags? Don't forget them next time. Now there are many built in Exceptions in Java and you can make your own kinds of exceptions. So the next thing you want to do now is to create your own type of Exception. Then all you need is a simple if test that throws the exception if the grade is greater than 100. Have you read about Exceptions yet?Originally posted by Dlangdamanhere is what i have working....
[CODE=java] import javax.swing.JOp tionPane;
public class Grades
{
public Grades()
{
float grades [] = new float[5];
float ave, sum = 0.0f;
String students[] = {"Tim", "Mary", "Jen","Dere k", "John"};
String sgrades, sResult;
int i;
for(i = 0; i < grades.length; ++i)
{
sgrades = JOptionPane.sho wInputDialog(nu ll,
"Enter Students Grade: ", students[i],1);
grades[i] = Float.parseFloa t(sgrades);
}
for(i = 0;i < grades.length; ++i)
{
sum += grades[i];
}
ave = sum / 5.0f;
sResult = "The Class average is " + Float.toString( ave);
JOptionPane.sho wMessageDialog( null,sResult);
}
public static void main(String args[])
{
new Grades();
System.exit(0);
}
}
[/CODE]
what i need is an exception to happen if some one enters a grade above 100 or below zero...how would I do that?Comment
-
Thank you for correcting the tags..i wasnt sure how to do that...as far as exceptions I am fairly new to them. i have been struggling on how or where to implement the exception in my codeComment
-
Well the exceptions tutorial is fairly short. If you go through it you should be able to write this in no time. Do that and post back if you get problems.Originally posted by DlangdamanThank you for correcting the tags..i wasnt sure how to do that...as far as exceptions I am fairly new to them. i have been struggling on how or where to implement the exception in my codeComment
-
Im sorry to bother you guys...I need someone to get me started..I cannot figure out how to imply an exception throw and catch into the code I already have...can anyone just show me where to insert? and first lineComment
-
Did you finish that small Exceptions tutorial that I gave you?Originally posted by DlangdamanIm sorry to bother you guys...I need someone to get me started..I cannot figure out how to imply an exception throw and catch into the code I already have...can anyone just show me where to insert? and first lineComment
-
Hi,Originally posted by DlangdamanIts been a while since I have had Java..I am getting stuck tying to write a program that will add up grades and give and average...and throw an exception when a grade below 0 is added or above 100....can anyone spin me in the write directions?
please see the updated code with required exception handling mechanism.
<Removed>
regards,
Kishore MComment
-
Please read the site guidelines concerning posting full code solutions.Originally posted by KishoreMHi,
please see the updated code with required exception handling mechanism.
<Removed>
regards,
Kishore MComment
-
I apologize for making such a stink on the site. I did browse through the tutorial and I will do so again today. I was just wondering on how to place it in my code. Thanks againComment
-
Don't worry, you didn't do much wrong. If there's any part of that tutorial that you don't understand you can post here and get help.Originally posted by DlangdamanI apologize for making such a stink on the site. I did browse through the tutorial and I will do so again today. I was just wondering on how to place it in my code. Thanks againComment
-
basically like I stated...my only issue is placement. I know that I must create a method that calls the exception. i need to make the exception and make it do what I need it to do in an if statement
try
if i>100 or i < 0
then
Throw
blah blah
catch?
is that the order or close?Comment
Comment