Created
August 12, 2013 14:35
-
-
Save Kurt-P/6211340 to your computer and use it in GitHub Desktop.
Just some practice with Java Strings and toString() method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public final class Truck { | |
private String fName = "Dodge"; | |
private int fNumDoors = 2; | |
private Date fYearManufactured = new Date(); | |
private String fColor = "Fuchsia"; | |
private List<String> fOptions = Arrays.asList("Air Conditioning"); | |
/** Simple test harness. */ | |
public static void main(String... aArgs){ | |
Truck planetKiller = new Truck(); | |
System.out.println(planetKiller); | |
} | |
/** | |
* Intended only for debugging. | |
* | |
* <P>Here, the contents of every field are placed into the result, with | |
* one field per line. | |
*/ | |
@Override public String toString() { | |
StringBuilder result = new StringBuilder(); | |
String NEW_LINE = System.getProperty("line.separator"); | |
result.append(this.getClass().getName() + " Object {" + NEW_LINE); | |
result.append(" Name: " + fName + NEW_LINE); | |
result.append(" Number of doors: " + fNumDoors + NEW_LINE); | |
result.append(" Year manufactured: " + fYearManufactured + NEW_LINE ); | |
result.append(" Color: " + fColor + NEW_LINE); | |
//Note that Collections and Maps also override toString | |
result.append(" Options: " + fOptions + NEW_LINE); | |
result.append("}"); | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment