Results 1 to 2 of 2
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default How do i do this method(objects)

    i have a program here with a bunch of constructors and methods. the last method i dont understand. theres comments about what it should do. also the method before the last, did i do that one right?

    Java Code:
    public class Airport
    {
    
      // instance variables
      private String airportCode;
      private int gates;
    
      // 1. ***** Add a static class variable *****
      //  countAirports is an int
      //  assign an initial value of 0
    	static int countAirports = 0;
    
      // 2. ***** Modify this method *****
      // Default constructor:
      // method name: Airport
      // return value:  none
      // parameters: none
      // function: sets the airportCode to an empty String
      //    ***** add 1 to countAirports class variable
      public Airport( )
      {
         airportCode = "";
    
      }
    
      // 3. ***** Modify this method *****
      // Overloaded constructor:
      // method name: Airport
      // return value: none
      // parameters:  a String airport code and an int startGates
      // function: assigns airportCode the value of the
      //      startAirportCode parameter;
      //      calls the setGates method,
      //      passing the startGates parameter
      //    ***** add 1 to countAirports class variable
      public Airport( String startAirportCode, int startGates )
      {
         airportCode = startAirportCode;
         setGates( startGates );
         countAirports += 1;
    	
      }
    
      // Accessor method for the airportCode instance variable
      // method name: getAirportCode
      // return value: String
      // parameters: none
      // function: returns airportCode
      public String getAirportCode( )
      {
         return airportCode;
      }
    
      // Accessor method for the gates instance variable
      // method name: getGates
      // return value: int
      // parameters: none
      // function: returns gates
      public int getGates( )
      {
         return gates;
      }
    
      // 4. ***** Write this method *****
      // Accessor method for the countAirports class variable
      // method name: getCountAirports
      // return value: int
      // parameters: none
      // function: returns countAirports
    
    	public int getCountAirports()
    	{
    		return countAirports;
    	}
    
    
      // Mutator method for the airportCode instance variable
      // method name: setAirportCode
      // return value: void
      // parameters: String newAirportCode
      // function: assigns airportCode the value of the
      //                    newAirportCode parameter
      public void setAirportCode( String newAirportCode )
      {
         airportCode = newAirportCode;
      }
    
      // Mutator method for the gates instance variable
      // method name: setGates
      // return value:  void
      // parameters: int newGates
      // function: validates the newGates parameter.
      //   if newGates is greater than 0, sets gates to newGates;
      //   otherwise, prints an error message to System.err
      //   and does not change value of gates
      public void setGates( int newGates )
      {
         if ( newGates  >=  0 )
           gates = newGates;
         else
         {
           System.err.println( "Gates must be at least 0" );
           System.err.println( "Value of gates unchanged." );
         }
      }
    
      // 5. ***** Write this method *****
      // method name:  toString
      // return value: String
      // parameters: none
      // function:  returns a String that contains the airportCode
      //    and gates
    
    	public String toString()
    	{
    		String codeAndGate = "airportCode" + gates;
    		return codeAndGate;
    	}
    
    
    
    
      // 6. ***** Write this method *****
      // method name: equals
      // return value: boolean
      // parameter:  Airport object
      // function:  returns true if airportCode
      //     and gates in this object
      //    are equal to those in the parameter object;
      //    returns false otherwise
    
    	public boolean equals(Airport Object)
    	{
    		
    	}
    
    
    
    
    
       }  // end of Airport class definition

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please see my comments:

    Java Code:
    public class Airport
    {
      // code deleted...
    
      // 2. ***** Modify this method *****
      //    ***** add 1 to countAirports class variable  ---- !! Have you done this yet?
      public Airport( )
      {
         airportCode = "";
    
      }
    
    
      // 4. ***** Write this method *****   ---- !! I'd make this method static.
    	public int getCountAirports()
    	{
    		return countAirports;
    	}
    
    
      // 5. ***** Write this method *****
      // method name:  toString
      // return value: String
      // parameters: none
      // function:  returns a String that contains the airportCode
      //    and gates
    
    	public String toString()
    	{
    		// ---- !! Incorrect: you're returning a String that states "airportCode" 
    		// ---- !! when you should be returning the contents of the airportCode variable instead.
    		String codeAndGate = "airportCode" + gates;  // !! incorrect
    		return codeAndGate;
    	}
    
      // 6. ***** Write this method *****
      // method name: equals
      // return value: boolean
      // parameter:  Airport object
      // function:  returns true if airportCode
      //     and gates in this object
      //    are equal to those in the parameter object;
      //    returns false otherwise
    
    //	public boolean equals(Airport Object)
    //	{
    //		// ---- !! This method signature is wrong.  It should be:
    //	}
    
    
    	// ---- !! better:
    	public boolean equals(Object otherObj)
    	{
    		// ---- !! You will need to first cast otherObj into an Airport variable:
    		Airport otherAirport = (Airport) otherObj;
    
    		// ---- !! and then try to write if statements to see if the gates variables and 
    		// ---- !! the airportCode variables for the current object and otherAirport are the same
    	}
    
       }
    Last edited by Fubarable; 04-20-2010 at 12:04 AM.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  2. referencing objects from static method
    By talktofrank in forum New To Java
    Replies: 4
    Last Post: 10-26-2009, 07:09 PM
  3. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 04:17 AM
  4. Replies: 6
    Last Post: 04-06-2009, 12:28 AM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •