Results 1 to 9 of 9
Thread: Code Exercise From Book
- 11-29-2012, 02:30 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Code Exercise From Book
Here's the code:
This is all in one source file, the book even says that the code all represents one complete source file. It doesn't compile, I get an Error stating that it can't find the main method in class DrumKit. Then, a friend told me that each class should be in a seperate file, tried that, both will compile but won't run. I doing these from the command prompt. Please help!Java Code:class DrumKit { boolean topHat = true; boolean snare = true; void playTopHat () { System.out.println("ding ding da-ding"); } void playSnare() { System.out.println("bang bang ba-bang"); } } class DrumKitTestDrive { public static void main(String [] args) { DrumKit d = new DrumKit(); d.playSnare(); d.snare = false; // why false here but true in class DrumKit? d.playTopHat(); if (d.snare == true) { d.playSnare(); } } }Last edited by gordlonious; 11-29-2012 at 03:02 AM.
- 11-29-2012, 02:48 AM #2
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Re: Code Exercise From Book
Please use [ CODE] [ /CODE](no spaces) to contain your code. Also, it works fine for me, so I would like more info. What IDE are you using, stuff like that.
Last edited by Darkzombies; 11-29-2012 at 02:58 AM.
- 11-29-2012, 03:04 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Code Exercise From Book
I just set the the environment varables to my C:\Java\jdk1.7.0_09\bin folder and am compiling and running from the command prompt using the javac and java commands. No IDE.
- 11-29-2012, 03:07 AM #4
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Re: Code Exercise From Book
Well, I figured it was either that or an IDE, but I couldn't think of the word. Either way, it works fine for me. So it's probably something to do with the path, but who knows.
- 11-29-2012, 03:17 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Code Exercise From Book
What? how could it have something to do with the path? and, should all my classes be in different files?
- 11-29-2012, 03:25 AM #6
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Re: Code Exercise From Book
Since they are different classes, they should be in different .java files, also there needs to be a .class file for each as well.
- 11-29-2012, 03:33 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Code Exercise From Book
well, that's what the compiler does for me, correct? creates the .class files for me to then execute? Also, i added C:\Java\jdk1.7.0_09\bin to the end of the PATH in the environment variables, idk where else to go with that.
- 11-29-2012, 04:11 AM #8
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Code Exercise From Book
From what I can see in you code this is should compile. Even if you place both class in a single file, for instance in a file called DrumKit.java. You obviously cannot run the DrumKit class because it doesn't have the main(String[] args) method. You can only run the DrumKitTestDrive class becuase it has the main method defined.
Website: Learn Java by Examples
- 11-29-2012, 05:23 AM #9
Re: Code Exercise From Book
gordlonious, please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
exercise book
By aizen92 in forum Forum LobbyReplies: 3Last Post: 02-03-2012, 01:05 AM -
Images not loading (Using code from a book)
By Nanashi in forum New To JavaReplies: 5Last Post: 11-15-2011, 02:33 PM -
I/O exercise
By Feldom in forum New To JavaReplies: 1Last Post: 10-28-2007, 04:48 PM -
help with exercise
By e_as're in forum New To JavaReplies: 3Last Post: 09-25-2007, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks