Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Not sure what is wrong with this

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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:
    return new Car(make, year, serialNumber, color);
    where MY CODE GOES HERE

    help? haha
    Last edited by jwb4291; June 28th, 2010 at 02:30 AM.


  2. #2
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

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

  3. #3
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

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

       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";

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Not sure what is wrong with this

    just dont know what to put in
    What does the assignment say should be there?
    public Car create(int serial) {
    How do you create a Car object?
    Create one with the required specs and return the reference to it.

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Not sure what is wrong with this

    This should do fine, you just needed to change serialNumber to serial.
     
     public Car create(int serial) {
     
     
    		return new Car(make, year, serial, color);
     
       }

  7. #7
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Not sure what is wrong with this

    Quote Originally Posted by Davidovic View Post
    This should do fine, you just needed to change serialNumber to serial.
     
     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.

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

Similar Threads

  1. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  2. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 06:11 PM
  3. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 06:37 PM
  4. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM