• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Student and Main Class project

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,

Im currently working on a project with these basic guidelines:


You may only use while(){ } or do{ }while() loops for any loops in this project.

1. Create a Java class named Student that extends Object.

2. The Student class should have instance variables (with getters and setters) for a student's last name (String), first name (String), student id number (int), an array for up to 15 project scores that are represented as double values, named "projects," and an array for up to 10 quiz scores that are represented as double values, named "quizzes."

3. There should be no getters or setters for the arrays themselves. They should not be accessible from outside of this class without going through the other methods of the class. The getters and setters described in 5 and 6 below indicate getting and setting specific contents of the arrays, not the array objects themselves.

4. The constructor of the Student class should take the student's last and first name and a student id as parameters and set the instance variables to those values. It should also instantiate the arrays to their size. There should be no console I/O in the Student class unless it is in the main method or a private static method called by the main method. The constructor should initialize all elements of the arrays to -1.0 to indicate there is no score in that element yet.

5. Create setProjectScore() and setQuizScore() methods for the arrays. These should only take a double as a parameter and an int as a project or quiz number. These methods should return a boolean to indicate success or failure, depending on if the project or quiz number is out of the bounds of the array. The setProjectScore() and setQuizScore() methods will place the double value into the project or quiz number index of its respective array.

6. Create getProjectScore() and getQuizScore() methods for the arrays. These methods will return a double value. These will take an index parameter, representing the project or quiz number to return. They will return the value stored at that index in the array. This method should return -1.0 if the array index is out of bounds. -1.0 is a common flag to indicate failure in methods that should only return non-negative floats or doubles. If the element in the array is a -1.0, that indicates there is no valid score in that slot and -1.0 should be returned as a failure code.

7. Create a method named getNextProjectIndex() that returns an int that gets the next index of the projects array that contains a -1.0. Find the next item in the projects array that contains a -1.0 and return that index of the array. This method should return -1 if the array is full. -1 is a common flag to indicate failure in methods that should only return non-negative integers.

8. Create a method named getNextQuizIndex() that returns an int that gets the next index of the quizzes array that contains a -1.0. Find the next item in the quizzes array that contains a -1.0 and return that index of the array. This method should return -1 if the array is full.
9. There should be no getters for the arrays, since we don't want any outside program modifying their contents without going through the setProjectScore() and setQuizScore() methods.

10. You may add other methods as needed, such as a method to return the size of the projects and quizzes arrays. Keep in mind that any method you add that is not meant to be called from an outside class should be private.

11. Create a Java class, named Main, that has a main method. The main() method of the class is a tester. We will exercise the methods of the Student class to make sure they are working properly. Any console I/O must be in the main() method or a private static method called by the main method. This Main class is only there to test the functionality of your Student class. The Student class should not do any I/O to the console (debugging output is allowed but should be removed in the final product); only the Main class' main() method should interact with the console.

12. Instantiate a Student object in the main() method. You should not make local copies of the Student object's arrays in the main() method, but rather only access them via the object's methods. Remember, these requirements state that no getters for the arrays in Student are allowed.

13. Try to display the student's information to the console. Use a loop to get project and quiz scores from the student object. This will test your code to make sure it handles a student with no project or quiz scores.

14. Fill the projects and quizzes for this student. Use the getNextProjectIndex() and getNextQuizIndex() methods to find the indexes in which to add the scores.

15. Try to go out of bounds to add a project and a quiz to the student to test the ability of the Student class's methods to handle full arrays.

16. Once full, display the student's information to the console. Use a loop to get project and quiz scores from the student object.


This is the code for my Student class:



Im on the step n. 12 of the project coding the Main app and, for whatever reason, I cant get it to display the LastName, FirstName and StudentID that Ive setted on the main app for testing purposes. Heres the code until now:



I know there are more requirements, but I would appreciate any advices on why I get the results below on the console instead of Braun, Jacob and the number 1.

Student Last Name is: null
Student First Name is: null
Student ID is: 0


Thanks in advance!
 
Ranch Hand
Posts: 75
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at class Main, when you construct your new Student lastName is null, firstName is null, and studentId is 0. Next your are assigning values to lastName, firstName, and studentId, which will be null, null, and 0 because you are getting them from Student st. You then set st variables with values. Lastly you're printing lastName, firstName, and studentId (but they haven't been changed from null, null, and 0). See it?
 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the input, Emil!

Heres my new Main class.



Im getting infinite loops trying to fulfill the requirement n. 18 (Fill the projects and quizzes for this student. Use the getNextProjectIndex() and getNextQuizIndex() methods to find the indexes in which to add the scores).

Any ideas why this is happening?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look CAREFULLY at this method you wrote:

You don't have curly braces around the body of your while loop. When that happens, only ONE line is include. This is why you should ALWAYS use them, needed or not. The above code, indented properly would look like this:


In other words, the "nextProjectIndex++" statement is outside the loop body, so it doesn't ever execute. This is equivilent to your code, with brackets added:



This should make it a little clearer what you need to do in this - and other - method.
 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That solved it! Thanks a lot, Fred.

The code compiles fine now. Only issue left is that the project and quiz grades are not being set. HereĀ“s my new Main class:



This is whet I get on the console:

Student Last Name is: Braun
Student First Name is: Thiago
Student ID is: 1
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Project Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0
Student Quiz Grades are: -1.0



Any ideas why some of the grades are not 70.0 on the console?
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nope. But you do have problems.

You've written WAY too much code before making sure what you've written works. Ideally, you would only write 2-3 lines before you compile, test, debug, repeat. You've got 45 lines in your Main class, and another couple hundred in other files. I wouldn't know where to START looking for bugs in this.

I would suggest you put in a ton of System.out.println statements to see what your code is actually doing. I'd probably even write some additional code that lets me only run/test/debug individual methods.
 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I actually found out the problem, but I will keep that in mind next time!

Cheers
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic