aspose file tools*
The moose likes Beginning Java and the fly likes How Do I Make This Example Compile & Run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Do I Make This Example Compile & Run?" Watch "How Do I Make This Example Compile & Run?" New topic
Author

How Do I Make This Example Compile & Run?

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
I am trying to better understand interfaces. This example is from my Java 2 Certification book from Osborne Press. I should be working, but instead I get a compile error in ShowGrowth.java saying that it expects to see the interface defined.
Also, the book stated that there is no need to use an import statement to access the Runnable interface, because it is part of the java.lang package which is automatically imported. Lastly, the run method is part of the Runnable interface.
The book listed the below ExponentialGrowth class code and ShowGrowth class code, but did not explain if/how they could run in separate files. So, I placed class ShowGrowth inside class ExponentialGrowth as follows:

Q1) Why does this generate a compile error?
Q2) Am I correct in putting ShowGrowth inside ExponentialGrowth?
Q3) Is there anyway I could run this with the 2 classes in
separate files?
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited July 23, 2001).]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Gary
There are several things wrong with the code you posted:
Originally posted by Gary Farms:

public class ExponentialGrowth implements Runnable
{
public final double INTEREST = 0.10;
public void run()
{
int dollars = 1;
int years = 0;
int n=0;
while(n<4)
{
System.out.println("$" + dollars + " after " + years
+ " years");
dollars *= (1 + INTEREST);
years += 1;
n +=1;
}
}
Class ShowGrowth
{
public static void main(String[] args)
{
ExponentialGrowth growth = new ExponentialGrowth();
Thread compoundThread = new Thread(growth);
CompoundThread.start();
}
}

}

The first thing wrong is there are a couple of syntax errors. you should try to post code that doesn't have basic errors like these in it. Some people will think you just typed in any-old-thing so you'd get a response. (In your main method you have compoundGrowth spellled with both capital and small case C. In your declaration for the inner class you have class spelled with a capital C.)
You have the main method in your inner class, the inner class can not have a static method unless the class itself is a static member class. Also because you have the main method in the inner class when you run the program it will not run, you'll get an exceptions aying it can't find the main method. It looks for main in the public class in the source file. You can have only one public class per source file.
To solve your problem you can just take ShowGrowth out of the ExponentialGrowth class and put it right after the ExponentialGrowth class code. You can have as many classes as you like in one file provided that only one of them is public.
hope that helps

Dave


[This message has been edited by Dave Vick (edited July 23, 2001).]


Dave
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes, they could each be in their own files with names that match the class name and life would be good (actually better).
PS the word class should not be capitalized.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Do I Make This Example Compile & Run?