Results 1 to 4 of 4
  1. #1
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default Array help please

    I have an assignment that I'm stumped on. I created a class and got it to work for one student but I can't figure it out with multiple students. I've included what I currently have. Here is what they are asking for.


    Write a class that uses a string array to hold the 5 students names, an array of 5 characters to hold the 5 students letter grades, and 5 arrays of four doubles each to hold each students test scores.

    Java Code:
    import java.util.Scanner;
    
    public class GradeBook {
        //declare variables
        private String names;
        private int scores1;
        private int scores2;
        private int scores3;
        private int scores4;
        private double average;
        private char grades;
       
    	 Scanner keyboard = new Scanner(System.in);    
    
        //default constructor
        public GradeBook()
        {
            //initialize variables
            names = "";
            scores1 = 0; // Test Score 1
            scores2 = 0; // Test Score 2
            scores3 = 0; // Test Score 3
            scores4 = 0; // Test Score 4
            average = 0; // total of 4 test scores
    		  grades = 'A';         
        }
        
        //overloaded constructor
       public GradeBook (String inName, int scr1, int scr2, int scr3, int scr4, int scr5,
                          int sAverage, char sLetterGrade)
           {    
               names = inName;
               scores1 = scr1;
               scores2 = scr2;
               scores3 = scr3;
               scores4 = scr4;
               average = sAverage;
               grades = sLetterGrade;           
           }
           
           public String toString()
           {
               return "Name: " +names+ "\t" + "Average score: " +average+ "\t" + "Grade: " + grades;    
           }
           
           public void display()
           {
               Scanner keyboard = new Scanner(System.in);
               System.out.println(toString());
           }
             
           //names setter
           public void setnames(String student)
           {
               names = student;    
           }
           
           // scores1 setter
           public void setscores1(int S1)
           {
               scores1 = S1;               
           }
           
           //scores2 setter
           public void setscores2(int S2)
           {
               scores2 = S2;    
           }
           
           //scores3 setter
           public void setscores3(int S3)
           {
               scores3 = S3;    
           }
           
           //scores4 setter
           public void setscores4(int S4)
           {
               scores4 = S4;    
           }
          
               
           //average setter
           public void setaverage(int scores1, int scores2, int scores3, int scores4)
           {
               average = scores1 + scores2 + scores3 + scores4;    
           }
           
           //grades setter
           public void setgrades(char gradesScore)
           {
               grades = gradesScore;    
           }
           
           //names getter
           public String getnames()
           {
               return names;
           }
              
           // scores1 getter
           public int getscores1()
           {
               return scores1;    
           }
           
           //scores2 getter
           public int getscores2()
           {
               return scores2;    
           }
           
           //scores3 getter
           public int getscores3()
           {
               return scores3;    
           }
           
           //scores4 getter
           public int getscores4()
           {
               return scores4;    
           }
                    
           //average getter
           public double getaverage()
           {
               return average;    
           }
           
           //grades getter
           public char getgrades()
           {
               return grades;    
           }
           
              
           public void startProcessing()
           {
                            
               String student = "";                  
               System.out.println("Enter student 1's name:");
               student = keyboard.next();
               setnames(student);
    			  System.out.println("Now enter student 1's four test scores");
               System.out.println(""); 
               
               System.out.println("");
               
                  
               System.out.println("");
                              
               scores1=retrieveScore("Test Score #1", 100);
               scores2=retrieveScore("Test Score #2", 100);
               scores3=retrieveScore("Test Score #3", 100);              
               scores4=retrieveScore("Test Score #4", 100);
              
    			  
    			  average=grades();
                                   
               System.out.println("");
                      
                         
           }
           
           public int retrieveScore(String sVal, int maxVal)
           {
               int x;
               System.out.println(sVal + ":");
               x = keyboard.nextInt();
               if ((x>=0)&&(x<=maxVal))
               {
                   System.out.println(" ");
               }
               else
               {//If not valid test score
                   System.out.println("Invalid test score");
                   System.exit(0);    
               }
               return x;
           }
           
           public double grades()
               {             
                   int sumTest;
                   double percentTest;
                                                 
                                              
                   sumTest = scores1+ scores2+ scores3+ scores4;
                   percentTest = ((sumTest)/4.0);
                   average = percentTest;
                                  
            if(average>=90)
                grades='A';
            else if(average >=80)
                grades='B';
            else if(average>=70)
                grades='C';
            else if(average>=60)
                grades='D';
            else if(average<60)
                grades='F';
                
            return average;    
            }

    Java Code:
    public class gradeDemo {
    
       public static void main(String[] arg)
       {
    //     GradeBook gradingTest = new GradeBook();
    //       gradingTest.display();  
               
        GradeBook gradingTest = new GradeBook();
        gradingTest.startProcessing();
        
        
        System.out.println(gradingTest.toString());
                
       }
        
        
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class GradeBook
    {
        //declare variables
        // a string array to hold the 5 students names
        private String[] names = new String[5];
        // an array of 5 characters to hold the 5 students letter grades
        private char[] grades = new char[5];
        // 5 arrays of four doubles each to hold each students test scores
        private double[] scores1 = new double[4];
        private double[] scores2 = new double[4];
        private double[] scores3 = new double[4];
        private double[] scores4 = new double[4];
        private double[] scores5 = new double[4];

  3. #3
    buzzdsm is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    Thanks a ton hardwired.

    Is there something I need to take out of my class for this to work?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I would start over and build your class so that it does what you want it to do (according to your assignment).
    For example:
    Java Code:
    import java.util.Scanner;
    
    public class GradeBookRx
    {
        //declare variables
        // a string array to hold the 5 students names
        private String[] names = new String[5];
        // an array of 5 characters to hold the 5 students letter grades
        private char[] grades = new char[5];
        // 5 arrays of four doubles each to hold each students test scores
        private double[] scores1 = new double[4];
        private double[] scores2 = new double[4];
        private double[] scores3 = new double[4];
        private double[] scores4 = new double[4];
        private double[] scores5 = new double[4];
        int count = 0;
       
        Scanner keyboard = new Scanner(System.in);    
                 
        public void enterData()
        {                  
            System.out.println("Enter student's name:");
            String student = keyboard.nextLine();
            System.out.println("Now enter student's first test score");
            double score1 = Double.parseDouble(keyboard.nextLine());
            System.out.println("Now enter student's second test score");
            double score2 = Double.parseDouble(keyboard.nextLine());
            System.out.println("Now enter student's third test score");
            double score3 = Double.parseDouble(keyboard.nextLine());
            System.out.println("Now enter student's fourth test score");
            double score4 = Double.parseDouble(keyboard.nextLine());
            addStudent(student, score1, score2, score3, score4);
        }
           
        public String toString()
        {
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < names.length; i++) {
                if(names[i] == null)
                    continue;
                sb.append("Name: " + names[i] + "\t" +
                          "Average score: " + getAverage(i) +
                          "\t" + "Grade: " + grades[i] + "\n");
            }
            return sb.toString();
        }
    
        private void addStudent(String inName, double scr1,
                                double scr2, double scr3, double scr4)
        {
            // If arrays are full, start over at beginning.
            if(count == names.length-1)
                count = 0;
            names[count] = inName;
            scores1[count] = scr1;
            scores2[count] = scr2;
            scores3[count] = scr3;
            scores4[count] = scr4;
            grades[count] = getGrade(count);
            count++;
        }
          
        private double getAverage(int index)
        {
            double score1 = scores1[index];
            double score2 = scores2[index];
            double score3 = scores3[index];
            double score4 = scores4[index];
            double average = (score1 + score2 + score3 + score4)/4.0;
            return average;
        }
           
        private char getGrade(int index)
        {             
            double average = getAverage(index);
            char grade;
                                  
            if(average>=90)
                grade='A';
            else if(average >=80)
                grade='B';
            else if(average>=70)
                grade='C';
            else if(average>=60)
                grade='D';
            else   // average<60
                grade='F';
    
            return grade;    
        }
    
        public static void main(String[] argsd)
        {
            GradeBookRx test = new GradeBookRx();
            test.enterData();
            System.out.println("gradebook = " + test);
        }
    }

Similar Threads

  1. help with an array.
    By f_the_cook in forum New To Java
    Replies: 7
    Last Post: 06-03-2008, 05:05 AM
  2. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  3. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 09:59 PM
  4. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 06:57 PM
  5. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:32 AM

Posting Permissions

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