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

Thread: Create a Balloon object in java

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Create a Balloon object in java

    Programming Assignment #2

    (Using an Existing Class: Creating Objects
    and Calling Accessor and Mutator Methods)


    I. The Assignment

    This assignment is to write a “test” class (aka: a “driver” class or “client code”) that uses the class Balloon.java, available on the class web page.

    To use the Balloon class, download it and store it in the src folder of your NetBeans project. Make sure you save it as Balloon.java.

    The best way to learn how to use the Balloon class – or any other Java class, for that matter - is to consult the documentation, Balloon.html (online). You can also read the javadoc comments that appear just above the class declaration and above each method declaration, which explain what each method does, what the method’s parameters are, and what value – if any - is returned by the method. The html “help” pages are generated from these comments.

    Don’t worry if you don’t understand the code. It will all be covered later. It is not necessary to know how a method works as long as you know what it does and how to call it.

     Review declaring variables, creating objects, calling methods that return a value vs. “void” methods, and accessor and mutator methods before beginning.

    To receive credit for this assignment, you must not modify the Balloon class in any way!


    II. Your BalloonTester Class

    Your BalloonTester class will have only a single method – main – and will perform each of the following operations, in the exact order listed below. Each operation may be done in one or two statements. Make sure you follow directions faithfully, and note that once you have done step 3, you can copy and paste it to do steps 6, 9, and 12.

    1. Create a Balloon object with a name of your own choosing and an altitude of 100 meters.

    2. Create a second Balloon object with a name of your own choosing, and specify an initial altitude of -100 meters.

    3. Call the accessor methods of the Balloon class to get the name and altitude of each Balloon object. Print the data, one object per line.

    4. Make the object you created in step 1 ascend to an altitude of 250 meters.

    5. Call the adjustAltitude method to increase the altitude of the object you created in step 2 by 150 meters.

    6. Call the accessor methods of the Balloon class to get the name and altitude of each object. Print the data, one object per line.

    7. Call the adjustAltitude method to decrease the altitude of the object you created in step 1 by 150 meters.

    8. Make the object you created in step 2 descend to the same altitude as the other object. You may assume that the other object is at a lower altitude.

    To get credit for step 8., the statement(s) you write must always work, regardless of the actual altitude of the second object. It cannot depend on you knowing the altitude of the second object, but must utilize the fact that the object knows its own altitude. In other words, if you use a literal in any way to set the altitude, it is not correct.

    9. Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.

    10. Move the object you created in step 1 to an altitude that is four times its current altitude. As in step 8, the statement(s) you write must work for any altitude and may not depend on you “figuring out” the new altitude beforehand.

    11. Attempt to move the object you created in step 2 to an altitude that is 150 meters below its current altitude.

    12. Call the accessor methods to get the name and altitude of each object. Print the data, one object per line.

    --- Update ---

    and this is the Balloon.java given:
     
    // File: Balloon2.java
     
    // Modified Balloon class has overloaded constructors
     
    /**
     * A class to represent a hot-air balloon.  Balloon objects have a name and an
     * altitude.
     */
    public class Balloon2
    {
    	// instance variables
      	private String name ; 			// name of the balloon
      	private int altitude;     		// altitude (height) of balloon in meters
     
      	// "class" variable
      	private static int count = 1 ;	// to create default name
     
    	/**
       	 * Create a ballon object with a given name at a given altitude.
         * @param theName the name of the ballon object
         * @param theAltitude the altitude
         */ 
      	public Balloon2(String theName, int theAltitude)
      	{
      		name = theName ;
      		// make sure altitude is not negative!
      		altitude = Math.max(theAltitude,0) ;
      	}
     
    	/**
       	 * Create a ballon object with a given name at defaule altitude of 0.
         * @param theName the name of the ballon object
         */ 
      	public Balloon2(String theName)
      	{
      		name = theName ;
      		altitude = 0 ;
      	}
     
    	/**
       	 * Create a ballon object with a default name at a given altitude.
         * @param theAltitude the altitude
         */ 
      	public Balloon2(int theAltitude)
      	{
      		name = "Balloon" + count ;
      		count = count + 1 ;
      		// make sure altitude is not negative!
      		altitude = Math.max(theAltitude,0) ;
      	}
     
    	/**
       	 * Create a ballon object with a default name and default altitude of 0.
         */ 
      	public Balloon2()
      	{
      		name = "Balloon" + count ;
      		count = count + 1 ;
      		altitude = 0 ;
      	}
     
    	/**
    	 * Ascend to a particular altitude.
    	 * @param newAlt the altitude to which to ascend, in meters.
    	 */
    	public void ascendTo(int newAlt)
    	{
      		// ascend to new altitude only if it is greater than current altitude, 
     
        	if (newAlt > altitude)
        	{
            	altitude = newAlt ; 
        	}
    	}
     
    	/**
    	 * Descend to a particular altitude.
    	 * @param newAlt the altitude to which to descend, in meters.
    	 */
    	public void descendTo(int newAlt)
    	{
        	// prevent possible crash into ground
        	if (newAlt < 0)			// if desired altitude is below ground level!
        	{
        		altitude = 0 ;		// ...descend only to ground level
        	}
        	// otherwise, descend only if new altitude is less than current altitude
        	else if (newAlt < altitude)
        	{
            	altitude = newAlt ; 
        	}
    	}
     
      	/**
      	 * Modify altitude by a given number of meters, up or down.
      	 * @param change number of meters to add to current altitude
      	 */	
    	public void adjustAltitude(int change)
    	{
        	// if change is negative (i.e. descending), can't go below 0 altitude
        	if (change + altitude < 0)  // change < 0 && abs(change) > altitude
        	{
        		altitude = 0 ;	// ...descend only to ground level
        	}
        	else				// safe to modify current alt by "change" meters
        	{
            	altitude = altitude + change ; 
        	}
    	}
     
    	/**
    	 * Get ballon name.
    	 * @return name the name of the balloon
    	 */
    	public String getName()
    	{
    		return name ;
    	}
     
    	/**
    	 * Get current altitude.
    	 * @return altitude the altitude of the balloon
    	 */
    	public int getAltitude()
    	{
    		return altitude ;
    	}
    }
    // end of Balloon class definition


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Create a Balloon object in java

    What's your question?

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    ok this is what i wrote for my tester class
     
    public class BalloonTester
    {
        private static int altitude;
        private static String name;
     
        /**
         * @param args the command line arguments
         * @param h
         */
     
        public static void main(String[] args) 
     
        {
            int h100 = 100;
            BalloonTester myBalloon = new BalloonTester ();
     
     
     
     
             //create a second object
     
            BalloonTester secondBalloon = new BalloonTester(); //creates the object
     
     
             //call accesor methods
     
            name = myBalloon.getname();
     
            altitude = myBalloon.getaltitude();
     
            altitude = secondBalloon.getaltitude();
     
             //print the info obtained
     
            System.out.println( " MyBalloon has an altitude of " + myBalloon.getaltitude()
                    + "," + " and secondBalloon has an altitude of " + secondBalloon.getaltitude());
     
     
            //Ascend myBalloon to an altitude of 250 meters
     
            myBalloon.Ascend(250);
     
            //Adjust the altitude of secondBalloon to 250 meters
     
            secondBalloon.adjustAltitude();  
     
            System.out.println( " MyBalloon has an altitude of " + myBalloon.getaltitude());
            System.out.println( " SecondBalloon has an altitude of " + secondBalloon.getaltitude());
     
            //Adjust the altitude of MyBalloon to 400 meters
     
            myBalloon.adjustAltitude(0);
     
     
     
        }		
     
        private BalloonTester() {
     
        }
     
        private BalloonTester(String myBalloon) {
     
        }
     
        private int getaltitude() {
            return altitude;
        }
     
        private void Ascend(int i) {
     
        }
     
        private void adjustAltitude(int i) {
     
        }
     
        private String getname() {
            return name;
     
     
     
     
    }
     
        private void adjustAltitude() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }


    --- Update ---

    honestly i have changed my code and try other things for the last 4 days, i don t know much java, but i want to understand.....

  4. #4
    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: Create a Balloon object in java

    Is it working now?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    nope, im doing something wrong, since i m new to java and programming in general, it might be conceptual, but i truly need help.

    --- Update ---

    this is what tells me.....
    run:
    MyBalloon has an altitude of 0, and secondBalloon has an altitude of 0

    Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at BalloonTester.adjustAltitude(BalloonTester.java:87 )
    at BalloonTester.main(BalloonTester.java:45)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  6. #6
    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: Create a Balloon object in java

    Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at BalloonTester.adjustAltitude(BalloonTester.java:87 )
    Did you look at line 87 in the BallonTester program? It has the following statement which caused the above error.
     throw new UnsupportedOperationException("Not supported yet.");
    If you don't want the error, remove the throw statement. You might think about why that statement is there.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Lasoquet (September 19th, 2014)

  8. #7
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Create a Balloon object in java

    Adjust altitude expects an integer to be passed, but you don't give it anything.

    secondBalloon.adjustAltitude();

  9. The Following User Says Thank You to camel-man For This Useful Post:

    Lasoquet (September 19th, 2014)

  10. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    yes, that is there because somewhere along the way it suggested a fix, let me delete it and re check.
    OMG, no errors in code, but i have logic errors, because of course everything is 0, how can i add the values?


    run:
    MyBalloon has an altitude of 0, and secondBalloon has an altitude of 0
    MyBalloon has an altitude of 0
    SecondBalloon has an altitude of 0
    BUILD SUCCESSFUL (total time: 0 seconds)

  11. #9
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Create a Balloon object in java

    That's what your constructors are for. Anytime you create an object you should pass the values you want. This will initialize the values (if your constructors are set up that way.)

  12. The Following User Says Thank You to camel-man For This Useful Post:

    Lasoquet (September 19th, 2014)

  13. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    so, can you please teach me how do you pass the values. my first balloon needs 100, the second one -100, but I m not exactly sure where and how to do that. the other day i created pyramids just by myself but this one is giving me trouble, i ended up confused on what ive learned.

  14. #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
    If you don't understand my answer, don't ignore it, ask a question.

  15. #12
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    like this,

    private BalloonTester(String myBalloon, int h100) {

  16. #13
    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: Create a Balloon object in java

    What is that code for? It looks like a constructor for the BallonTester class. Is that right?
    Where is it supposed to be used? What other class will use that constructor to create an instance of the BallonTester class?
    Why private instead of public?

    Look at the Ballon2 class. It has a constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #14
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    let me ask you something first, the Balloon2 class was given, and they say to not modify, so, i basically have to create another testerclass? main class? to create the object, but then when i name my objects on my code, and i write myballoon and sencondBalloon, i notice that the code given has balloon1 and balloon2 as the names, am i suppose to name my objects that or modify the one given.

  18. #15
    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: Create a Balloon object in java

    It should be up to you How you name the Ballon2 objects in the tester code unless the instructor has told you what names to use.

    I don't know what the use of a BallonTester object is. If the code is to test the Ballon2 class, it should create objects of that class.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #16
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    i still don t get it. it says not to modify the one he gave, so i base mine on his. but still dont get about the names.

  20. #17
    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: Create a Balloon object in java

    Your class is supposed to create instances of the other class and call its methods.
    Your testing class shouldn't be based on the other class.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #18
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create a Balloon object in java

    Quote Originally Posted by Norm View Post
    Your class is supposed to create instances of the other class and call its methods.
    Your testing class shouldn't be based on the other class.
    Just trying not to be a pain Norm, some things I don't get but some I try to make sense of before I ask again, lol. You have been so good to me today though.

Similar Threads

  1. [SOLVED] Can't create Balloon Objects.
    By Lifeleaf in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 26th, 2014, 03:40 PM
  2. Can't create Balloon Objects.
    By Lifeleaf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2014, 02:20 PM
  3. java balloon project
    By srozo22 in forum Object Oriented Programming
    Replies: 12
    Last Post: September 19th, 2013, 03:02 PM
  4. Create Object
    By TitanVex in forum Object Oriented Programming
    Replies: 8
    Last Post: December 29th, 2011, 11:24 PM
  5. Balloon.java, what in the world do i do?
    By toterzuko in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 13th, 2010, 05:19 PM