Results 1 to 6 of 6
  1. #1
    trejorchest is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Java assignment, need guidance ...

    I'm incredibly new to writing Java code. For my assignment, I have to use arrays to calculate the sum, average, largest, and smallest of any number of test scores. In addition, I have to use a "for loop" to tally up the number of A'S, B'S, C'S, D'S, and F'S (ex. "The number of students with scores of 90-100 (A) is 4", etc. for each letter grade) based on the test scores. All this has to be shown in a message box. I can calculate the sum, average, largest, and smallest parts of this assignment, but I can't get the latter half of the assignment done right. Instead of "The number of students with scores of 90-100 (A) is (insert calculated number here)" showing up, I get "The number of students with scores of 90-100 (A) is 0", and this happens for each letter grade I'm trying to determine. I'm not sure why the 0 is showing up. Here is my code:



    import javax.swing.JOptionPane;
    public class ArrayProject3 {

    public static void main(String[] args) {

    String response = JOptionPane.showInputDialog(null, "Number of scores:");
    int number = Integer.parseInt(response);
    int scores[] = new int[number];
    for (int a = 0; a < number; a++) {
    response = JOptionPane.showInputDialog(null, "Enter score " + (a+1));
    scores[a] = Integer.parseInt(response);
    }
    int count = 0;
    int gradeA = 0;
    int gradeB = 0;
    int gradeC = 0;
    int gradeD = 0;
    int gradeF = 0;
    int sum = 0;
    int largest = scores[0];
    int smallest = scores[0];

    for (int i = 0; i < scores.length; i++) {
    sum = sum + scores[i];
    if (scores[i] > largest) {
    largest = scores[i];
    }
    if (scores[i] < smallest) {
    smallest = scores[i];
    }
    if (scores[i] <= 90 && scores[i] >= 100){
    gradeA = count++;}
    if (scores[i] <= 80 && scores[i] >= 89){
    gradeB = count++;}
    if (scores[i] <= 70 && scores[i] >= 79){
    gradeC = count++;}
    if (scores[i] <= 60 && scores[i] >= 69){
    gradeD = count++;}
    if (scores[i] <= 60){
    gradeF = count++;}
    }

    JOptionPane.showMessageDialog(null, "The sum is " + sum
    + "\nThe average is " + (sum / scores.length)
    + "\nThe largest is " + largest
    + "\nThe smallest is " + smallest
    + "\nThe number of students with scores of 90-100 (A) " + gradeA
    + "\nThe number of students with scores of 80-89 (B) " + gradeB
    + "\nThe number of students with scores of 70-79 (C) " + gradeC
    + "\nThe number of students with scores of 60-69 (D) " + gradeD
    + "\nThe number of students with scores below 60 (F) " + gradeF);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Look at your logic, logically. Ask yourself, is this ever going to be true?

    Java Code:
    if (scores[i] <= 90 && scores[i] >= 100) {
    Again, think carefully on this and see if a number can be less than 91 and greater than 99 at the same time. The same logic applies for your other conditions.

    Then look at this:
    Java Code:
    gradeA = count++;
    Does this make sense? What variable do you want to increase by one in this block? Is this how you increase an integer variable by one?

  3. #3
    trejorchest is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Okay, thank you for explaining my mistake in the "for loop" part. I changed the "&&" to just "&". I'm still puzzled on how to tally the grades up, though. Creating the "int count" variable is, presently, the only way I know how to increment the gradeA, gradeB, etc. variables, and its not working.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Changing from logical and to bitwise and is incorrect. You need to consider when a grade can simultaneously be less than 90 and greater than 100. Try thinking of one number that can meet that criteria.

    Do you understand how the increment operator works? Each time you do
    Java Code:
    gradeX = count++;
    You are setting the value of gradeX to count, and then incrementing count. You can increment any integer, so why not just increment the gradeX variable?
    Last edited by sunde887; 04-17-2011 at 05:46 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by trejorchest View Post
    I changed the "&&" to just "&".
    Why on earth?

  6. #6
    trejorchest is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Okay, I got the program working! THANK YOU for your help sunde887. To summarize the corrections I made, in my "for loop", I replaced the "&&" with just "&". I also removed the "count" variable completely and incremented the gradeA, gradeB, etc. variables inside the "for loop" with "gradeA = gradeA + 1", etc. Here is the working code:


    import javax.swing.JOptionPane;
    public class ArrayProject3 {
    public static void main(String[] args) {

    String response = JOptionPane.showInputDialog(null, "Number of scores:");
    int number = Integer.parseInt(response);
    int scores[] = new int[number];
    for (int a = 0; a < number; a++) {
    response = JOptionPane.showInputDialog(null, "Enter score " + (a+1));
    scores[a] = Integer.parseInt(response);
    }

    int gradeA = 0;
    int gradeB = 0;
    int gradeC = 0;
    int gradeD = 0;
    int gradeF = 0;
    int sum = 0;
    int largest = scores[0];
    int smallest = scores[0];

    for (int i = 0; i < scores.length; i++) {
    sum = sum + scores[i];
    if (scores[i] > largest) {
    largest = scores[i];
    }
    if (scores[i] < smallest) {
    smallest = scores[i];
    }
    if (scores[i] >= 90 & scores[i] <= 100){
    gradeA = gradeA + 1;}
    if (scores[i] <= 89 & scores[i] >= 80){
    gradeB = gradeB + 1;}
    if (scores[i] <= 79 & scores[i] >= 70){
    gradeC = gradeC + 1;}
    if (scores[i] <= 69 & scores[i] >= 60){
    gradeD = gradeD + 1;}
    if (scores[i] < 60){
    gradeF = gradeF + 1;}}

    JOptionPane.showMessageDialog(null, "The sum is " + sum
    + "\nThe average is " + (sum / scores.length)
    + "\nThe largest is " + largest
    + "\nThe smallest is " + smallest
    + "\nThe number of students with scores of 90-100 (A) " + gradeA
    + "\nThe number of students with scores of 80-89 (B) " + gradeB
    + "\nThe number of students with scores of 70-79 (C) " + gradeC
    + "\nThe number of students with scores of 60-69 (D) " + gradeD
    + "\nThe number of students with scores below 60 (F) " + gradeF);
    }
    }

Similar Threads

  1. Project Guidance
    By Catfather in forum Advanced Java
    Replies: 2
    Last Post: 02-28-2011, 05:36 AM
  2. Some Guidance
    By bengregg in forum Java Applets
    Replies: 15
    Last Post: 01-11-2011, 10:04 AM
  3. newbie guidance.
    By Bgreen7887 in forum New To Java
    Replies: 28
    Last Post: 10-17-2010, 10:31 PM
  4. Guidance for SCJP
    By sairam in forum New To Java
    Replies: 1
    Last Post: 06-30-2010, 09:21 AM
  5. Guidance
    By gizdev in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 07:14 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
  •