Not sure what is wrong with this
this is the prompt for the question:
The Car class contains four class attributes, make, year, serialNumber, and color. We sometimes want to create a new Car object with the same make, year and color but with a different serial number, as another car.
For example, if myCar is a red, 1999 VW, with serial number 4444, then this statement:
myCar.create(2345);
will create a new red 1999 VW with serial number 2345.
Write the create method, which takes an int value -- the new serial number -- as an input parameter, and then creates a new Car object with the same make, year, and color values as the Car object being called, but with the new serial number. The method should return the newly created Car object.
then the code given
Code Java:
public class Car {
public static final String FORD = "Ford";
public static final String GM = "GM";
public static final String SAAB = "Saab";
public static final String TOYOTA = "Toyota";
public static final String VW = "Volkswagen";
public static final String RED = "red";
public static final String BLUE = "blue";
public static final String WHITE = "white";
private String make;
private int year;
private int serialNumber;
private String color;
public Car(String mk, int yr, int serial, String cl) {
make = mk;
year = yr;
serialNumber = serial;
color = cl;
}
public Car create(int serial) {
MY CODE GOES HERE
} // end method
} // end class
I inputed:
Code Java:
return new Car(make, year, serialNumber, color);
where MY CODE GOES HERE
help? haha
Re: Not sure what is wrong with this
You dont want to return a new object, you want to create one. So basically youll have a method called create which sets make, year and color to this.make, this.year, this.color and then youll set serial to the input-parameter. The name of the new object isnt specified in the assignment, but it doesnt look like it wants you to overwrite either. Hummz. Just give it a generic name then, myCreatedCar or whatevers.
Right?
Re: Not sure what is wrong with this
OK so what exactly is the problem you're having? Is it compiling with or without errors? If without what is happening that you don't expect? And what exactly is this code for?
Code :
public static final String FORD = "Ford";
public static final String GM = "GM";
public static final String SAAB = "Saab";
public static final String TOYOTA = "Toyota";
public static final String VW = "Volkswagen";
public static final String RED = "red";
public static final String BLUE = "blue";
public static final String WHITE = "white";
Re: Not sure what is wrong with this
well the code is already done it is just missing a line, it's a homework problem. I have to fill in where I wrote CODE GOES HERE with something to make the whole code work. and that is where im running in to an issue, i just dont know what to put in
Re: Not sure what is wrong with this
Quote:
just dont know what to put in
What does the assignment say should be there?
Quote:
public Car create(int serial) {
How do you create a Car object?
Create one with the required specs and return the reference to it.
Re: Not sure what is wrong with this
This should do fine, you just needed to change serialNumber to serial.
Code Java:
public Car create(int serial) {
return new Car(make, year, serial, color);
}
Re: Not sure what is wrong with this
Quote:
Originally Posted by
Davidovic
This should do fine, you just needed to change serialNumber to serial.
Code Java:
public Car create(int serial) {
return new Car(make, year, serial, color);
}
Yes that should do it but I was rather hoping he'd figure it out himself so that he understands why that is.
Re: Not sure what is wrong with this
Yeah thanks, I ended up figuring it out before coming back to the site anyways but thanks all!