• Post Reply Bookmark Topic Watch Topic
  • New Topic

First OO Program - Placement of Loop  RSS feed

 
Kelli Cain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a program to ask the user to input how many cars they would like to consider, followed by asking them to choose between three car makes and three car colors. It needs to loop through prompting for make/model however many times they input in the first question and then display the make and color. I can't figure out where to put the while loop. It makes sense to me to go in the driver file immediately after prompting the user for number of cars, but it doesn't seem to want to work there. Does it go in the class file? Do I need to create a method that also includes input validation? I've attached both below for reference. Any insight that could be provided would be greatly appreciated.

Driver


Class
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right about where it should go. you should show us what exactly you've tried.
Also, have you learned about for loops yet? They seem better suited for the job, although a while would work as well.
 
Kelli Cain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joel, the for loop worked. However, I now need to have a method for validating input to ensure it's a valid integer. I tried using a method that I used on another program, but I'm receiving errors on assigning "num" as a String vs int. Thoughts?

Driver


Class
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, I want to talk about your classes. Classes are usually the blueprint for some sort of object, such as your Automobile class is. One would likewise expect your other class, AutomobileDriver, to provide the blueprint for a person who drives an automobile. However, that class only serves to house your main method. Even though it allows for interaction with automobile drivers, it in itself is not being used to make AutomobileDrivers. So, I would name it something like Main or AutomobileMain. I know this seems really nit-picky, but naming is the essence of OO.

Now onto methods. Methods are named pieces of functionality that objects or (in the case of static methods) classes know how perform. So, do Automobiles need to know how to check for valid, positive integers? No, automobiles drive; they don't do error checking. In the case of static methods, do blueprints of automobiles need to know about valid integers? Nope. Therefore, I'd suggest moving that method to your Main class because that's the class where you're trying to use that method. And you can mark it as private because you don't need to access it from any other classes.

This should also take care of your first error. The cause of your error was that you were trying to use that method as if it were already in the Main class. When calling a public method from a different class you must first type the object name, a dot (.), and then the method. In the case of static methods, you must type the class name and a dot. When the method you are calling is in the same class from which you are calling it, the method name suffices.

You have some other errors, though, when you do:



num is a String variable, but you are trying to assign an int to it. In your for loop, num refers to a String, but it needs an int. Even though you've verified that num holds an int, the compiler doesn't know that. Therefore, in your for loop logic, you can simply replace num with Integer.parseInt(num).

EDIT: I guess I have some more things to suggest. Firstly, you very rarely need to use the words true and false in loops and if statements. while (!isValidPositiveInteger(num)) is preferred over while (isValidPositiveInteger(num)== false). Using if (someBoolean == true) is considered superfluous as well, as it can be simply shortened to if (someBoolean).

Secondly, classes conventionally don't have methods like this: public void printName(). The preferred version would be public String getName(). Calling the method will then give you a String, which you can print at will.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!