By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

pre exam study help needed

P: 45
Hi,

i've been practicing again......my exam is tomorrow :( and this is another practice question that my instructor told us to practice on, its not that easy, it was from a past final exam paper, so please help me out. I only did one little bit of it, and im 100% sure that its wrong......please bear with me :(

Here's how it goes:

Expand|Select|Wrap|Line Numbers
  1. public class BadStudentScores {
  2.         public BadStudentScores() { 
  3.             scores = new int[MAX_STUDENTS]; 
  4.             names = new String[MAX_STUDENTS]; 
  5.             numStudents = 0; 
  6.         } 
  7.  
  8.         public void add(String name, int score){ 
  9.            if (numStudents >= MAX_STUDENTS) 
  10.            return; // not enough space to add new student score 
  11.            names[numStudents] = name; 
  12.            scores[numStudents] = score; 
  13.            numStudents++; 
  14.         } 
  15.  
  16.         public String getHighest(){ 
  17.            if (numStudents == 0) 
  18.               return null; 
  19.           int highest = 0; 
  20.           for (int i = 1; i < numStudents; i++) 
  21.              if (scores[i] > scores[highest]) 
  22.                  highest = i; 
  23.           return names[highest]; 
  24.         } 
  25.         private final int MAX_STUDENTS = 100; 
  26.         private String[] names; 
  27.         private int[] scores; 
  28.         private int numStudents; 
  29. }
1.The implementation of the BadStudentScores class presented in the figure above, uses two parallel arrays to keep track of the names and scores of students. We want to modify this program to eliminate the use of parallel arrays and to be able to store an undefined number of students. The skeleton of the new design includes three classes: “student”, “GoodStudentsScores” and a tester “StudentsScoresTester”

1) The class “student” keeps all the information related to a student; provide the declaration for the necessary attributes and methods so that the three classes (student,GoodStudentsScores and StudentsScoresTester) work together.

2) Provide the code of the methods of the class GoodStudentsScores:
Expand|Select|Wrap|Line Numbers
  1. public GoodStudentsScores();
  2.     public void add(Student stud);
  3.     public String getHighest();
The figure below shows the skeletons for the three classes:

Expand|Select|Wrap|Line Numbers
  1. public class student {
  2.     // provide code here for constructor and necessary  methods
  3.     // provide declaration of attributes here
  4. }
  5.  
  6. public class GoodStudentsScores {
  7.        public GoodStudentsScores() { 
  8.         // provide code here ..
  9.         } 
  10.         public void add(student stud){ 
  11.             // provide code here ..
  12.         } 
  13.  
  14.         public String getHighest(){ 
  15.             // provide code here ..
  16.         }         
  17.         private ArrayList<student> data;
  18. }
  19.  
  20. public class StudentsScoresTester {
  21.     public static void main(String[] args) {
  22.         String names [] =  { "John", "Ali", "Layla"};
  23.         int scores      [] =  {   75   ,   85,     95};
  24.     GoodStudentsScores board = new GoodStudentsScores();
  25.         for (int i=0; i < names.length;i++){
  26.             student s = new student(names[i], scores[i]);
  27.             board.add(s);
  28.         }
  29.         System.out.println(board.getHighest() + 
  30.              "  has the highest scores");
  31.     }
  32. }
  33.  
  34. The output produced is:  Layla has the highest scores
This is what I tried to do:

Expand|Select|Wrap|Line Numbers
  1. public class student {
  2.     // provide code here for constructor and necessary methods
  3. public student(){
  4. string studName = null;
  5. int Studscores=0;
  6. }
  7.  
  8. public student (string studName, int studScores)
  9. {
  10.    this. studName = studName;
  11.    this. studScores = studScores;
  12. }    
  13.  
  14. public String getstudName ()
  15. {
  16.    return studName;
  17.  
  18. public void set studName(String studName)
  19. {
  20.   this.studName = studName;
  21. }
  22.  
  23. public int get studScores ()
  24. {
  25.    return studScores;
  26. }
  27.  
  28. public void setstudScores (int studScores)
  29. {
  30.    this.studScores = studScores;
  31. }
  32.  
  33.     // provide declaration of attributes here
  34.  
  35. String studName;
  36. int studScores;
  37.  
  38.  
  39. public class GoodStudentsScores {
  40.        public GoodStudentsScores() { 
  41.         // provide code here ..
  42.              studName=null;
  43.              studScores=0;
  44.          } 
  45.  
  46. }

Can anyone please tell me how i can do the public void add(student stud) part and the public String getHighest() part??? pleaseeee!!!

thanks soooooooooooooooooooooo much

outofmymind
Jan 8 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Hi,

i've been practicing again......my exam is tomorrow :( and this is another practice question that my instructor told us to practice on, its not that easy, it was from a past final exam paper, so please help me out. I only did one little bit of it, and im 100% sure that its wrong......please bear with me :(

Here's how it goes:

Expand|Select|Wrap|Line Numbers
  1. public class BadStudentScores {
  2.      public BadStudentScores() { 
  3.      scores = new int[MAX_STUDENTS]; 
  4.      names = new String[MAX_STUDENTS]; 
  5.      numStudents = 0; 
  6.      } 
  7.  
  8.      public void add(String name, int score){ 
  9.      if (numStudents >= MAX_STUDENTS) 
  10.      return; // not enough space to add new student score 
  11.      names[numStudents] = name; 
  12.      scores[numStudents] = score; 
  13.      numStudents++; 
  14.      } 
  15.  
  16.      public String getHighest(){ 
  17.      if (numStudents == 0) 
  18.      return null; 
  19.      int highest = 0; 
  20.      for (int i = 1; i < numStudents; i++) 
  21.      if (scores[i] > scores[highest]) 
  22.           highest = i; 
  23.      return names[highest]; 
  24.      } 
  25.      private final int MAX_STUDENTS = 100; 
  26.      private String[] names; 
  27.      private int[] scores; 
  28.      private int numStudents; 
  29. }
1.The implementation of the BadStudentScores class presented in the figure above, uses two parallel arrays to keep track of the names and scores of students. We want to modify this program to eliminate the use of parallel arrays and to be able to store an undefined number of students. The skeleton of the new design includes three classes: “student”, “GoodStudentsScores” and a tester “StudentsScoresTester”

1) The class “student” keeps all the information related to a student; provide the declaration for the necessary attributes and methods so that the three classes (student,GoodStudentsScores and StudentsScoresTester) work together.

2) Provide the code of the methods of the class GoodStudentsScores:
Expand|Select|Wrap|Line Numbers
  1. public GoodStudentsScores();
  2.     public void add(Student stud);
  3.     public String getHighest();
The figure below shows the skeletons for the three classes:

Expand|Select|Wrap|Line Numbers
  1. public class student {
  2.     // provide code here for constructor and necessary methods
  3.     // provide declaration of attributes here
  4. }
  5.  
  6. public class GoodStudentsScores {
  7.      public GoodStudentsScores() { 
  8.         // provide code here ..
  9.      } 
  10.      public void add(student stud){ 
  11.          // provide code here ..
  12.      } 
  13.  
  14.      public String getHighest(){ 
  15.          // provide code here ..
  16.      }      
  17.      private ArrayList<student> data;
  18. }
  19.  
  20. public class StudentsScoresTester {
  21.     public static void main(String[] args) {
  22.         String names [] = { "John", "Ali", "Layla"};
  23.         int scores [] = { 75 , 85, 95};
  24.     GoodStudentsScores board = new GoodStudentsScores();
  25.         for (int i=0; i < names.length;i++){
  26.             student s = new student(names[i], scores[i]);
  27.             board.add(s);
  28.         }
  29.         System.out.println(board.getHighest() + 
  30. " has the highest scores");
  31.     }
  32. }
  33.  
  34. The output produced is: Layla has the highest scores
This is what I tried to do:

Expand|Select|Wrap|Line Numbers
  1. public class student {
  2.     // provide code here for constructor and necessary methods
  3. public student(){
  4. string studName = null;
  5. int Studscores=0;
  6. }
  7.  
  8. public student (string studName, int studScores)
  9. {
  10. this. studName = studName;
  11. this. studScores = studScores;
  12. }    
  13.  
  14. public String getstudName ()
  15. {
  16. return studName;
  17.  
  18. public void set studName(String studName)
  19. {
  20. this.studName = studName;
  21. }
  22.  
  23. public int get studScores ()
  24. {
  25. return studScores;
  26. }
  27.  
  28. public void setstudScores (int studScores)
  29. {
  30. this.studScores = studScores;
  31. }
  32.  
  33.     // provide declaration of attributes here
  34.  
  35. String studName;
  36. int studScores;
  37.  
  38.  
  39. public class GoodStudentsScores {
  40.      public GoodStudentsScores() { 
  41.         // provide code here ..
  42. studName=null;
  43. studScores=0;
  44.  
  45. }

Can anyone please tell me how i can do the public void add(student stud) part and the public String getHighest() part??? pleaseeee!!!

thanks soooooooooooooooooooooo much

outofmymind
Now rember that the point is not too have the code working but to understand every line of it and why it's written that way because you are practising for a test. Now ask any part of the program that you don't understand.



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. class Student {
  4.  private String name;
  5.  private int score;
  6.  public Student() {
  7.   name = "";
  8.   score = 0;
  9.  }
  10.  public Student(String name, int score) {
  11.   this.name = name;
  12.   this.score = score;
  13.  }
  14.  public int getScore() {
  15.   return score;
  16.  }
  17.  public String getName() {
  18.   return name;
  19.  }
  20.  public String toString() {
  21.   return name + " has a score of " + score;
  22.  }
  23. }
  24. class GoodStudentsScores {
  25.  private ArrayList<Student> data;
  26.  public GoodStudentsScores() { 
  27.         data = new ArrayList<Student>();
  28.      }
  29.      public void add(Student stud){
  30.          data.add(stud);
  31.      }
  32.      public String getHighest(){
  33.          int highest = 0;
  34.          for(int i = 1; i < data.size(); i++) {
  35.    if(data.get(i).getScore() > data.get(highest).getScore()) {
  36.     highest = i;
  37.    }
  38.    }
  39.    return data.get(highest).getName();
  40.      }
  41. }
  42.  
  43. public class StudentsScoresTester {
  44.     public static void main(String[] args) {
  45.         String names [] = { "John", "Ali", "Layla"};
  46.         int scores [] = { 75 , 85, 95};
  47.     GoodStudentsScores board = new GoodStudentsScores();
  48.         for (int i=0; i < names.length;i++){
  49.             Student s = new Student(names[i], scores[i]);
  50.             board.add(s);
  51.         }
  52.         System.out.println(board.getHighest() +
  53. " has the highest scores");
  54.     }
  55. }
  56.  
Jan 8 '07 #2

P: 45
Now rember that the point is not too have the code working but to understand every line of it and why it's written that way because you are practising for a test. Now ask any part of the program that you don't understand.



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. class Student {
  4.  private String name;
  5.  private int score;
  6.  public Student() {
  7.   name = "";
  8.   score = 0;
  9.  }
  10.  public Student(String name, int score) {
  11.   this.name = name;
  12.   this.score = score;
  13.  }
  14.  public int getScore() {
  15.   return score;
  16.  }
  17.  public String getName() {
  18.   return name;
  19.  }
  20.  public String toString() {
  21.   return name + " has a score of " + score;
  22.  }
  23. }
  24. class GoodStudentsScores {
  25.  private ArrayList<Student> data;
  26.  public GoodStudentsScores() { 
  27.         data = new ArrayList<Student>();
  28.      }
  29.      public void add(Student stud){
  30.          data.add(stud);
  31.      }
  32.      public String getHighest(){
  33.          int highest = 0;
  34.          for(int i = 1; i < data.size(); i++) {
  35.    if(data.get(i).getScore() > data.get(highest).getScore()) {
  36.     highest = i;
  37.    }
  38.    }
  39.    return data.get(highest).getName();
  40.      }
  41. }
  42.  
  43. public class StudentsScoresTester {
  44.     public static void main(String[] args) {
  45.         String names [] = { "John", "Ali", "Layla"};
  46.         int scores [] = { 75 , 85, 95};
  47.     GoodStudentsScores board = new GoodStudentsScores();
  48.         for (int i=0; i < names.length;i++){
  49.             Student s = new Student(names[i], scores[i]);
  50.             board.add(s);
  51.         }
  52.         System.out.println(board.getHighest() +
  53. " has the highest scores");
  54.     }
  55. }
  56.  
Thanks for your help r035198x !!!!

When i saw your code i wa ssurprised that it turned out to be that way when i 1st tried it out i had the "name" and "score" as the names of my attributtes for the class students, but then i got confused seeing that they are declared as arrays in the tester program.
I understood the program you did, once again i expected something more complex than that, that's why i got nervous, and i just forgot everything and i didnt know ho to solve it.
what I didnt understand is commented in the code:
Expand|Select|Wrap|Line Numbers
  1.  public String getHighest()
  2.  {
  3.     int highest = 0;
  4.    for(int i = 1; i < data.size(); i++)  //you used size() instead of .length() ??
  5.    {
  6.    if(data.get(i).getScore() > data.get(highest).getScore())
  7.    {
  8.    highest = i;
  9.  }
  10.  }
  11.    return data.get(highest).getName(); //cant we just say return highest instead   
  12.      }                                    //of all that
Thank you!!!!!! couldn'tve done it without you
Jan 8 '07 #3

10K+
P: 13,264
Thanks for your help r035198x !!!!

When i saw your code i wa ssurprised that it turned out to be that way when i 1st tried it out i had the "name" and "score" as the names of my attributtes for the class students, but then i got confused seeing that they are declared as arrays in the tester program.
I understood the program you did, once again i expected something more complex than that, that's why i got nervous, and i just forgot everything and i didnt know ho to solve it.
what I didnt understand is commented in the code:
Expand|Select|Wrap|Line Numbers
  1. public String getHighest()
  2. {
  3. int highest = 0;
  4. for(int i = 1; i < data.size(); i++) //you used size() instead of .length() ??
  5. {
  6. if(data.get(i).getScore() > data.get(highest).getScore())
  7. {
  8. highest = i;
  9. }
  10. }
  11. return data.get(highest).getName(); //cant we just say return highest instead 
  12.      } //of all that
Thank you!!!!!! couldn'tve done it without you
Anytime.

The method for getting the number of elements in an ArrayList is size() not length(). There is no length() method in the class ArrayList. The length method is used for Strings to find the number of characters in a String.
Jan 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.