Results 1 to 5 of 5
  1. #1
    rosscortb is offline Member
    Join Date
    Apr 2017
    Posts
    1
    Rep Power
    0

    Default How do I get this code to work?

    Hello

    Newish to Java. I'm working my way through a book and can't get this code to run. Totally lost. Do I need to create two classes or can I put the whole code in one class? thanks Ross

    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;
    d.playTopHat();
    if (d.snare == true) {
    d.playSnare();
    }
    }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: How do I get this code to work?

    For a quick test it would suffice to keep it in one class. Lose the DrumKitTestDrive class and move its main-method to DrumKit.

    For more organized testing and perhaps a better overview, you can create two separate classes, where one is specifically for running the test, and keeping DrumKit as clean as possible.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do I get this code to work?

    Please explain what problems you are having.
    Copy and post here the full text of any error messages you are having problems with.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: How do I get this code to work?

    Additional hints: do not test for == true, but just the boolean variable itself.

    Example:
    Java Code:
    public class DrumKit {
       private boolean snare = true;
    
       public boolean hasSnare() {
           return snare;
       }
    
    }
    
    //some time later:
    if ( mydrumkit.hasSnare() ) {
       mydrumkit.playSnare();
    }
    This will prevent unmaintainable code like this below. Can you see when this code block will run?
    Java Code:
    if ( ! mySomething.initialized != false) {...}
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: How do I get this code to work?

    the code runs here as expected.

Similar Threads

  1. Will this code work
    By rajat16 in forum XML
    Replies: 3
    Last Post: 09-24-2010, 11:52 AM
  2. I can't get my code to work?
    By ComicStix in forum New To Java
    Replies: 5
    Last Post: 09-19-2010, 03:15 PM
  3. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 09:37 AM
  4. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 02:57 AM
  5. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 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
  •