Why is there no output?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

54 Replies - 3856 Views - Last Post: 19 March 2012 - 08:21 PM Rate Topic: -----

#1 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Why is there no output?

Posted 19 March 2012 - 07:16 AM

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 this there is no output? Nothing is showing up at all.

Is This A Good Question/Topic? 0
  • +

Replies To: Why is there no output?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Why is there no output?

Posted 19 March 2012 - 07:23 AM

This won't even compile as you've presented it, so there must be something missing, but the basic problem is that you've got an inner class with your main method in it, and java won't see that.

When you run a class file, Java looks for a main method in the public class declared in that file.

If you run "java Foo", java will look for the file Foo.class and try to run the method with the signature public static void main(String[]) {}

that it finds in that class. It's not going to look in some inner class Test that happens to be declared in the same file.
Was This Post Helpful? 0
  • +
  • -

#3 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Re: Why is there no output?

Posted 19 March 2012 - 07:33 AM

View Postjon.kiparsky, on 19 March 2012 - 07:23 AM, said:

This won't even compile as you've presented it, so there must be something missing, but the basic problem is that you've got an inner class with your main method in it, and java won't see that.

When you run a class file, Java looks for a main method in the public class declared in that file.

If you run "java Foo", java will look for the file Foo.class and try to run the method with the signature public static void main(String[]) {}

that it finds in that class. It's not going to look in some inner class Test that happens to be declared in the same file.


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

package assignment.pkg5.pkg1;

public class HotDogStand {

So what do I about that inner class thing?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: Why is there no output?

Posted 19 March 2012 - 07:44 AM

Move your main() method outside the inner/nested class.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Why is there no output?

Posted 19 March 2012 - 07:49 AM

View Postjonjones, on 19 March 2012 - 09:33 AM, said:

So what do I about that inner class thing?



The main method must be a method of a public class. So make it a member of the public class (in this case, HotDogStand)

Note that there's nothing illegal about having a method called main in an inner class, even one that is public and static and takes an array of Strings as an argument, but java won't see it when it goes to run the program and is looking for a main method. It'll only look in the public class declared in that class file.
Was This Post Helpful? 0
  • +
  • -

#6 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Why is there no output?

Posted 19 March 2012 - 08:32 AM

one (1) - 2
two (2) - 3
three (3) - 4
Total: 9
Name ID Number Sold
________________________________
one 1 2
two 2 3
three 3 4
Total: 9




Is this want you want as Output ?
Was This Post Helpful? 0
  • +
  • -

#7 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Re: Why is there no output?

Posted 19 March 2012 - 09:25 AM

So after closer examination I realize i was answering the question wrong so i changed it up a bit and here is what i got:

package assignment.pkg5.pkg1;

/**
 *
 * 
 */
import java.util.Scanner;

public 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 int getTotalSold()
{
return totalSold;
}
public void justSold()
{
totalSold = numSold ++;
}
}
 
public class Assignment51
{
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 = 1; i <=5; i++)
s1.justSold();
for(i = 1; i <=5; i++)
s2.justSold();
for(i = 1; i <=5; i++)
s3.justSold();
for(i = 1; i <=5; i++)
s1.getNumSold();
for(i = 1; i <=5; i++)
s2.getNumSold();
for(i = 1; i <=5; i++)
s3.getNumSold();
 
int TotalSold = s1.getTotalSold() + s2.getTotalSold() + s3.getTotalSold();
 

 
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 = " + TotalSold + "\n");
 

 
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 = " + TotalSold + "\n");
}
}


There are two errors that still show up though. They are:

public class HotDogStand { - class HotDogStand is public, should be declared in a file named HotDogStand.java

public class Assignment51 - for here it says duplicate class: assignment.pkg5.pkg1.Assignment51

Why does it say those things for those two lines? I saved my project as Assignment51.java by the way and I'm currently using NetBeans. If someone can help me with this I would be sooo grateful. Thank you.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Why is there no output?

Posted 19 March 2012 - 09:38 AM

Looks like you want HotDogStand to represent the Hot Dog Stand, and Assignment51 to be the runner/tester. Try using two files - HotDogStand.java and Assignment51.java.



Also, this line

s1.getId();


and the others like it will produce no visible output. They'll evaluate to the ID for s1, but if you want to do anything with that, like print it or write it to a file or look it up on the web or make it into a dancing animated graphic or whatever, you have to say what it is you want to do.
Was This Post Helpful? 1
  • +
  • -

#9 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Re: Why is there no output?

Posted 19 March 2012 - 09:44 AM

View Postjon.kiparsky, on 19 March 2012 - 09:38 AM, said:

Looks like you want HotDogStand to represent the Hot Dog Stand, and Assignment51 to be the runner/tester. Try using two files - HotDogStand.java and Assignment51.java.



Also, this line

s1.getId();


and the others like it will produce no visible output. They'll evaluate to the ID for s1, but if you want to do anything with that, like print it or write it to a file or look it up on the web or make it into a dancing animated graphic or whatever, you have to say what it is you want to do.


Ah so I'm supposed to do them on two separate java files? Is there a way to combine them into one afterwards?
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Why is there no output?

Posted 19 March 2012 - 09:45 AM

Why would you want to do that?
Was This Post Helpful? 0
  • +
  • -

#11 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Re: Why is there no output?

Posted 19 March 2012 - 09:54 AM

View Postjon.kiparsky, on 19 March 2012 - 09:45 AM, said:

Why would you want to do that?


I just tried doing what you said. I deleted everything up until where it says public class Assignment51 and it still says the same thing on how it's a duplicate class.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Why is there no output?

Posted 19 March 2012 - 09:57 AM

Okay, you might want to delete the Assignment51.class file and try again.

EDIT: .CLASS!! not Assignment51.java!

This post has been edited by jon.kiparsky: 19 March 2012 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#13 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Re: Why is there no output?

Posted 19 March 2012 - 10:14 AM

View Postjon.kiparsky, on 19 March 2012 - 09:57 AM, said:

Okay, you might want to delete the Assignment51.class file and try again.

EDIT: .CLASS!! not Assignment51.java!


So just delete the entire public class Assignment 51? I did and it just brought up more errors..
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7893
  • View blog
  • Posts: 13,424
  • Joined: 19-March 11

Re: Why is there no output?

Posted 19 March 2012 - 10:28 AM

Okay, now you should be able to recompile it from the Assignment51.java file.


You need to have two files. One is called Assignment51.java, and it contains the class Assignment51. The other is called HotDogStand.java, and it has the class HotDogStand.
Was This Post Helpful? 0
  • +
  • -

#15 jonjones  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 12-March 12

Re: Why is there no output?

Posted 19 March 2012 - 10:34 AM

View Postjon.kiparsky, on 19 March 2012 - 10:28 AM, said:

Okay, now you should be able to recompile it from the Assignment51.java file.


You need to have two files. One is called Assignment51.java, and it contains the class Assignment51. The other is called HotDogStand.java, and it has the class HotDogStand.


When i try to run it as it is this is what it says on the output:

java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - class Assignment51 is public, should be declared in a file named Assignment51.java
at assignment.pkg5.pkg1.Assignment51.<clinit>(HotDogStand.java:54)
Could not find the main class: assignment.pkg5.pkg1.Assignment51. Program will exit.
Exception in thread "main" Java Result: 1

Do you know what this means?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »