Results 1 to 3 of 3
- 11-12-2008, 08:22 PM #1Member
- Join Date
- Nov 2008
- Rep Power
question about writing a toString method
I need to write the following program and It seems to be moving along nicely, although for some reason i dont understand what is needed from me on the line "Write a toString method in each class that prints out the name of the subclass using the getClass() method of class Object followed by the instance variables, each preceded by a description."
i have read about a toString method and understand more or less what it is, but i dont really get how i need to incorporate that into my program.
just in case attaching the FULL (its a bit of a read) assignment (NOTE: i dont need help with the entire assignment, i am doing it well on my own, but the toString method i do need help with, so if you have comments I would greatly appreciate them)
Write a toString method in each class that prints out the name of the subclass using the getClass() method of class Object followed by the instance variables, each preceded by a description.
Create a class named MetroCard that can keep track of its balance and expiration date. Each MetroCard has a unique serial number.
* There are different types of metro cards:
o Pay-per ride
o Unlimited Ride
o Reduced Fare (half fare) for senior citizens or disabled individuals, stores the owner's name
* All metro cards have common characteristics. They should inherit from a common base class. Should the base class be an interface or abstract since if only specialized versions exist?
Create a class that represents the card reader. It can deduct one fare from the MetroCard. Consider:
* What if there is an unexpired transfer that can be used? (Remember: transfers are only valid on a different route within a specified amount of time.)
* What if the balance is insufficient?
* Plan for rate changes. Should the fare be a literal or a variable?
Create another class that represents the metro card vending machine. It should have the capacity to create new MetroCards and refill existing cards.
* New cards can have a choice of dollar amounts ($4, $10, $20, etc.). The amount paid is not necessarily the same as the initial balance of the MetroCard (i.e., $10 might give you $12).
* When refilling an old card, the user can either enter an arbitrary amount OR choose from a list of dollar amounts. If the user enters a dollar amount not on the list, the card's balance is increased by the amount paid. If the user chooses an amount from the list, he might receive a bonus (as in a new card).
* Remember that the rates change periodically. Develop the class in a way that supports this feature.
- 11-12-2008, 10:16 PM #2
Invoking getClass() on a reference type variable returns a reference to a Class object. Read the API for Class and you'll find it has only 3 or 4 methods that return a String. Find the one that will get you the name of the Class.
Convert your class variables to Strings using API methods as necessary and concatenate them to the class name. Return the complete String from the method.
For brevity, you can make use of the fact that appending a numeric type to a String automatically concatenates the String representation of a number. As inJava Code:
String thisMonth = "November, "; int year = 2008; thisMonth = thisMonth + 2008; // or thisMonth += 2008; System.out.println(thisMonth);
Last edited by DarrylBurke; 11-12-2008 at 10:19 PM.
- 11-12-2008, 11:13 PM #3Member
- Join Date
- Jul 2008
- Rep Power
- By Nicholas Jordan in forum Advanced JavaReplies: 4Last Post: 09-18-2008, 02:32 PM
- By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 02:39 AM
- By apfroggy0408 in forum New To JavaReplies: 6Last Post: 01-31-2008, 04:08 AM
- By MetalGear in forum New To JavaReplies: 1Last Post: 01-13-2008, 04:45 AM
- By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-03-2007, 03:32 PM