I have got this far, pls help

code an application that creates an array of five car objects from a car class

1)This application should use an instantiable class

2)The car class has already been created and you will need to code an additional method in the car class that will keep track of the amount of times a particular model has been purchased.

3) Create an array of objects using the car class. The five car
objects should contain the following information.
 
Make              Model         Year          Price
 
Dodge           Viper          2003           $72,471



public class Car
{
      private String make, model, year;
      private double price;
      private int count;

      public Car(String tempMake, String tempModel, String tempYear, double tempPrice)
      {
            make = tempMake;
            model = tempModel;
            year = tempYear;
            price = tempPrice;
            count++;
      }

      public String getMake()
      {
            return make;
      }

      public String getModel()
      {
            return model;
      }

      public String getYear()
      {
            return year;
      }

      public double getPrice()
      {
            return price;
      }
      public void setMake(String m)
       {
               make = m;
       }
       public void setModel(String mo)
      {
            model  = mo;
      }
      public void setYear(String y)
      {
            year  = y;
      }
     public void setPrice(double p)
      {
           price = p;
     }
     //method to return number of instantiated class.
     public  int getTrack()
     {
         return count;
     }


}
faraq1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodingExpertsCommented:
Probably a homework Question.... ;-)
Well what kind of help do you require on this !!!

Well you need to make a function for purchaseCar() which will increment the count  of the cars...
-CE
0
petmagdyCommented:
here are some tips:

1- Add an Aditional class called CarModel contains Model Name and number of times purchased, with constructor and getters and setters plus a method like this:

puvlic void purchase()
{
 numberOfPurchases++;
}

2- Create  new member in class car of type java.util.Hashtable initiated at the constructor of Car, where is the Key is the model name and the Value is Model CarModel instance.

3- u will add 2 methods in ur Car class lik this:

public void addModel(String modelName)
{
 //construct a Model class with modelName and number of purchases is zero
 //Add it to the models HashTable like this
  models.put(modelName, new CarModel( modelName, 0 ));
}

public void purchase(String modelName)
{
 CarModel model  = (CarModel) models.get(modelName);
  model.purchase();
}

now in ur class Car create a main method that will construct 5 Car objects, add models to each, purchase a number of cars, put the 5 cars into array and display the result

Good Luck :)
0
suprapto45Commented:
Hi,

Please tell me if this is not really useful to you. Assuming that you have three kind of cars (Viper, Toyota and Ford). In your class declaration, you can add three additional fields.

public class Car
{
     private static int ViperPurch, ToyotaPurch, FordPurch;
     private String make, model, year;
     private double price;
     private int count;

     public Car(String tempMake, String tempModel, String tempYear, double tempPrice)
     {
          make = tempMake;
          model = tempModel;
          year = tempYear;
          price = tempPrice;
          count++;
     }

    public int getViperPurch()
    {
        return ViperPurch;
    }

    public int getToyotaPurch()
    {
        return ToyotaPurch;
    }

Then you need to create one additional method in your class.

public void purchase()
{
    if(this.getModel().equals("Viper"))
    {
        ViperPurch++;
    }
    else if(this.getModel().equals("Toyota"))
    {
        ToyotaPurch++;
    }
}

Then in your main class, you can simply do this.

Car[] car = new Car[5];

car[0] = new Car("Dodge", "Viper", "2003", 72471.0);
.
.
.
car[5] = new Car("Blah", "Toyota", "2003", 70071.0);

car[0].purchase();
car[1].purchase();

System.out.println("Number of Viper purchase is : " + getViperPurch());  

Well, I hope that helps. The disadvantage of this is that it will not be that dynamic but it should be sufficient.

Regards
Dave

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
suprapto45Commented:
Oh yes,

Please try whether

    public int getViperPurch()
    {
        return ViperPurch;
    }

    public int getToyotaPurch()
    {
        return ToyotaPurch;
    }

or

    public static int getViperPurch()
    {
        return ViperPurch;
    }

    public static int getToyotaPurch()
    {
        return ToyotaPurch;
    }

Sorry, I have some doubts on that :).
Regards
Dave
0
faraq1Author Commented:
Thanks Ya all, Appreciate it ..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.