How to use this method in java? Using a method in java?
-
hello there, i been having a problem in my grading program and i need some feedback some help setting up a method so it will return to its original value...wat i need to do is to remove the nested if expression for letter grade calculation from the main method which i believe is in the middle of my program and i have to create a separate method for LetterGrade.the method LetterGrade needs one percent argument and returns one letter grade...can anyone help me plz...any help with be aprreciated thank you... import java.util.*; public class GradeList2 { public static void main(String[] args) { LetterGrade (); // main method //tell the user the name of this program System.out.println ("This Program calculates grades for a class of students"); Scanner input = new Scanner(System.in); double class_average, bestPercent=0.0, lowPercent=101.0,sum=0.0; int Total_Points = 0, numStudents; int students, points, grade; int smallest = 0, largest = 0; // Prompt user to enter tge number of students System.out.println(" Enter number of students?"); numStudents = input.nextInt(); int[] student_points = new int [numStudents]; double[] percent =new double [numStudents]; char [] Grade = new char [numStudents]; // Prompt user to enter the total possible points in this semester System.out.println(" Enter the the total possible points"); Total_Points =input.nextInt(); // Prompt user to enter each students points System.out.println(" Enter each students points"); for ( int i=0; i < numStudents; i++){ System.out.println(" Please enter each student points"); System.out.println(" "); //ask the user to enter each point for each student System.out.print(" Enter points for student " + (i+1)+" " ); student_points[i] = input.nextInt(); // check that the user total possible points are with in total possible points if ( Total_Points < student_points[i]){ System.out.print( "Sorry that is over the total possible, try again"); i--; } } for( int i=0; i< numStudents; i++){ // students percent percent[i]= ((double) student_points[i] / Total_Points * 100); } public static String LetterGrade (String percent ){ if (percent >= 90) {Grade='A';} else if (percent>= 80) {Grade='B';} else if (percent >= 70) {Grade='C';} else if (percent >= 60) {Grade = 'D';} else {Grade = 'F' ;} return percent; String studentGrade = LetterGrade (percent); } } for( int i=0; i< numStudents; i++){ // calculate the sum of the averages sum = percent[i] + sum; //calculate best percent if (bestPercent <= percent[i]){ bestPercent = percent[i]; } //calculate lowest percent if (lowPercent >= percent[i]){ lowPercent = percent[i]; } } // calculate class average
-
Answer:
Use a case statement.
Misiekj at Yahoo! Answers Visit the source
Other answers
Not bad for a first stab at it, but you're guessing...not learning. A method definition can't be nested inside another method, so the whole thing, starting with "public static String letterGrade...", up to the "return percent;" needs to be moved outside of main(). You need to add a closing brace after the return statement to end the method body, too. You are using char values for letter grades, so the return type from letterGrade() should be char, not String. The Grade variable should be declared in your method as a local variable, and shouldn't be capitalized (Java convention is to capitalize class and interface names, not methods or variables.) Try adding "char grade;" to the front of your method. The percent argument should be an int or a double, not a string. You are comparing to int constants, so try int first. The percent argument is the input to your function. The calculated grade char is your output. The final statement should be "return grade;", not "return percent;"
husoski
Here you are. Hope this is what you're after. import java.util.*; // for scanner public class GradeList2 { public static void main(String[] args) { // partially by cbc // main method //tell the user the name of this program System.out.println ("This Program calculates grades for a class of students"); Scanner input = new Scanner(System.in); double class_average, bestPercent=0.0, lowPercent=101.0,sum=0.0; int Total_Points = 0, numStudents; int students, points, grade; int smallest = 0, largest = 0; // Prompt user to enter tge number of students System.out.println(" Enter number of students?"); numStudents = input.nextInt(); int[] student_points = new int [numStudents]; double[] percent =new double [numStudents]; char [] Grade = new char [numStudents]; // Prompt user to enter the total possible points in this semester System.out.println(" Enter the the total possible points"); Total_Points =input.nextInt(); // Prompt user to enter each students points System.out.println(" Now enter each students points"); for ( int i=0; i < numStudents; i++) { //ask the user to enter points for each student System.out.print(" Enter points for student " + (i+1)+" " ); student_points[i] = input.nextInt(); // check that the user total possible points are with in total possible points if ( Total_Points < student_points[i]){ System.out.print( "Sorry that is over the total possible, try again"); i--; } } for( int i=0; i< numStudents; i++) { // calculate and display percent and grade percent[i]= ((double) student_points[i] / Total_Points * 100); Grade[i] = LetterGrade(percent[i]); System.out.print(percent[i]+", "); System.out.println("Grade: "+Grade[i]); } for( int i=0; i< numStudents; i++) { // calculate the sum, the best and the lowest sum += percent[i]; //calculate best percent if (bestPercent < percent[i]) { bestPercent = percent[i]; } //calculate lowest percent if (lowPercent > percent[i]) { lowPercent = percent[i]; } } // calculate average class_average = sum / numStudents; System.out.println("Best percent: "+bestPercent); System.out.println("Lowest percent: "+lowPercent); System.out.println("Average percent: "+class_average); } // end of main public static char LetterGrade (double percent ) { char Grade; if (percent >= 90.0) {Grade='A';} else if (percent>= 80.0) {Grade='B';} else if (percent >= 70.0) {Grade='C';} else if (percent >= 60.0) {Grade = 'D';} else if (percent >= 50.0) {Grade = 'E' ;} else {Grade = 'F' ;} return Grade; } // end of LetterGrade } // end of class
brilliant_moves
Related Q & A:
- How To Connect Two Different Network Segments Using A Switch And A Router?Best solution by Super User
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How to Get Main output into Common() method in Java?Best solution by pages.cs.wisc.edu
- How do i scan something? First time using a scaner and i dont think im doing it right. help. thanks?Best solution by Yahoo! Answers
- How to use a laptop screen as just a screen?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.