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

Thread: Desperate help needed with class and methods

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Desperate help needed with class and methods

    ok so i am looking at my homework for class and i am horribly confused lol. i would greatly appreciate it if someone could help me out with this with a good explanation.

    so the instructions are

    In this homework, you will write a Car.java file in order to make HW1tester.java (see the attachment) file work. And the output is required to be exactly

    This car is Chevy, year 2005, price 3000
    This car is Ford, year 2011, price 22000
    This car is Audi, year 2012, price 25000
    This car is Cadillac, year 2005, price 3000
    The total car number is: 4
    The total car number is: 4
    The total car number is: 4
    The total car number is: 4

    HW1tester is right here
    hw1test.txt


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Desperate help needed with class and methods

    Start writing a Car class. Compile hw1test and have a look at the comliper errors. They tell you what's missing.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Desperate help needed with class and methods

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.
    i am horribly confused
    We can't help you if we don't know what you're confused about. Describe your confusion or show what code you've managed to write and then explain why you can go no further.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Desperate help needed with class and methods

    Sorry about that, and thank you for helping me to clean up my post. Basically, my confusion is where to start. I created the Car class, and gave price, brand, and year their separate private classes, but i got stuck when it came to creating the constructor and the methods. Here is the instance code we were given. Note: this IS a homework problem.


    public class HW1tester
    {
       public static void main(String[] args)
       {
          Car car1 = new Car();
          Car car2 = new Car("Ford", 2013, 20000);
          Car car3 = new Car("Audi", 2012, 25000);
          Car car4 = new Car();
     
          car2.setPrice(22000);
          car2.setYear(2011);
     
          car4.setBrand("Cadillac");
     
          System.out.println("This car is " + car1.getBrand() + ", year " + car1.getYear() + ", price " + car1.getPrice());
          System.out.println("This car is " + car2.getBrand() + ", year " + car2.getYear() + ", price " + car2.getPrice());
          System.out.println("This car is " + car3.getBrand() + ", year " + car3.getYear() + ", price " + car3.getPrice());
          System.out.println("This car is " + car4.getBrand() + ", year " + car4.getYear() + ", price " + car4.getPrice());
     
          System.out.println("The total car number is: " + car1.getNumber());
          System.out.println("The total car number is: " + car2.getNumber());
          System.out.println("The total car number is: " + car3.getNumber());
          System.out.println("The total car number is: " + car4.getNumber());
     
       }
    }

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Desperate help needed with class and methods

    You should show us your Car class.
    If you look at the main method that you have posted you will see all the methods / constructors that your Car class will need.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Desperate help needed with class and methods

    public class Car
    {
    private String brand;
    private int year;
    private double price;
     
     
     
    }

    that's about as far as i got and then my brain started jumbling everything up.

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Desperate help needed with class and methods

    Do you know what Getters / Setters are? Because that is what you have to write now.

    Perhaps you should Google "Getters / Setter java", I bet you will find good tutorials on this topic, Getters and Setters are quite common.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Desperate help needed with class and methods

    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Desperate help needed with class and methods

    yes i know these things. i guess what is confusing me is the fact that car1 and car4 have no field initialization. i will begin to try to write a full code once i am on my own laptop and post it here. thanks

    --- Update ---

    heres what i got:

    public class Car
    {
    private String brand;
    private int year;
    private double price;
     
     
    public Car(String carBrand, int carYear, double carPrice)
    {
    	brand = carBrand;
    	year = carYear;
    	price = carPrice;
    }
     
    public void setBrand(String b)
    {
    	brand = b;
    }
     
    public void setYear(int y)
    {
    	year = y;
    }
     
    public void setPrice(double p)
    {
    	price = p;
    }
     
     
    public String getBrand()
    {
    	return brand;
    }
     
    public int getYear()
    {
    	return year;
    }
     
    public double getPrice()
    {
    	return price;
    }

    the error says:

    HW1tester.java:5: error: constructor Car in class Car cannot be applied to given types;
    Car car1 = new Car();
    ^
    required: String,int,double
    found: no arguments
    reason: actual and formal argument lists differ in length


    it says the same for car4 as well. not sure what is going on lol.

  10. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Desperate help needed with class and methods

    Your class is almost finished.
    The error you get is because you only defined one constructor, your homework requires you to define 2 different constructors.

    The one constructor you got is this:
    public Car(String carBrand, int carYear, double carPrice)
    {
    	brand = carBrand;
    	year = carYear;
    	price = carPrice;
    }
    it is used in your homework at this point:
          Car car2 = new Car("Ford", 2013, 20000);
          Car car3 = new Car("Audi", 2012, 25000);

    But your homework also uses a different constructor, here:
          Car car1 = new Car();
          ...
          ...
          Car car4 = new Car();
    You need to define both of them.


    There is yet another method that is missing:
          System.out.println("The total car number is: " + car1.getNumber());
          System.out.println("The total car number is: " + car2.getNumber());
          System.out.println("The total car number is: " + car3.getNumber());
          System.out.println("The total car number is: " + car4.getNumber());
    As you can see, your car must also provide a method called "getNumber()".

  11. The Following User Says Thank You to Cornix For This Useful Post:

    spangledhoagies (September 24th, 2014)

  12. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Desperate help needed with class and methods

    ok i added the constructors now i am stuck at the getNumber. this is what i have now:

    public class Car
    {
    private String brand;
    private int year;
    private double price;
    private int Number = 4;
     
    public Car(String carBrand, int carYear, double carPrice)
    {
    	brand = carBrand;
    	year = carYear;
    	price = carPrice;
    }
     
    public Car(int y, double p )
    {
       y = 2005;
       p = 3000;
    }
     
    public Car()
    {
     
    }
    public void setBrand(String b)
    {
    	brand = b;
    }
     
    public void setYear(int y)
    {
    	year = y;
    }
     
    public void setPrice(double p)
    {
    	price = p;
    }
     
     
    public String getBrand()
    {
    	return brand;
    }
     
    public int getYear()
    {
    	return year;
    }
     
    public double getPrice()
    {
    	return price;
    }
     
    public int getNumber()
    {
     
       return Number;
    }
    }

    do i need a constructor for the getNumber?

    update- just looked over this and also forgot to mention that i had a second constructor with no arguments. confused on what to put there. very lost.
    Last edited by spangledhoagies; September 24th, 2014 at 12:14 PM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Desperate help needed with class and methods

    constructor with no arguments
    Use that to assign default values to the class's fields instead of their default values of null and 0.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    spangledhoagies (September 24th, 2014)

  15. #13
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Desperate help needed with class and methods

    got it. thanks guys for helping. really appreciate it.

Similar Threads

  1. [SOLVED] Write a class named Calculator add four methods to that class
    By zahid32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 30th, 2014, 07:06 AM
  2. Can't create inner class object outside the outer class help needed?
    By Dark knight in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2012, 02:34 AM
  3. Stuck with KMeans Clustering Algorithm DESPERATE HELP NEEDED
    By Suzanne42 in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 23rd, 2011, 08:29 PM
  4. [SOLVED] Least Coins Needed For Amount class
    By mwardjava92 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2011, 04:36 PM
  5. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM