473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

pre exam study help needed

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
3 1852
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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

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

Similar topics

0
by: who be dat? | last post by:
I'm currently taking an online class for SQL Server which is aimed at teaching you the information you need to pass exam #70-229. However, concerns exists on my part on how good this web based...
2
by: Mark | last post by:
I've taken 4 of 5 exams to get my MCSD. My last exam is the Solution Architecture exam. At least in my experience, I've been surprised at how different the difficultly level *felt* between the...
6
by: Wee Bubba | last post by:
hello. I have decided to study for my MCAD qualifications. I am definetely going to do a core exam in "Developing Web Applications With C#". I am also doing a core exam in "Developing XML...
7
by: Likhith Areekkal | last post by:
Hi, If any one has the exam questions for MCAD 70-316 (App. Dev. C#) please send it to me in the following address: li_areekkal (at) sympatico.ca Thank you. Regards, Li
29
by: Mr Newbie | last post by:
Im going to be looking to do this exam fairly soon, but ive done a couple of practice tests and found them to be a bit tricky in as much as you have to REALLY READ the questions carefully else they...
2
by: derekbarrett | last post by:
Hi, I found this article in DB2 magazine and learned about the Problem Determination Mastery Exam. I am very interested in taking the exam, however, following the links in the article leads to...
3
by: samadams_2006 | last post by:
Hello, I'm interested in taking the following exam for an upcoming job. Exam 70-315: Developing and Implementing Web Applications with Microsoft Visual C#â„¢ .NET and Microsoft Visual Studio...
3
by: SomeGuyHere | last post by:
I'm studying for the Microsoft .NET 2.0 Application development foundation exam. I just finished the 1000 page study guide cover-2-cover doing all examples. My first try I failed the practice test...
0
by: Joel Garry | last post by:
earl00002@yahoo.com (ear U) wrote in message news:<ea1dedaf.0307040500.25511592@posting.google.com>... Hey extremely stupid person! Do not crosspost among these groups. If you have a legitimate...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.