11 Replies - 313 Views - Last Post: 11 November 2017 - 01:00 AM Rate Topic: -----

#1 Davidhworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-November 17

Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 12:24 AM

Hi guys,

i bump into some difficulty while doing this assignment.

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

Have your program output the students names, test scores, and average in a grid. Also, the program must output the class average, the name of the student with the name of the student with the highest average. The output should look like:

Name Test 1 Test 2 Test 3 Test 4 Test 5 Average
Johnson 85 83 77 91 76 82.40
Aniston 80 90 95 93 48 81.20
Cooper 78 81 11 90 73 66.60
Gupta 92 83 30 69 87 72.20
Blair 23 45 96 38 59 52.20
Clark 60 85 45 39 67 59.20
Kennedy 77 31 52 74 83 63.40
Bronson 93 94 89 77 97 90.00
Sunny 79 85 28 93 82 73.40
Smith 85 72 49 75 63 68.80


Class average: 70.94

Use at least 3 arrays: a one-dimensional array to store the students' names, a (parallel) two-dimensional integer array to store the test scores, and a one-dimensional (parallel) double array containing each students average.

This is what i came up with but having some error while running the programm.

import java.util.Scanner;
public class Scores
{
    // One-dimensional array to store the student names
    String[] names = {"Johnson", "Aniston", "Cooper", "Gupta", "Blair", "Clark", "Kennedy", "Bronson", "Sunny", "Smith"};
    // Parallel two-dimensional array to store the test score
    int[][] score = {{85, 83, 77, 91, 76},{80, 90, 95, 93, 48},{78, 81, 11, 90, 73},{92, 83, 30, 69, 87},{23, 45, 96, 38, 59},{60, 85, 45, 39, 67},{77, 31, 52, 74, 83},{93, 94, 89, 77, 97},{79, 85, 28, 93, 82},{85, 72, 49, 75, 63}};
    // Parallel one-dimenstional array to store grades
    char[] grades = new char[10];
    // Class average variable
    double classAverage;
    
    /**
     * Main function
     * @param args
     */
    public static void main(String[]args)
    {
        Scores s = new Scores();
        s.readData(); // comment this whole line if you want to use predefined values for arrays
        s.calculateAverage;
        s.printGrades();
    }
    /**
     * Reads and stores data into two arrays
     */
    private void readData()
    {
        Scanner kb = new Scanner(System.in);
        
        for(int i = 0; i < names.length; i++)
        {
            System.out.print("Please enter student name #" + (i+1) + ":");
            names[i] = kb.nextLine();
            for (int j = 0; j < scores[0].length; j++)
            {
                System.out.print("Please enter test result" + (j+1) + ":");
                scores[i][j] = kb.nextInt();
            }
            kb.nextLine();
        }
        kb.close();
    }
    
    /**
     * Calculates the average test score and grade
     */
    private void calculateAverage()
    {
        double average = 0;
        
        for (int i = 0; i < scores.length; i++)
        {
            average = 0;
            
            for (int j = 0; j < scores[0].length; j++)
            {
                average += scores[i][j];
            }
            average /= scores[0].length;
            
            if (average >= 0 && average < 20)
                grades[i] = 'E';
            else if (average >= 20 && average < 40)
                grades[i] = 'D';
            else if (average >= 40 && average < 60)
                grades[i] = 'C';
            else if (average >= 60 && average < 80)
                grades[i] = 'B';
            else if (average >= 80 && average <= 100)
                grades[i] = 'A';
            else
                grades[i] = '?';
            classAverage += average;
        }
        classAverage /= scores.length;
    }
    
    /**
     * Outputs the results
     */
    
    private void printGrades()
    {
        System.out.println("\nGrades:");
        
        for (int i = 0; i < grades.length; i++)
        {
            System.out.println(names[i] + ":" + grades[i]);
        }
        System.out.println("Class average: " + classAverage);
    }
}


Thanks and appreciate for your time!

This post has been edited by andrewsw: 10 November 2017 - 12:46 AM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Please see what mistakes i did in this program thanks

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6378
  • View blog
  • Posts: 25,769
  • Joined: 12-December 12

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 12:47 AM

Please wrap code in code tags, I have added them for you this time.

"having some error" What errors?
Was This Post Helpful? 0
  • +
  • -

#3 Davidhworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-November 17

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 12:53 AM

hi Andrew,

Thanks for helping me out here.

i have attached below the error message.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6378
  • View blog
  • Posts: 25,769
  • Joined: 12-December 12

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 12:56 AM

Please paste the text of the error message rather than attempting a screenshot.
Was This Post Helpful? 0
  • +
  • -

#5 Davidhworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-November 17

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 01:22 AM

hi Andrew,

this is the error i have encounter.

run:
Please enter student name #1:Johnson
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable scores
location: class Scores
at Scores.readData(Scores.java:35)
at Scores.main(Scores.java:20)
C:\Users\lauren\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 8 seconds)

regards,
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 01:29 AM

Ok, so what do you think it's telling you? Have you looked at the relevant lines (20 and 35 in Scores.java) to see what the problem might be? The error message is pretty clear.
Was This Post Helpful? 0
  • +
  • -

#7 Davidhworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-November 17

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 01:42 AM

hi ndc85430,

yes i have checked those line seems to have error on my scores. red underline on all scores.


regards,
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 01:48 AM

You really didn't answer the question about what you think the error means. That's the most important thing here, since it tells you what the problem is. A hint: where is scores declared?
Was This Post Helpful? 0
  • +
  • -

#9 Davidhworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-November 17

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 01:53 AM

i have already declare the score under the main method line 19.

public static void main(String[]args)
{
scores s = new Scores();
s.readData();
s.calculateAverage;
s.printGrades();

java is actually quite confusing for me..
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,482
  • Joined: 13-June 14

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 01:56 AM

No, on line 19, you're declaring a variable called s whose type is Scores (i.e. s is an instance of class Scores). On line 35, you're trying to use a variable called scores, which isn't declared anywhere.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6378
  • View blog
  • Posts: 25,769
  • Joined: 12-December 12

Re: Please see what mistakes i did in this program thanks

Posted 10 November 2017 - 02:04 AM

Please, again, put your code in code tags.
Was This Post Helpful? 0
  • +
  • -

#12 Davidhworld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-November 17

Re: Please see what mistakes i did in this program thanks

Posted 11 November 2017 - 01:00 AM

Hi guys,

Thank you for your hint.
I have corrected the error with your help.

I will remember to use the code tags in the future. Thank you for your patience as I'm learning the forum etiquette.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1