[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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


}
0
faraq1
Asked:
faraq1
1 Solution
 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now