Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: Why isn't this code working?

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why isn't this code working?

    Ok so I have a problem.

    You operate several hot dog stands distributed throughout town. Define a class named HotDogStand that has a member variable for the hot dog stand's ID number and a member variable for how many hot dogs the stand has sold that day. Create a constructor that allows a user of the class to initialize both values.
    Also create a method named justSold that increments the number of hot dogs the stand has sold by one. The idea is that this method will be invoked each time the stand sells a hot dog so that we can track the total number of hot dogs sold by the stand. Add another method that returns the number of hot dogs sold.

    Finally, add a static variable that tracks the total number of hotdogs sold by all hot dog stands and a static method that returns the value in this variable.

    Write a main method to test your class with at least three hot dog stands that each sell a variety of hot dogs.

    private static int totalSold = 0;
     
          public static int getTotalSold()
          {
                return totalSold;
          }
     
     
          private String name;
     
          private int id;
     
          private int numSold;
     
          public HotDogStand(String name, int id)
          {
                this.name = name;
                this.id = id;
          }
     
          public void setJustSold()
          {
                numSold++;
                totalSold++;
          }
     
          public int getNumSoldToday()
          {
                return numSold;
          }
     
          public int getID()
          {
                return id;
          }
     
          public String getName()
          {
                return name;
          }
     
          public String toString()
          {
                return name+" ("+id+") - "+numSold;
          }
     
          public HotDogStand(HotDogStand rhs)
          {
                id = rhs.id;
                name = ""+rhs.name;
                numSold = rhs.numSold;
          }
    }
     
    class Test
    {
     
          public static void main(String[] args)
          {
     
                HotDogStand one = new HotDogStand("one", 1);
                HotDogStand two = new HotDogStand("two", 2);
                HotDogStand three = new HotDogStand("three", 3);
     
                System.out.println(one);
                System.out.println(two);
                System.out.println(three);
     
                System.out.println("Total: "+HotDogStand.getTotalSold());
     
                one.setJustSold();
                two.setJustSold();
                three.setJustSold();
     
                System.out.println("Name\tID\tNumber Sold");
                System.out.println("________________________________");
                System.out.println(one.getName()+"\t"+one.getID()+"\t"+one.getNumSoldToday());
                System.out.println(two.getName()+"\t"+two.getID()+"\t"+two.getNumSoldToday());
                System.out.println(three.getName()+"\t"+three.getID()+"\t"+three.getNumSoldToday()+"\t");
     
                System.out.println("Total: "+HotDogStand.getTotalSold());
     
                HotDogStand four = new HotDogStand(one);
          }
    }

    How come when I run it nothing shows up?


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Why isn't this code working?

    Hello tai8!
    I just run your code and it printed what it was supposed to.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    nothing shows up?
    Please copy and paste here the console from when you execute the program.
    I see lots of println statements, some of them will print if the program executes.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by Norm View Post
    Please copy and paste here the console from when you execute the program.
    I see lots of println statements, some of them will print if the program executes.
    All it says is this:

    run:
    BUILD SUCCESSFUL (total time: 2 seconds)

    What is it supposed to show?

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by andreas90 View Post
    Hello tai8!
    I just run your code and it printed what it was supposed to.
    Really? What IDE did you use and what does your output look like?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    Can you use the java command to execute your program in a command prompt window?
    run:
    BUILD SUCCESSFUL (total time: 2 seconds)
    The output you posted is from the IDE not from the java program.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by Norm View Post
    Can you use the java command to execute your program in a command prompt window?

    The output you posted is from the IDE not from the java program.
    How do i do that?

  8. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by tai8 View Post
    Really? What IDE did you use and what does your output look like?
    I run it on NetBeans. Here is the output.


    run:
    one (1) - 0
    two (2) - 0
    three (3) - 0
    Total: 0
    Name ID Number Sold
    ________________________________
    one 1 1
    two 2 1
    three 3 1
    Total: 3
    BUILD SUCCESSFUL (total time: 2 seconds)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    Open a command prompt window change directory to the folder with the class file and enter:
    java <THECLASSNAME>
    If Test is the class with the main() method:
    Java Test
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by andreas90 View Post
    I run it on NetBeans. Here is the output.


    run:
    one (1) - 0
    two (2) - 0
    three (3) - 0
    Total: 0
    Name ID Number Sold
    ________________________________
    one 1 1
    two 2 1
    three 3 1
    Total: 3
    BUILD SUCCESSFUL (total time: 2 seconds)
    I'm using NetBeans too. How did you get that? Did you copy and pasted exactly what I posted and ran it or did you change anything to the code at all?

    Oh I forgot to mention I also have this right before the code i posted:

    package assignment.pkg5.pkg1;

    public class HotDogStand {

    What did you have before the code?

  11. #11
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by tai8 View Post
    I'm using NetBeans too. How did you get that? Did you copy and pasted exactly what I posted and ran it or did you change anything to the code at all?

    Oh I forgot to mention I also have this right before the code i posted:

    package assignment.pkg5.pkg1;

    public class HotDogStand {

    What did you have before the code?
    Just copy-pasted your code in a public class. I had no package. Did you try the cmd as Norm suggested? I did it and it worked too.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    My suggestion might not work if the class is in a package and the class files are not in the correct folders.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    tai8 (March 19th, 2012)

  14. #13
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Why isn't this adding up?

    Ok so i finally got the program to output but the problem is the sum of the output is wrong. This is what i have:


     class HotDogStand
    {
     
    private int id;
    private int numSold;
    private static int totalSold = 0;
     
    public HotDogStand()
    {
    id = 0;
    numSold = 0;
    totalSold = 0;
    }
    public HotDogStand(int newId, int newNumSold)
    {
    id = newId;
    numSold = newNumSold;
    }
    public int getId()
    {
    return id;
    }
    public void setId(int newId)
    {
    id = newId;
    }
    public int getNumSold()
    {
    numSold ++;
    return numSold;
    }
    public static int getTotalSold()
    {
    return totalSold;
    }
    public void justSold()
    {
    totalSold = numSold ++;
    }        
    }
     
    public class program2JS
    {
    public static void main (String [] args)
    {
    HotDogStand s1 = new HotDogStand();
    HotDogStand s2 = new HotDogStand();
    HotDogStand s3 = new HotDogStand();
    s1.getId();
    s2.getId();
    s3.getId();
    s1.setId(1);
    s2.setId(2);
    s3.setId(3);
    s1.getNumSold();
    s2.getNumSold();
    s3.getNumSold();
    s1.justSold();
    s2.justSold();
    s3.justSold();
    s1.getTotalSold();
     
    int i;
    for(i = 0; i <=5; i++)
    s1.justSold();
    for(i = 0; i <=5; i++)
    s2.justSold();
    for(i = 0; i <=5; i++)
    s3.justSold();
    for(i = 0; i <=5; i++)
    s1.getNumSold();
    for(i = 0; i <=5; i++)
    s2.getNumSold();
    for(i = 0; i <=5; i++)
    s3.getNumSold();
     
    int getTotalSold = (s1.getNumSold() + s2.getNumSold() + s3.getNumSold());
     
    //Test our code with three hot dog stands
    //sold at stand 1, 2
     
    s1.justSold();
    s2.justSold();
    s1.justSold();
     
     
    System.out.println("Stand " + s1.getId() + " Sold " + s1.getNumSold());
    System.out.println("Stand " + s2.getId() + " Sold " + s2.getNumSold());
    System.out.println("Stand " + s3.getId() + " Sold " + s3.getNumSold());
    System.out.println("Total sold = " + getTotalSold);
     System.out.println();
    //Sold some more
     
    s3.justSold();
    s1.justSold();
     
     
    System.out.println("Stand " + s1.getId() + " Sold " + s1.getNumSold());
    System.out.println("Stand " + s2.getId() + " Sold " + s2.getNumSold());
    System.out.println("Stand " + s3.getId() + " Sold " + s3.getNumSold());
    System.out.println("Total sold = " + getTotalSold);
    System.out.println();
    }
    }

    When i run it i get this:

    Stand 1 Sold 18
    Stand 2 Sold 17
    Stand 3 Sold 16
    Total sold = 45

    Stand 1 Sold 20
    Stand 2 Sold 18
    Stand 3 Sold 18
    Total sold = 45

    20 + 18 + 18 is not 45 so can someone please help me here?
    Last edited by tai8; March 19th, 2012 at 05:14 PM.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    You have used the wrong tags for the code.

    What does the justSold() method do? Does it change any of the data included in the total? If so, then you need to recompute the total AFTER changing the data.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why isn't this code working?

    Sorry, but that is some weird coding practice.

    Anyway, the reason it always sums up to 45 is because for every justSold() and getNumSold() call, you increment your variables by 1.
    If you count how many times they are all called, it adds up to 45 (42 when you exit the for loops, then an additional 3 calls here : int getTotalSold = (s1.getNumSold() + s2.getNumSold() + s3.getNumSold())).
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  17. The Following User Says Thank You to newbie For This Useful Post:

    tai8 (March 19th, 2012)

  18. #16
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by newbie View Post
    Sorry, but that is some weird coding practice.

    Anyway, the reason it always sums up to 45 is because for every justSold() and getNumSold() call, you increment your variables by 1.
    If you count how many times they are all called, it adds up to 45 (42 when you exit the for loops, then an additional 3 calls here : int getTotalSold = (s1.getNumSold() + s2.getNumSold() + s3.getNumSold())).
    So how would i fix it then? Do i just delete all the loops then?

  19. #17
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Why isn't this code working?

    Just rework your logic.

    Plan what you want to do, write down on paper how you would do it in the real world, then convert that logic into Java.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  20. #18
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by newbie View Post
    Just rework your logic.

    Plan what you want to do, write down on paper how you would do it in the real world, then convert that logic into Java.
    The problem is i'm not sure why the numbers for hot dog stands 1, 2, and 3 are not adding up. I know what i want to do, i just don't know how to get there.

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    You can not change the values used to compute a sum after you compute the sum.
    for example
    x = y+ z;
    y = y = 2;
    now x does not equal the current value of y + z because y has been changed/
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by Norm View Post
    You can not change the values used to compute a sum after you compute the sum.
    for example
    x = y+ z;
    y = y = 2;
    now x does not equal the current value of y + z because y has been changed/
    I don't think changing the order does anything. I just tried and nothing. I dont know why the 3 stands are not adding together. what is wrong with the code there?

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    Move this line to just in front of where you print out the value of getTotalSold.
    Have these two line right next to each other.
    int getTotalSold = (s1.getNumSold() + s2.getNumSold() + s3.getNumSold());
    System.out.println("Total sold = " + getTotalSold);
    If you don't understand my answer, don't ignore it, ask a question.

  24. #22
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    Quote Originally Posted by Norm View Post
    Move this line to just in front of where you print out the value of getTotalSold.
    Have these two line right next to each other.
    int getTotalSold = (s1.getNumSold() + s2.getNumSold() + s3.getNumSold());
    System.out.println("Total sold = " + getTotalSold);
    I tried it and still nothing. sigh... i want to die

  25. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    Post the new code and its output to show what is happening..
    If you don't understand my answer, don't ignore it, ask a question.

  26. #24
    Member
    Join Date
    Feb 2012
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't this code working?

    my god i really dont know... anyone?

  27. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,751
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: Why isn't this code working?

    If you don't post the current code and its output, how can anyone help you fix it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: March 12th, 2012, 03:23 PM
  2. I don't get why this code isn't working
    By tai8 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: February 20th, 2012, 11:38 AM
  3. Code working only on ide
    By xeyos in forum Java Networking
    Replies: 0
    Last Post: November 17th, 2011, 04:40 PM
  4. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM
  5. Getting code working with gui
    By Nostromo in forum AWT / Java Swing
    Replies: 2
    Last Post: March 21st, 2011, 09:34 PM