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 14 of 14

Thread: Balloon.java, what in the world do i do?

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Balloon.java, what in the world do i do?

    This is the first time i'm ever attempting java. My professor has given us an assignment to follow the balloon.java code and we are to create a tester for it.

    I dont even know where to begin this thing! i'm supposed to create a ballon with a name i choose and set it to 100 meters
    public class BalloonTester
        {
    	// instance variables
      	private String balloonthing ; 			// name of the balloon
      	private int 100;     		// altitude (height) of balloon in meters
     
    	/**
       	 * Create a balloon object with a given name at a given altitude.
         * @param theName the name of the balloon object
         * @param theAltitude the altitude
         */
      	public Balloon(String balloonthing, int 100) //THIS portion ALWAYS gives me an error no matter what i type.
      	{
      		name = balloonthing ;
      		// make sure altitude is not negative!
      		altitude = Math.max(100,0) ;
    }
    }
    i'm so miserably lost =/
    Last edited by copeg; September 11th, 2010 at 10:54 PM. Reason: Please use the code tags


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Balloon.java, what in the world do i do?

    Hello toterzuko

    I think you really need to go over course material or the lecture again. The mistakes you have made shows, that you are off on the very fundamentals of java. This will haunt you in further development.

    Here is what I think the exercise is about: Making a tester class that instatiates the balloon. You do this by calling the constructor of balloon.

    //in the balloontester class
    ...
      Balloon balloon = new Balloon("name", 100);
    ...

    Your notable mistakes:
    Using undeclared variables.
    Trying to make the Balloon constructor inside the Tester class
    Calling a method in the Math class - I dont know what you where trying to do here, but its not needed. Just put the int in your height variable.

    Hopes this helps you to get further.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Balloon.java, what in the world do i do?

    public class BalloonTester
    ...
    public Balloon(String balloonthing, int 100) //THIS portion ALWAYS gives me an error no matter what i type.

    This is something very subtle in Java. The name of the Object you are creating has to match the name of the class it is in (with the exception of nested classes, but dont worry about those right now).

    So, the name of your class is BalloonTester but the name of the Object you are making is Balloon. Since the name of your class is BalloonTester, the name of your Object should also be BalloonTester. So, here is what that would look like:
    public class BalloonTester
    ...
    public BalloonTester(String balloonthing, int 100)

    BUT, we have more issues. One of the rules in Java is that variable names MUST start with a letter. Look at the line from your code:
    private int 100;
    You need to give this variable a name that doesnt start with a number. Here are some examples:
    //You could type out the word 100
    private int hundred;
    //Or you can put a letter in the front and then type the numbers
    private int h100;

    This change also needs to be made in your constructor:
    public Balloon(String balloonthing, int 100)

    While we are talking about your constructor, there is another change that should take place. You should not have the names of the local variables in your constructor (or anywhere in your program) have the same name as the class variables (its early, I cant think of what they are actually called).

    Lastly, the code in your constructor:
    name = balloonthing ;
    // make sure altitude is not negative!
    altitude = Math.max(100,0) ;
    Implies your class variables should be named name and altitude instead of balloonthing and 100.


    Tell me if I lost you, I just woke up and I might not be very concise.

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Balloon.java, what in the world do i do?

    i thought every code had to start with
    public static void main(String [] args)

    for this tiny part of the code im supposed to make the balloon and have it be 100 meters, then im to make a second balloon and have it be -100 meters. the prof. told us to choose w.e name we wanted for it but i have no idea which one do i change for that to take place. i tried changing the private String name; and replacing the name part with what i wanted which then gave me an error where it says name = theName

    public class BalloonTester
    {
        // instance variables
          private String name ;             // name of the balloon
          private int altitude;             // altitude (height) of balloon in meters
     
          public BalloonTester(String theName, int theAltitude)
          {
              name = theName ;
              // make sure altitude is not negative!
              altitude = Math.max(theAltitude,0) ;
          }
    }

    the professor also gave us the little code portion that says
    Balloon myBalloon = new Balloon("red",100);
    do i need two of those since i'm making the second balloon also? where would the proper placement even be!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon.java, what in the world do i do?

    You could name you ballon objects: myBallon1 and myBallon2

    gave me an error
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Balloon.java, what in the world do i do?

    Quote Originally Posted by toterzuko View Post
    i thought every code had to start with
    public static void main(String [] args)

    for this tiny part of the code im supposed to make the balloon and have it be 100 meters, then im to make a second balloon and have it be -100 meters. the prof. told us to choose w.e name we wanted for it but i have no idea which one do i change for that to take place. i tried changing the private String name; and replacing the name part with what i wanted which then gave me an error where it says name = theName

    public class BalloonTester
    {
        // instance variables
          private String name ;             // name of the balloon
          private int altitude;             // altitude (height) of balloon in meters
     
          public BalloonTester(String theName, int theAltitude)
          {
              name = theName ;
              // make sure altitude is not negative!
              altitude = Math.max(theAltitude,0) ;
          }
    }

    the professor also gave us the little code portion that says
    Balloon myBalloon = new Balloon("red",100);
    do i need two of those since i'm making the second balloon also? where would the proper placement even be!
    Ok, let me step you through this because it is JAVA 101 and I want to make sure it makes sense to you.

    There are three types of Classes in JAVA.
    1) The Main Class
    2) An Object Class
    3) An Interface Class

    When making an application (opposed to an applet), every program MUST have a Main Class. The other two are optional and I will mention them later.

    The Main Class is the Class that is the RUN Class for the JVM (Java Virtual Machine). You cannot have more than 1 Main Class in your program. The Main Class is recognized by two things:
    1) The method: public static void main(String[] args)
    2) The variables and methods being static (probably not relevant for what you are doing)

    An Object Class is a Class that is created as the framework for an Object. This Class contains all of the variables and methods that are local and important for the Object. The Object Class is used by other Classes to create and use Objects. A program does not necessarily need an Object Class.

    An Interface Class is a Class that contains undeclared methods to be inherited by an Object Class. It is more to create restrictions for Objects to categorize them. For you, this is completely irrelevant for what you are doing and we won't confuse you with this for now.


    So, for your Balloon Project, you need two classes. Class 1 is your Main. Class 2 is your Balloon Object.

    Your Main will look like this:
    public class BalloonTester
    {
    	public static void main(String[] args)
    	{
    		/* Create Your Balloon Object here and send it
    		 * the variables specified in the Balloon Object
    		 * Constructor in the other Class
    		 */
    	}
    }
    And your Balloon Object Class will look like this:
    public class Balloon
    {
        // instance variables
          private String name ;             // name of the balloon
          private int altitude;             // altitude (height) of balloon in meters
     
          public Balloon(String theName, int theAltitude)
          {
              name = theName ;
              altitude = Math.max(theAltitude,0) ;
          }
    }

    I'm letting you add the little amount of code needed in the Main Method in the Main Class because I would think you know how to do it. Basically, you want to create a Balloon Object and pass the appropriate variables. I do not know if your teacher wants to put the variables into the code or if s/he wants you to read from an exterior input.

    If you need any additional help or explanation, I'll help as much as I can.
    Last edited by aussiemcgr; September 12th, 2010 at 06:42 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon.java, what in the world do i do?

    every program MUST have a Main Class
    Are you talking about some IDE?
    The JVM does NOT require ANY class named Main or any other specially named classes.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Balloon.java, what in the world do i do?

    I'm not talking about a class named Main, but a Main Class (a class with the Main).

    If I'm wrong, that is news to me. I know applets are different, but for Applications, all programs need a Main.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon.java, what in the world do i do?

    Why do you captialize Main? I'm confused by that.
    All java programs are in classes. To start the execution of a java program using the java command, you specify the name of a class containing the method: public static void main(String[] args).

    Do you call a class with the above main() method the Main class?
    It could be the only class, so calling it the Main one is redundant.

    All the classes in a program could have a main() method. There is no restriction there.

  10. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Balloon.java, what in the world do i do?

    Hey guys. I'm doing the same assignment and this is what I have so far with the errors I received:

    public class BalloonTester
    {

    private String Balloon1;
    private int hundred;

    public BalloonTester(String theName, int theAltitude); error: missing method body, or declare abstract
    {
    Balloon1 = theName; error: cannot find symbol, symbol: variable theName/Altitude,
    hundred = Math.max(theAltitude,0); location: class BalloonTester
    }


    Thanks for the help.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Balloon.java, what in the world do i do?

    Please post the full text of the errors from the compiler. Your edited version has left out useful information.

  12. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Balloon.java, what in the world do i do?

    Quote Originally Posted by Norm View Post
    Why do you captialize Main? I'm confused by that.
    I was trying to separate the difference between a class with a main method and an object class. I just didnt know what to call it. I figured that since the main method is not capitalized, then it wouldnt confuse anyone.

    All java programs are in classes. To start the execution of a java program using the java command, you specify the name of a class containing the method: public static void main(String[] args).

    Do you call a class with the above main() method the Main class?
    It could be the only class, so calling it the Main one is redundant.
    Thats why I said:
    The other two are optional
    and:
    A program does not necessarily need an Object Class.

    All the classes in a program could have a main() method. There is no restriction there.
    That is something I didnt know. How does the compiler know what to execute when you test/debug? I know if you run the program from the command prompt or via a manifest in a jar you specify the main class, but you dont have to do that in IDEs, so that is where I have questions. Learning something new every day.

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Balloon.java, what in the world do i do?

    Quote Originally Posted by th3chef View Post
    Hey guys. I'm doing the same assignment and this is what I have so far with the errors I received:

    public class BalloonTester
    {

    private String Balloon1;
    private int hundred;

    public BalloonTester(String theName, int theAltitude); error: missing method body, or declare abstract
    {
    Balloon1 = theName; error: cannot find symbol, symbol: variable theName/Altitude,
    hundred = Math.max(theAltitude,0); location: class BalloonTester
    }


    Thanks for the help.
    Oh crap, I hate subtle mistakes. I make the same mistake sometimes on loops. To indicate an end of a line or piece of code, you use the ; symbol. You do not, however, include that symbol when you are including additional code, like declaring a method or running a loop.

    For example, these two loops are not the same:
    //Correct
    for(int i=0;i<10;i++)
    {
    ...
    }
     
    //Incorrect
    for(int i=0;i<10;i++);
    {
    ...
    }

    Why is the second incorrect? It has a ; after it. When you do this, you will receive no error, because the program just treats this as an empty loop. It will run it 10 times and quit.

    However, if you make the same mistake on a method or constructor, you will get an error because you have to provide a body for that method/constructor, and by having the ; you are saying it has no body.

    Look at these two method declarations:
    //Correct
    public void method()
    {
    ...
    }
     
    //Incorrect
    public void method();
    {
    ...
    }

    Once again, our pain-in-the-ass ; is ruining it for us. The good news is that when you do this with a method/constructor, you WILL get a compiler error.

    Look at the end of your constructor declaration and I'm sure you can find the issue.

  14. #14
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Balloon.java, what in the world do i do?

    Quote Originally Posted by aussiemcgr View Post
    Oh crap, I hate subtle mistakes. I make the same mistake sometimes on loops. To indicate an end of a line or piece of code, you use the ; symbol. You do not, however, include that symbol when you are including additional code, like declaring a method or running a loop.

    For example, these two loops are not the same:
    //Correct
    for(int i=0;i<10;i++)
    {
    ...
    }
     
    //Incorrect
    for(int i=0;i<10;i++);
    {
    ...
    }

    Why is the second incorrect? It has a ; after it. When you do this, you will receive no error, because the program just treats this as an empty loop. It will run it 10 times and quit.

    However, if you make the same mistake on a method or constructor, you will get an error because you have to provide a body for that method/constructor, and by having the ; you are saying it has no body.

    Look at these two method declarations:
    //Correct
    public void method()
    {
    ...
    }
     
    //Incorrect
    public void method();
    {
    ...
    }

    Once again, our pain-in-the-ass ; is ruining it for us. The good news is that when you do this with a method/constructor, you WILL get a compiler error.

    Look at the end of your constructor declaration and I'm sure you can find the issue.
    You're a lifesaver man, I knew it was something small like that, always is

    Thanks a lot I really appreciate it

Similar Threads

  1. hello world
    By WiFiLeech in forum Member Introductions
    Replies: 2
    Last Post: August 31st, 2010, 07:33 AM
  2. Hello World !
    By ArunUOM in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2010, 03:29 AM
  3. Hello World.
    By cselic in forum Member Introductions
    Replies: 0
    Last Post: April 21st, 2010, 03:41 AM
  4. [SOLVED] The infamous Hello World
    By ProfessorBellom in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 17th, 2010, 05:44 PM
  5. hello world
    By fiZ in forum Member Introductions
    Replies: 1
    Last Post: January 20th, 2010, 09:46 AM