Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By AndrewM16921

Thread: I'm TRYING... Program w/ a method that accepts and returns grades...

  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default I'm TRYING... Program w/ a method that accepts and returns grades...

    I am to: write a program with a method named evaluateGrade that accepts multiple students (at least 5) grades (ranging from 1 to 100) by using a Scanner and returns the students' course grades on the 4.0 scale. To stop grade input, please use '-1'.

    Java Code:
     // accessing integers in the array
    public class TEST
    {
    public static int main ( String args[] )
    {
    int [] scores; // declare array
    scores = new int[4]; // allocate memory space
    
    scores[0] = 90;
    scores[1] = 91;
    scores[2] = 95;
    scores[3] = -1;
    scores[3]++;
    
    // compute the average of the scores
    double average = ( scores[0] + scores[1]
    + scores[2] + scores[3] ) / 4.0;
    return Math.max(-1, average);
    }
    }
    Please, don't bash me. I tried, mixing and matching stuff, lol. And this isn't a duplicate...I added a code.

  2. #2
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: I'm TRYING... Program w/ a method that accepts and returns grades...

    Oh, for line 18, I get this error: Type mismatch: cannot convert from double to int

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: I'm TRYING... Program w/ a method that accepts and returns grades...

    Several things:

    1. The main method needs to return a void, not an int. So you need to create a second static method and call it from "public static void main(String[] args)."
    2. The error message you are getting is because you are trying to return a double (Math.max returns a double in that case) when the return type is declared as int.
    3. scores[3] = -1; scores[3]++; is the same as saying scores[3] = 0;
    4. I believe the assignment wants you to retrieve input from the console then stop reading when the input is -1. the max function isn't needed for this, and you should probably look into using a Scanner.


    I hope these things help. If you have any specific questions on any of them, feel free to ask.
    gimbal2 likes this.

  4. #4
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: I'm TRYING... Program w/ a method that accepts and returns grades...

    I think I got it:
    Java Code:
    public class evaluateGrade {
      public static void main ( String args[]) {
          Scanner input = new Scanner(System.in);
          
            int score = 0;
            double grade = (double)(score - 60) / 10.0;      
     
            while (score != -1){
                  if (score > 0){
                  }
                  {
    System.out.println("Type Grade (-1 to quit): ");
        }
                    
            score = input.nextInt();      
            for(int i = 0; i < 5; i++)
    // COMPUTE AND OUTPUT GRADE
            if (score >= 90) {
               grade = 4.0;
           } else if (score >= 80) {
               grade = 3.0;
           } else if (score >= 70) {
               grade = 2.0;
           } else if (score >= 60){
               grade = 1.0;
           } else if (score < 60)
           grade = 0.0;   
        System.out.println("Grade scale is "+ grade);
        //END
           }
      }
    }

Similar Threads

  1. Method returns value of 0
    By gerchan in forum New To Java
    Replies: 5
    Last Post: 09-27-2012, 09:47 AM
  2. using the nextLine method returns nothing
    By Bravid2001 in forum New To Java
    Replies: 6
    Last Post: 10-14-2011, 05:06 PM
  3. Inherited method that returns string
    By Xeal Rebad in forum New To Java
    Replies: 5
    Last Post: 05-23-2011, 01:26 PM
  4. Problems with method which returns result
    By new Object() in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 12:16 PM
  5. Replies: 5
    Last Post: 11-21-2010, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •