Results 1 to 3 of 3
  1. #1
    heather.diggs is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    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 in
    Java Code:
    String thisMonth = "November, ";
    int year = 2008;
    thisMonth = thisMonth + 2008; // or thisMonth += 2008;
    System.out.println(thisMonth);
    luck, db
    Last edited by DarrylBurke; 11-12-2008 at 10:19 PM.

  3. #3
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    Might be valuable to note that all java objects inherit the toString() method from Object and that by writing your own in your classes you are over-riding that method.

Similar Threads

  1. toString() method of java.security.MessageDigest
    By Nicholas Jordan in forum Advanced Java
    Replies: 4
    Last Post: 09-18-2008, 02:32 PM
  2. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 02:39 AM
  3. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 04:08 AM
  4. Return question in a method.
    By MetalGear in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 04:45 AM
  5. Writing the filter method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 03:32 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
  •