Results 1 to 9 of 9
  1. #1
    gordlonious is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Code Exercise From Book

    Here's the code:
    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();
    		}
    	}
    }
    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!
    Last edited by gordlonious; 11-29-2012 at 03:02 AM.

  2. #2
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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.

  3. #3
    gordlonious is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  4. #4
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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.

  5. #5
    gordlonious is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default 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?

  6. #6
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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.

  7. #7
    gordlonious is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Code Exercise From Book

    Quote Originally Posted by gordlonious View Post
    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!
    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Code Exercise From Book

    gordlonious, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. exercise book
    By aizen92 in forum Forum Lobby
    Replies: 3
    Last Post: 02-03-2012, 01:05 AM
  2. Images not loading (Using code from a book)
    By Nanashi in forum New To Java
    Replies: 5
    Last Post: 11-15-2011, 02:33 PM
  3. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  4. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •