• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How Do I Make This Example Compile & Run?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic