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

Thread: Need help with creating a counter. conflicting constructors?...

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

    Default Need help with creating a counter. conflicting constructors?...

    So i posted this homework problem awhile back but my professor just decided to let us know that we must use a counter. and i am having trouble writing a constructor that doesnt conflict with another.

    the home work problem is as follows:

    -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 as follows:

    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());
     
       }
    }

    and my class is as follow:

    public class Car
    {
     
    private String brand;
    private int year;
    private int price;
    private static int number = 0;
     
     
    public Car()
    {
       number += 1;
    }
     
    public static int getNumber()
       {
          return number;
       }
     
    public int decrease()
       {
          number--;
          return number;
       }
     
     
    public Car(String carBrand, int carYear, int carPrice)
    {
    	brand = carBrand;
    	year = carYear;
    	price = carPrice;
    }
     
     
    public Car()
    {
       brand = "Chevy";
       year = 2005;
       price = 3000;
    }
     
     
    public void setBrand(String b)
    {
    	brand = b;
    }
     
    public void setYear(int y)
    {
    	year = y;
    }
     
    public void setPrice(int p)
    {
    	price = p;
    }
     
     
    public String getBrand()
    {
    	return brand;
    }
     
    public int getYear()
    {
    	return year;
    }
     
    public int getPrice()
    {
    	return price;
    }
     
     
    }

    the problem is where i tried creating a constructor for the count, but clearly it conflicts with the constructor for the chevy. not sure how to fix this.
    thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,190
    Thanks
    53
    Thanked 2,384 Times in 2,338 Posts

    Default Re: Need help with creating a counter. conflicting constructors?...

    If there are error messages, please copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with creating a counter. conflicting constructors?...

    Why not just increment the counter in the existing constructors?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    spangledhoagies (September 29th, 2014)

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

    Default Re: Need help with creating a counter. conflicting constructors?...

    Car.java:44: error: constructor Car() is already defined in class Car
    public Car()
    ^
    1 error

    ----jGRASP

    --- Update ---

    damn haha... was staring me right in the face.
    thanks.

Similar Threads

  1. constructors
    By sumitroy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 22nd, 2014, 08:44 AM
  2. Creating a Simple, Beginner-Level Vowel Counter
    By tryingtolearn in forum Collections and Generics
    Replies: 1
    Last Post: July 8th, 2012, 03:25 PM
  3. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM