Send Snippet To: Save this snippet to Code Collector Pro -- view all bassglider's snippets
language: Java
license: Other

Code for Snippet:

I can show you the ideas and problems more in detail after class or before, or later online.
// Your second constructor
public DerbyCar(String DriverName, double Value) {
	this (DriverName,  "Car 10", Value);  // Your idea is you want this.description, more on that below
Since there are overloaded constructors you need to have two constructors (which you do).  The idea
behind overloaded constructors is that the arguments of the new statement like:
DerbyCar car4 = new DerbyCar("Tester John", 0.50);  // Passing two arguments 
Must match the parameters of the constructor (below) when you want to create a new object:
public DerbyCar (String driver, double value) {   // To parameters in the constructor
So for the first few objects you create:
DerbyCar car1 = new DerbyCar("Batman", "Batmobile", 3000);
DerbyCar car2 = new DerbyCar("the Mystery Men", "Herkimer Battle Jitney", 2000);
DerbyCar car3 = new DerbyCar("Michael Knight", "KITT", 2500);
You are using this constuctor:
public DerbyCar (String driver, String desc, double value) {
because there are three parameters (in correct order) for driver, description, and value (with their types)
Ok so back to the second constructor.  We have to have (because we want the values that the first constructor for the objects has
public DerbyCar (String driver, double value) {
	this.dollarValue = value;
    this.driverName = driver;
	this.idNumber = id;
	this.description = "Car "   getIDNumber();    
Because the last derby car we create only has two parameters.  Which parameter/argument is it missing?  
It's missing the description.  So if you look at the output on the assignment it says it must be:
Car 13 ["Car 13", driven by Tester John. Value: $0.5]
So we say that the description for this instance of the new car is:
"Car"   getIDNumber(); so that means "Car" 13
For the toString() method:
public String toString() {   //method you can concatenate Strings here with methods
return "Car "   this.getIDNumber()   " [\""   this.getDescription()   "\", driven by "     etc
To get the output you want:
Created 3 cars: 
Car 10 ["Batmobile", driven by Batman. Value: $3000.0]
Car 11 ["Herkimer Battle Jitney", driven by the Mystery Men. Value: $2000.0]
Car 12 ["KITT", driven by Michael Knight. Value: $2500.0]
See if that helps :)
comments powered by Disqus


Link to this snippet:

Download to Code Collector

To use the direct link to your snippet on either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 772
Date Added: 2008-03-19 19:00:45
Last Modified: 0000-00-00 00:00:00

Web Analytics