Results 1 to 2 of 2
Thread: How do i do this method(objects)
- 04-19-2010, 10:16 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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
-
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-19-2010 at 11:04 PM.
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
referencing objects from static method
By talktofrank in forum New To JavaReplies: 4Last Post: 10-26-2009, 06:09 PM -
Static method cannot make new objects?
By zerkz in forum New To JavaReplies: 2Last Post: 10-15-2009, 03:17 AM -
Need help with drawing multiple objects with mouseClicked method.
By busdude in forum New To JavaReplies: 6Last Post: 04-05-2009, 11:28 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks