-4

This program is supposed to find the maximum, minimum, and average of grades. User inputs int inputGrade and the program displays letter it is. It's supposed to do this how however many students are needed. I'm having trouble writing the method where it finds the max and min. (yes I've talked to my teacher if anyone's wondering...) I pasted the methods below (they don't work). Just like IN GENERAL, does anyone know how to find the maximum and minimum of a set of entered numbers? (not using arrays, lists, or any unusual imports other than scanner) ** note I've updated this a lot...

import java.util.Scanner;
  public class GetLetterGrade
  {
  static int inputGrade; // input grade

   public static void main(String [] args)
  {
   Scanner reader = new Scanner(System.in); 


     int classAverage;
     int classMin; // class's minimum grade
     int classMax; // class's maximum grade

     while (inputGrade != -1) // while user is entering grades
  {

       System.out.println("Welcome to the grade calculator. \nPlease enter a 
       numeric grade.  After the last student in the class, enter a grade of 
       -1.");
       inputGrade = reader.nextInt();
       letterGrade(inputGrade); // calls letter grade method 
       findMaxAndMin();
        result();
   }

   }
   // find letter grade
   public static String letterGrade(int numGrade)
   {

   String gradeMessage = "";

   {

  if (numGrade >= 96 && numGrade <= 100) // if numeric grade is 96-100 then 
   it's A+
  {
    gradeMessage = "That's an A+.";
    result();
  // DOES THIS FOR GRADES A+ TO F, NOT SHOWN, too much to paste!
  }

  }
  }
     return gradeMessage;
  }

   public static int findCharGrade(int numGrade)
   {
      char letter;
      if (numGrade >= 90 && numGrade <= 100) // A
   {
       letter = 'A';

   }
       else if (numGrade >= 80 && numGrade < 90)  // B
  {
       letter = 'B';
  }
       else if (numGrade >= 70 && numGrade < 80)  // C
  {
       letter = 'C';

  }
       else if (numGrade >= 60 && numGrade < 70) // D
  {
       letter = 'D';
  }
       else if (numGrade < 60) // F
  {
       letter = 'F'; 
  }

  }




   // finds maximum and minimum grades
    public static int findMaxAndMin(int inputGrade)
    {

       int max = Math.max(inputGrade, max);
       int min = Math.min(inputGrade, min);

        if (inputGrade < max)
    {
          inputGrade = max;
          findCharGrade(inputGrade);
    }
       else if (inputGrade > min)
    {
          inputGrade = min;
          findCharGrade(inputGrade);
    }

     }
   public static void calcAverage(int sumOfGrades, int numOfStudents)
   {
   // something goes here

    }



   // finds results
   public static void result()
   {
     int min = findMaxAndMin(inputGrade);
     int max = findMaxAndMin(inputGrade);
     System.out.println("Please enter a numeric grade");
     int inputGrade = reader.nextInt();
     letterGrade(inputGrade);


     if (inputGrade == -1)
  {
    System.out.println("You entered " + numOfStudents + " students. Class 
    Average: " + average + " Class Minimum: " + min + " Class maximum: " + max 
    + " \nThanks for using the class grade calculator!");
  }

}
Janie
  • 41
  • 1
  • 9

3 Answers3

1

here is a more simplistic way of doing it not using Lists or arrays

    double sum = 0;  // use double so that you do not do integer arithmetic
    int count = 0;
    int min = Integer.MAX_VALUE;  // set to very high value
    int max = Integer.MIN_VALUE;  // set to bery low value

    Scanner scan1  =  new Scanner(System.in);
    System.out.println("enter numbers (-1 to quit");

    while (scan1.hasNextInt()) {
        int i = scan1.nextInt();  // get the number (assuming only int value)
        if (i == -1) break;

        min = Math.min(i, min);
        max = Math.max(i, max);
        sum += i;
        count++;
    }

    if (count > 0) {
        System.out.println("min " + min);
        System.out.println("max " + max);
        System.out.println("avg " + sum / count);
    }

disclaimer

This code will not handle wrong type of input e.g. Strings

edit

If you want the average to be calculated in a separate method you can have a method like

double calcAvg (double sum, int count) {
    return sum / count;
}

this can then be called as

    if (count > 0) {
        System.out.println("min " + min);
        System.out.println("max " + max);
        System.out.println("avg " + calcAvg (sum, count));
    }
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • wow I didn't even know that Math.min and Math.max existed. super helpful thanks – Janie Nov 15 '18 at 01:12
  • @Janie, if you are scared to use the `Math.min` you can use `if (i < min) min = i;` (Similar approach to max). And, yes, accept this answer! – zlakad Nov 15 '18 at 01:35
  • ok now my new question is how should I make the program keep track of the grades inputted so that way it can average them in the calcAverage() method? *see question for calcAverage() @zlakad – Janie Nov 15 '18 at 01:43
  • Do you want a running average without using a List or Array? Just put the code for `System.out.println("avg " + sum / count);` into a method – Scary Wombat Nov 15 '18 at 01:49
  • This answer already keeps the inputted grades and number of students in variables `sum` and `count`. So, you can **call** the method `double calcAverage(int sumOfGrades, int numberOfStudents) {return (double) sumOfGrades/numberOfStudents}`. Simple as that – zlakad Nov 15 '18 at 01:57
  • @zlakad `calcAverage(int sumOfGrades, int numberOfStudents) {return cumOfGrades/numberOfStudents}` -- eek no, that will do Integer division see http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 – Scary Wombat Nov 15 '18 at 01:58
  • how should I find sum though is my question? What should I add? – Janie Nov 15 '18 at 02:04
  • @ScaryWombat typo: `System.out.println("avg " + calcAvg (sum / count))` should be `System.out.println("avg " + calcAvg (sum, count))` – zlakad Nov 15 '18 at 02:13
  • sorry, got busy elsewhere. I've read the comments, but I'm not sure how I should get sumOfGrades or numOfStudents. I've tried placing numOfStudents++ in a method and it didn't work so idk how to count the entries (as students) in the program – Janie Nov 15 '18 at 03:27
  • If you can still not figure it out then edit your question with your new code – Scary Wombat Nov 15 '18 at 03:38
  • Did you read my edited answer? Where is your code that resembles my `calcAvg` that contains two paramaters and returns a `double`? Why are you taking more input in `result`? – Scary Wombat Nov 15 '18 at 04:08
  • er Janie - where ya gone to? – Scary Wombat Nov 15 '18 at 05:12
  • I can't use a double because my teacher might question that. There's more input in result because the user has to enter a bunch of grades and I call on that method from another method – Janie Nov 15 '18 at 13:56
  • You can and should use a double otherwise you will have integer division -see https://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 - you should be able to explain why you are using something, not just not use something – Scary Wombat Nov 15 '18 at 23:54
  • @Janie You seem to have ignored everything I have said - shall we delete this answer? – Scary Wombat Nov 16 '18 at 01:43
0

You can (and should) divide your problem into the smaller methods. I'll drop the code, and you read and study it. I admit I haven't pay to much attention of this simple quest, but still... Here you are:

import java.util.List;

public class Answer {

    public static void main(String[] args) {

        //test with some grades (integers)
        Answer answer = new Answer();
        List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
        System.out.println("max = " + answer.findMaxGrade(someGrades));
        System.out.println("min = " + answer.findMinGrade(someGrades));
        System.out.println("avg = " + answer.findAverageGrade(someGrades));

    }

    private int findMaxGrade(List<Integer> grades) {
        int max = Integer.MIN_VALUE;
        for (int grade : grades) {
            if (grade > max) max = grade;
        }
        return max;
    }

    private int findMinGrade(List<Integer> grades) {
        int min = Integer.MAX_VALUE;
        for (int grade : grades) {
            if (grade < min) min = grade;
        }
        return min;
    }

    private double findAverageGrade(List<Integer> grades) {
        double average = 0;
        for (int grade : grades) {
            average += grade;
        }
        return average / grades.size();
    }
}
zlakad
  • 1,246
  • 1
  • 9
  • 15
0

package example;

import java.util.Scanner;

class Example {

public static void main(String[] args) {
    Scanner r = new Scanner(System.in);
    int m = 1, total = 0, max = 0, min = 100;
    double avg = 0;
    while (m <= 5) {
        System.out.print("Input marks " + m + " = ");
        int inp = r.nextInt();
        total += inp;
        m++;
        min=min<inp?min:inp;
        max=max<inp?inp:max;


    }
    avg = (double)(total) / 5;
    System.out.println("Total : " + total);
    System.out.println("Max : " + max);
     System.out.println("Min : " + min);
    System.out.println("Average : " + avg);

}

}