File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes In need of much help with my main method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "In need of much help with my main method." Watch "In need of much help with my main method." New topic

In need of much help with my main method.

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54
I'm making a project that will display project and quiz scores for a student.

I'm done with the student class but I have to make a separate class called 'main' to display all the info.

I was told that the main method would look something like this:

but the code just becomes an infinite loop

Here are the instructions for the main method for clarification:

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.

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.

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.

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

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.

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

Note: I changed the above to be inside quote tags rather than code tags. Code tags don't allow text wrapping which makes viewing those long, unbroken lines of text hard, and messes up the formatting of the page. /Steve

My student class:

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

The condition to end the while() loop in the main method is to call the getNextQuizIndex() and stop when it reaches -1. The content of the while loop does not modify the way this code gets executed - it doesn't change anything associated with the quiz index, nor does it change the Object Kevin refers to. So you are relying on the method getNextQuizIndex() to behave differently each time it is called. Since you get an infinite loop you can be pretty sure this doesn't happen. So you should go through the getNextQuizIndex() method and see what it does.

Using a pen and paper, go through each step in the method and write down each variable, what its value is, and when the method ends (when return is encountered) record the value returned by the method. Then walk through the method a second time, writing the variable assignments on a different column on paper. Did anything change? What did you expect to change? What value did it change to? If nothing changed, why not? What can you do to make it change?

I agree. Here's the link:
subject: In need of much help with my main method.
It's not a secret anymore!