3 Replies - 940 Views - Last Post: 04 March 2014 - 03:39 AM Rate Topic: -----

#1 ettedo2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-September 12

Problem with arrays

Posted 03 March 2014 - 06:16 PM

Hello,
I've been working on this Grad Book program that includes arrays. I got the program working except of one little problem at the end where I want to display all the data.

This is not a Homework project, I am just trying to understand array's better and grabbed this programming challenge on my own.

The problem is that when it display all 5 Students it only gives me the Score and Average value of my last data entry.
Somewhere I must have a logical error but I am still confused with arrays and so I do not seem to find the problem on my own.

Here is my code
public class Grade {
    
    private final int NUM_GRADE = 5;
    private String[] names = {"john","mark","frank","peter","tom"};
    private char[] letter = new char[NUM_GRADE];
    private double[] score = new double [NUM_GRADE];
    
    public void setNames(String n, int i)
    {
        names[i] = n;
    }
    
    public void setLetter(char l, int i)
    {
        letter[i] =l;
    }
    
    public void setScore(double s, int i )
    {
      score[i] = s; 
    }
    
    public String getNames(int i)
    {
        return names[i];
    }
    
    public char getLetter(int i)
    {
        
        if(score[i] > 90)
        {
          letter[i] = 'A';  
        }
        else if (score[i] < 90 && score[i] > 80)
        {
            letter[i] = 'B';
        }
        else if (score[i] < 80 && score[i] > 70)
        {
            letter[i] = 'C';
        }
        else if (score[i] < 70 && score[i] > 60)
        {
            letter[i] = 'D';
        }
        else 
        {
            letter[i] = 'D';
        }
        
        return letter[i];
    }
    
    public double getScore(int i)
    {
        double total = 0.0;
        
        for (i = 0; i < NUM_GRADE; i++)
            total += score[i];
        return total;
    }
    
    public double getAverage(int i)
    {
        double total = 0.0;
        double average;
        
        for (i = 0; i < score.length; i++)
            total += score[i];
        
        average = total / score.length;
        
        return average;
    }
    
}



import java.util.Scanner;

public class GradeDemo 
{ 
    public static void main(String[] args) 
  {
    final int NUM_ARRAY = 5;
    double score;
    int i , x,y;

    
    Scanner kb = new Scanner(System.in);
    Grade grade = new Grade();

    for(i = 0; i < NUM_ARRAY; i++)
    {
        System.out.print("Enter scores for Student: ");
        System.out.println(grade.getNames(i));
       for(x = 0; x < NUM_ARRAY; x++)
       {
         System.out.println("Score " + (x+1) + ": ");
         score = kb.nextDouble();
         grade.setScore(score, x);
       }  
    }
   
     for(y = 0; y < NUM_ARRAY; y++)
    {
        System.out.println("Student information: " + grade.getNames(y));
        System.out.println("Scores: " + grade.getScore(y));
        System.out.println("Average: " + grade.getAverage(y));
        System.out.println("Grade: " + grade.getLetter(y));

    }
  
  }
}


Here is my display of the code running:
Enter scores for Student: john
Score 1:
1
Score 2:
1
Score 3:
1
Score 4:
1
Score 5:
1
Enter scores for Student: mark
Score 1:
2
Score 2:
2
Score 3:
2
Score 4:
2
Score 5:
2
Enter scores for Student: frank
Score 1:
3
Score 2:
3
Score 3:
3
Score 4:
3
Score 5:
3
Enter scores for Student: peter
Score 1:
4
Score 2:
4
Score 3:
4
Score 4:
4
Score 5:
4
Enter scores for Student: tom
Score 1:
5
Score 2:
5
Score 3:
5
Score 4:
5
Score 5:
5
Student information: john
Scores: 25.0
Average: 5.0
Grade: D
Student information: mark
Scores: 25.0
Average: 5.0
Grade: D
Student information: frank
Scores: 25.0
Average: 5.0
Grade: D
Student information: peter
Scores: 25.0
Average: 5.0
Grade: D
Student information: tom
Scores: 25.0
Average: 5.0
Grade: D


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with arrays

#2 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Problem with arrays

Posted 03 March 2014 - 06:23 PM

Please create an array of Objects rather than 3 parallel arrays. This is not only easier to debug, but best practice.

// Create a Grade class to hold information about a grade.
public class Grade {
    private String name;
    private char letter;
    private double score;

    // Getters/setters
}

// Now you can create an array of Grade objects.
Grade[] grades = new Grade[5];



Please refer to macosxnerd101's tutorial titled: Moving Away From Parallel Arrays for more information on the subject.

This post has been edited by x68zeppelin80x: 03 March 2014 - 06:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 ettedo2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-September 12

Re: Problem with arrays

Posted 03 March 2014 - 07:01 PM

View Postx68zeppelin80x, on 03 March 2014 - 06:23 PM, said:

Please create an array of Objects rather than 3 parallel arrays. This is not only easier to debug, but best practice.

// Create a Grade class to hold information about a grade.
public class Grade {
    private String name;
    private char letter;
    private double score;

    // Getters/setters
}

// Now you can create an array of Grade objects.
Grade[] grades = new Grade[5];



Please refer to macosxnerd101's tutorial titled: Moving Away From Parallel Arrays for more information on the subject.


OK, this is confusing. What I need to do is take out the array's from my Grade class and create one array object in my Demo class?
Was This Post Helpful? 0
  • +
  • -

#4 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Problem with arrays

Posted 04 March 2014 - 03:39 AM

View Postettedo2000, on 03 March 2014 - 10:01 PM, said:

OK, this is confusing. What I need to do is take out the array's from my Grade class and create one array object in my Demo class?


This is a skeleton, you must provide the business logic. You already have everything you need.

import java.util.*;

// Stores a name, letter grade, and list of assignment scores.
class Grade {
    private String name;
    private char letter;
    private double[] scores;

	// Constructor: Set the name of the student for the Grade.
	public Grade(String name) {
		this.name = name;
	}

    public void setName(String name) { this.name = name; }
    public String getName() { return name; }
    
    public void setLetter(char letter) { this.letter = letter; }
    public char getLetter() { return letter; }
    
    public void setScores(double[] scores) { this.scores = scores; }
    public double[] getScores() { return scores; }
    
    public double getAverage() {
        double total = 0.0;

        for (double score : scores) {
            total += score;
        }
        
        return total / scores.length;
    }
}

class GradeBook {
	// A costant list of all student names.
	private static final String[] NAMES = {
		"John", "Mark", "Frank", "Peter", "Tom" };
	// The number of assignments for each student.
	private static final int ASSIGNMENT_COUNT = 5;
	// Create a new Grade entry for each student.
	private static Grade[] grades = new Grade[NAMES.length];
	
	public static void main (String[] args) {
		Scanner kb = new Scanner(System.in);

		// Loop over all students.
		for (int i = 0; i < grades.length; i++) {
			// Create a new Grade object with the current student's name.
			Grade grade = new Grade(NAMES[i]);
			// Prompt user to enter scores for the current student.
			System.out.printf("Enter grades for %s: ", grade.getName());
			
			// Create a local scores array.
			double[] scores = new double[];
			// Loop over all scores.
			for (int j = 0; j < ASSIGNMENT_COUNT; j++) {
				// Prompt the user to enter a score.
				System.out.printf("Score %d: ", (j + 1));
				// Add score to the local array of scores.
				scores[i] = kb.nextDouble();
			}
			
			// Assign the local copy to the grade object.
			grade.setScores(scores);
		}
	}
}

This post has been edited by x68zeppelin80x: 04 March 2014 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1