File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes calculating stopping distance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calculating stopping distance" Watch "calculating stopping distance" New topic
Author

calculating stopping distance

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
This code is partially from the book Simply Programming An Application Driven Tutorial Approach. Some of the code I have inserted myself to complete the assignment, which is:

The user enters a starting and stopping speed range and the program produces a chart of speeds and stopping distances.
The module should receive 1 data (argument=parameter), an integer representing the speed of travel. The module should convert that speed into the number of feet needed to stop using the formula given above. The module should return the number of feet.

I don't want any answers but just look at my code if you would be so kind and drop some hints as to what I am doing wrong. I keep getting an error message that says:



C:\Test4.java:147: class, interface, or enum expected
}
^

And:

--------------------Configuration: <Default>--------------------
java.lang.NoClassDefFoundError: Test4
Caused by: java.lang.ClassNotFoundException: Test4
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Test4. Program will exit.
Exception in thread "main"
Process completed.



I am having trouble figuring out how to return more than one value from this method. From the picture I attached it appears I would have to return several values to have calculated all of those stopping distances between the first and the last one. The only thing I could think of was to put the method in a for loop and call it as many times as needed to keep delivering the return values but I can't seem to get it right.

Any help would be appreciated.

Thanks,
Von


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The error message is saying that at or about line 147, the compiler is expecting to find the start of another class. Or more specifically, the start of another class, interface, or enum. But instead it's finding something else -- it doesn't say what.

But you didn't intend to start a second class in that same Java file, did you? So that means you have something extra at the very end which is confusing the compiler.

Your braces seem to be matched up very nicely, although it's always hard to tell that by looking at the code. Easy to miss an unmatched brace. But assuming your brace-matching is all okay, then what's with the two braces at the same level of indentation at the very end? That doesn't look right to me.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
[size=12]Thank you, Paul. Well, I fixed the braces and managed to get rid of that error message, but now I only get one stopping distance value and I need all the stopping distances for several starts and stops. This is where I am really stumped. Here is my fixed code:




I keep thinking I can loop those returns in a for loop but I'm just not getting it right. I've been looking at it for over 20 hours in the last week so my eyes are going batty about now. I'm going to look over my for loop more closely.

Thanks,
Von
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11243
    
  16

i edited your post - you had 'quote' tags around your source, so I changed them to 'code' tags.

I don't know what the problem is, but i would suggest using a lot of System.out.println() statements. If your for loop isn't doing what you think it should, put a bunch in and around it. Print out what the relevant variables are before you enter it, what your flag is on each loop around, print out a variable each time it is changed, and print out the various variables right after you exit it.

Note that a method can only return ONE thing. Your calcSpeed method will only ever return one double. if you want it to return a bunch of things, you should probably change it to return an array.

But also...why does a method called "calcSpeed" return feet? That doesn't make sense. When I see something called that, I would expect it to return a value that represents a speed. If you are calculating the stopping distance, I would call it "calcStopDist", or something like that.

Further, I would make it calculate the distance for a single speed. You pass it X mph, and it returns Y feet. If you wan't to calculate the distances for several speeds, I would write another method that calls this method multiple times.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
I did it!!! I fixed everything and it now works!

Here is my fixed code:




I had various little mistakes like int where there should have been a double, and x where miles should have been! Whew! Just posting it here made me look at it with a keener eye.

Thanks everyone!
Vonique
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calculating stopping distance