a java programm with try and catch mechanism

a java programm with try and catch mechanism

Estyzo

Senior Member
Joined
Jul 24, 2011
Posts
158
Reaction score
39
msaada kwa swali hapo chini.

Write Java class(es) which accept ten (10) integer values ranging between zero (0) and a
hundred (100), calculates the average mark, assigns an appropriate grade to the average mark
and outputs the average mark with the corresponding grade (use the university convention of
grading).
In your source codes;
• The user has a choice to either input values as command line arguments or the
program prompts for the input with the Scanner utility.
• If he/she chooses one method of input, the other should be skipped.
• There should be mechanism(s) to check that the integer values entered are valid that
is, they range between zero and a hundred inclusively and there are ten integer values
entered.
• There should be a feedback mechanism to inform the user if the values entered are
valid or not.
• Whenever necessary, use exception handling technique.
• Adhere to the fundamental object oriented concepts.
 
Let us start with stepwise refinement as we develop the pseudo code for the solution....
 
Required :
1. accept ten (10) integer s btn 0 and 100
2. calculate the average mark, (assuming the integers entered are marks)
3. assign an appropriate grade to the average mark
4 Output the average mark with the corresponding grade

These form the main logical chunks for your solution
Next we have to add more d3tails to th3 chunks ....
 
Each of the four logical chunks will form a method (or function)
The four methods will b3 called and manipulated by the main method ....
 
Adding more details to the logical chunks continues as follows

1. accept ten (10) integers
_ EITHER read as command line parameters
- OR use the scanner utility to read 10 numbers
- each number read must be btn 0 and 100 otherwise prompt with error

2. calculate the average mark, (assuming the integers entered are marks)
- take the 10 integers read and find their average

3. assign an appropriate grade to the average mark

4 Output the average mark with the corresponding grade
 
You are told to use OO concepts, so I think we can use 2 classes ( one could also be enough- if i get time can show you how)

A well encapsulated class has several methods that do one thing only and do so well enough

OO encourages instantiation of objects and using methods they contain....
 
Class Grades
{
getMarks();

calcAverage();

setGrade();

getMarkNGrade();


}

Class Test
{
private static void main();
}
 
Let me stop here for today ...

If you already have some knowledge you should be able to proceed and finish implementing the class... can you do that? You learn driving by actually taking the car to the road, same with programming....

If you have any questions to further your understanding, you are welcome to seek more pointers ...
 
msaada kwa swali hapo chini.

Write Java class(es) which accept ten (10) integer values ranging between zero (0) and a
hundred (100), calculates the average mark, assigns an appropriate grade to the average mark
and outputs the average mark with the corresponding grade (use the university convention of
grading).
In your source codes;
• The user has a choice to either input values as command line arguments or the
program prompts for the input with the Scanner utility.
• If he/she chooses one method of input, the other should be skipped.
• There should be mechanism(s) to check that the integer values entered are valid that
is, they range between zero and a hundred inclusively and there are ten integer values
entered.
• There should be a feedback mechanism to inform the user if the values entered are
valid or not.
• Whenever necessary, use exception handling technique.
• Adhere to the fundamental object oriented concepts.

hili swali la shule? lijibu, andika code ukikwama au ikigoma, omba msaada.. uonyeshe ulipofikia
 
Let me stop here for today ...

If you already have some knowledge you should be able to proceed and finish implementing the class... can you do that? You learn driving by actually taking the car to the road, same with programming....

If you have any questions to further your understanding, you are welcome to seek more pointers ...
You do injustice to our country by doing someone's assignment. look how lazy he is, he just posted and let us answer it!
 
You do injustice to our country by doing someone's assignment. look how lazy he is, he just posted and let us answer it!

umeona ee, njemba anatupia swali la assignment zima na kulitelekeza akisubiri watu washuke code, huu ni uvivu wa hali ya juu, angeonesha kwanza uwezo wake ulipofikia ili watu wapate moyo wa kumsadia, hapa ndo unapoona jinsi gani watu wanasomea fani zisizo zao
 
nimejaribu hilo swali kipengere cha kwanza ndio utata (command line )kodi nimeifanya na ina run.
 
thanks htuc though nimefika mbele zaidi na kuiandika kodi but kipengele hicho cha kommand line ndio kinasumbua kama unaweza andika an example ya ku enter number kwa command line argument.
 
thanks htuc though nimefika mbele zaidi na kuiandika kodi but kipengele hicho cha kommand line ndio kinasumbua kama unaweza andika an example ya ku enter number kwa command line argument.
 
Last edited by a moderator:
acha mbwembwe. onyesha juhudi. ujanja ujanja hausaidii. update post yako useme umefanya nini!
 
acha mbwembwe. onyesha juhudi. ujanja ujanja hausaidii. update post yako useme umefanya nini!

sio kwamba nimeshindwa kabisa kodi hii hapo chini na inaru kabisa bro.


///////////////////////////////////////////////////////////////////
import java.util.Scanner;

public class grades {

public static void main(String[] args) {
int grade = 0;
for(int i=0; i < 10; i++){
grade += validInt("Enter mark "+(i+1) +"[0-100]: ",0,100);
}
grade /= 10;
System.out.print("You earned a "+grade+" ");
if(grade >= 70 && grade <= 100)
System.out.println("A");
else if(grade >= 60 && grade <= 69)
System.out.println("B+");
else if(grade >= 50 && grade <= 59)
System.out.println("B");

else if(grade >= 40 && grade <= 49)
System.out.println("C");
else if(grade >= 30 && grade <= 39)
System.out.println("D");

else
System.out.println("E");
}

public static int validInt(String prompt,int min,int max){
Integer result = null;
while(true){
System.out.print(prompt);
Scanner in = new Scanner(System.in);
if(in.hasNextInt())
result = in.nextInt();
if(result != null && result >= min && result <= max)
return result;
else
System.out.println("Invalid Number!...enter again");
}
}
}
///////////////////////////////////////////////////////////////
 
na kuiweka try and catch block (exception handling) to CTO , htuc
 
Last edited by a moderator:
Back
Top Bottom