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 7 of 7

Thread: Will someone help me with code? Creating classes, getters & setters. Beginner :(

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Will someone help me with code? Creating classes, getters & setters. Beginner :(

    package CarLot;

    Main Method:


    //HW06.java CS 140-01
    //10/23/12
    //Dominic Torro
    //This program builds several car objects


    public class CarLot
    {

    public static void main (String[] args)

    {

    //Builds 3 car objects.

    Car car1 = new Car (1996, "Dodge", "Neon", "4dr", "rust" );
    Car car2 = new Car (1978, "Ford", "Mustang", "2dr", "black");
    Car car3 = new Car (1967, "Chevrolet", "Corvette", "2dr", "silver");

    //Header to explain what program does

    System.out.println("This program builds several car objects");

    //Print original information for each car using a toString()

    System.out.println ("Car 1: " +car1);
    System.out.println ("Car 2: " +car2);
    System.out.println ("Car 3: " +car3);

    //Set color of car3 to red
    car3.setColor("red");

    //Set year of car1 to 2001
    car1.setYear(2001);

    //Set model of car 1 to Challenger
    car1.setModel("Challenger");

    //Set model of car2 to Taurus
    car2.setModel("Taurus");

    //Print new info for car1 using getter methods
    System.out.println("Car 1 is now a: " +car1.getYear() +car1.getModel());

    System.out.println ("Car 2 is now a: " +car2.getModel());

    System.out.println ("Car 3 is now a: " +car3.getColor());
    }
    }

    This is my worker class:

    package CarLot;

    import java.text.DecimalFormat;

    public class CarWorker
    {
    //Instance Data
    int year;
    String make;
    String model;
    String bodytype;
    String color;

    //Constructor method

    public Car (int yearIn, String makeIn, String modelIn, String bodytypeIn, String colorIn)


    {
    year = yearIn;
    make = makeIn;
    model = modelIn;
    bodytype = bodytypeIn;
    color = colorIn;
    }

    //Getters and setters

    public int getYear()

    {
    return year;
    }

    public void setyear(yearIn)

    {
    year = yearIn;
    }

    public String getmake()

    {
    return make;
    }

    public void setmake(makeIn)

    {
    make = makeIn;
    }

    public String getmodel()

    {
    return model;
    }

    public void setmodel(modelIn)

    {
    model = modelIn;
    }

    public String bodytype()

    {
    return bodytype;
    }

    public void setbodytype(bodytypeIn)

    {
    bodytype = bodytypeIn;

    }

    public String getcolor()

    {
    return color;
    }

    public void setcolor(colorIn)

    {
    color = colorIn;
    }

    public String toString()
    {

    String result = "";
    return result = ""
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Will someone help me with code? Creating classes, getters & setters. Beginner :(

    what is your question sir?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Will someone help me with code? Creating classes, getters & setters. Beginner :(

    my code has all sorts of errors and I need help figuring them out

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Will someone help me with code? Creating classes, getters & setters. Beginner :(

    firstly, the carWorker class, the constructor MUST be the same name as the class, you wrote public car(int....)
    it needs to be carWorker(int....)
    secondly, the toString method in the same class should return a string,
    you wrote return result = "";
    which is actually a boolean query that returns true or false, not a string.
    what you can do is build the result variable as following:
    String result = "";
    result = result + getColor() + " , " + getModel() + .....
    return result;
    hope it helps,
    btw if you are using ecliple or netbeans or something like that you can read exactly which errors you get and understand from
    that where the problem is.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Will someone help me with code? Creating classes, getters & setters. Beginner :(

    secondly, the toString method in the same class should return a string,
    There is nothing wrong with:
    return result = "";
    ...assuming you want to return the empty string.

    Please see the announcements page for the use of code tags.

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Location
    Germany
    Posts
    19
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Will someone help me with code? Creating classes, getters & setters. Beginner :(

    Quote Originally Posted by skitheeast8 View Post
    my code has all sorts of errors and I need help figuring them out
    Your code? Are you sure that it's not your task to find and correct the errors in the code? :-)

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Will someone help me with code? Creating classes, getters & setters. Beginner :(

    yes i see now i was mistaken about the string = "" , since it is an assignment and not comparison.. oopsi

Similar Threads

  1. Question about creating and using classes.
    By iDizzle in forum Object Oriented Programming
    Replies: 20
    Last Post: April 1st, 2012, 03:14 PM
  2. Problems with input/output and getters/setters
    By robbiep551 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: January 5th, 2012, 11:44 PM
  3. [SOLVED] Help with classes & instance methods
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 16
    Last Post: July 30th, 2010, 01:20 PM
  4. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM
  5. A Question Regarding Abstract Classes & Packages
    By Kerubu in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 01:27 PM