Arrays

Having some trouble understanding arrays and how to reference them in my methods. Trying to get going on a program but don't know where to start...

Design and implement a class called Statistics that stores an array of int (grades) and does
various statistical calculations. The class will contain the following methods/operations:

1) highest - returns the highest grade
2) lowest - returns the lowest grade
3) number of grades - returns the number of grades in the array
4) average - returns the average of all the grades
5) print chart - prints out the number of A, B, C, D, F if an
A = 90 - 100
B = 89 - 80
C = 79 - 70
D = 69 - 60
F = < 60

Write a main method to test out this object. The grades to be implemented for this object
are:
int[ ] grades = {79, 93, 51, 50, 70, 75, 97, 97, 98, 62, 67, 80, 69, 44, 84, 93, 94,
76, 61, 96, 97, 58, 37, 83, 60, 59, 54, 56, 82, 33, 83, 62, 64, 26,
74, 71}

Any help is appreciated.
AW

Comments

  • 1)
    int highest = 0;
    for(int i = 0;ihighest){
    highest=grades[i];
    }

    2)
    int lowest = 100;
    for(int i = 0;i= 90){
    System.out.println("A");
    }
    if(grades[i]<90&&grades[i]>=80){
    System.out.println("B");
    }
    if(grades[i]<80&&grades[i]>=70){
    System.out.println("C");
    }
    if(grades[i]<70&&grades[i]>=60){
    System.out.println("D");
    }
    if(grades[i] < 60){
    System.out.println("F");
    }
    }











  • : 1)
    : int highest = 0;
    : for(int i = 0;ihighest){
    : highest=grades[i];
    : }
    :
    : 2)
    : int lowest = 100;
    : for(int i = 0;i= 90){
    : System.out.println("A");
    : }
    : if(grades[i]<90&&grades[i]>=80){
    : System.out.println("B");
    : }
    : if(grades[i]<80&&grades[i]>=70){
    : System.out.println("C");
    : }
    : if(grades[i]<70&&grades[i]>=60){
    : System.out.println("D");
    : }
    : if(grades[i] < 60){
    : System.out.println("F");
    : }
    : }
    :
    :

    How do I call this in my MAIN? I need to print distro of grades. i.e. 10 A's, 4 B's, 5 C's, etc...

    Here's my method:

    public static void getLetter(int grades[]){

    for(int i = 0;i= 90){
    System.out.println("A");
    }
    if(grades[i]<90&&grades[i]>=80){
    System.out.println("B");
    }
    if(grades[i]<80&&grades[i]>=70){
    System.out.println("C");
    }
    if(grades[i]<70&&grades[i]>=60){
    System.out.println("D");
    }
    if(grades[i] < 60){
    System.out.println("F");
    }
    }

    }

    Here's my main:

    System.out.println("Distribution of grades:");
    System.out.println("A = 90 - 100 :");
    System.out.println("B = 89 - 80 :");
    System.out.println("C = 79 - 70 :");
    System.out.println("D = 69 - 60 :");
    System.out.println("F = < 60 :");

    Thank you!
  • public static void getLetter(int grades[]){
    int counterA = 0;
    int counterB = 0;
    int counterC = 0;
    int counterD = 0;
    int counterF = 0;

    for(int i = 0;i= 90){
    counterA++;
    }
    if(grades[i]<90&&grades[i]>=80){
    counterB++;
    }
    if(grades[i]<80&&grades[i]>=70){
    counterC++;
    }
    if(grades[i]<70&&grades[i]>=60){
    counterD++;
    }
    if(grades[i] < 60){
    counterF++;
    }
    }
    System.out.println("Distribution of grades:");
    System.out.println("A = 90 - 100 :" + counterA);
    System.out.println("B = 89 - 80 :" + counterB);
    System.out.println("C = 79 - 70 :" + counterC);
    System.out.println("D = 69 - 60 :" + counterD);
    System.out.println("F = < 60 :" + counterF);

    }
    In main if this is same class just call this method

    getLetter(grades);

    or

    this.getLetter(grades);
  • Thank you.
    I'm confused as to what the grades[i] comes from. I see that often since I've been working on these array problems. Could you explain?
    Thanks again for your help.

    AW

    : public static void getLetter(int grades[]){
    : int counterA = 0;
    : int counterB = 0;
    : int counterC = 0;
    : int counterD = 0;
    : int counterF = 0;
    :
    : for(int i = 0;i= 90){
    : counterA++;
    : }
    : if(grades[i]<90&&grades[i]>=80){
    : counterB++;
    : }
    : if(grades[i]<80&&grades[i]>=70){
    : counterC++;
    : }
    : if(grades[i]<70&&grades[i]>=60){
    : counterD++;
    : }
    : if(grades[i] < 60){
    : counterF++;
    : }
    : }
    : System.out.println("Distribution of grades:");
    : System.out.println("A = 90 - 100 :" + counterA);
    : System.out.println("B = 89 - 80 :" + counterB);
    : System.out.println("C = 79 - 70 :" + counterC);
    : System.out.println("D = 69 - 60 :" + counterD);
    : System.out.println("F = < 60 :" + counterF);
    :
    : }
    : In main if this is same class just call this method
    :
    : getLetter(grades);
    :
    : or
    :
    : this.getLetter(grades);
    :

  • "i" is your counter. When you are looping thrue the array you increasing i by 1.That's what i++ does.
    So grades[i] first time in the loop means grades[0] second grades[1] and so on.
  • : "i" is your counter. When you are looping thrue the array you increasing i by 1.That's what i++ does.
    : So grades[i] first time in the loop means grades[0] second grades[1] and so on.
    :

    So does teh if(grades[i]..etc) theoretically 'scroll' thru all the grades in the array and counts off the ones in that range specified?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion