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
Caused by: java.lang.ClassNotFoundException: Test4
at java.security.AccessController.doPrivileged(Native Method)
Could not find the main class: Test4. Program will exit.
Exception in thread "main"
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.
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.
Joined: Mar 24, 2008
[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.
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
Joined: Mar 24, 2008
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.