Hi guys, so I have this project where the requirements are as follows.

Create a class called Car, which serves to represent a particular vehicle at a car
dealership. Consider that each car has the following properties, entered by the user (all value
ranges should be error checked):
Sticker price (a decimal greater than $10,000)
Year (an integer between 2002 and 2010)
Make (a String that represents the manufacturer of the car)
Top speed (an integer between 100 and 160)
Color (a String)
Availability (a boolean that states whether the car has been sold (false) or is available (true))
The class should also contain methods that perform the following functions:
Determine whether the car is used (if the car was made before 2009)
Apply a certain discount on the sticker price
o The discount percentage should be a parameter to the method
Change a car to being sold
o This replaces the mutator method for the “availability” property
Determine the final cost of the car, considering the following:
o 8.875% sales tax
o $500 delivery fee
Output a summary of all information about the car, using a toString() method

So far I have this,

import javax.swing.JOptionPane;
  
  public class Car
{
  //*******************************************************
  //Declaration of instance variables/fields of the class
  //*******************************************************
  private String make, color;
  private int year, topSpeed;
  private boolean availability;
  private double stickerPrice, discount, tax;
  
  public Car()
  {
    make = "";
    color = "";
    year = 0;
    topSpeed = 0;
    availability = true;
    stickerPrice = 0;
    discount = 0;
  }
  
  public Car(String make1, String color1, int year1, int topSpeed1, boolean availability1, double stickerPrice1, double tax1, double discount1)
  {
    make = make1;
    color = color1;
    year = year1;
    topSpeed = topSpeed1;
    availability = availability1;
    stickerPrice = stickerPrice1;
    tax = tax1;
    discount = discount1;
  }
  
  //***********************************************************
  //getMake()
  //This method will get the make of the car.
  //***********************************************************
  public String getMake()
  {
    return make;
  }
  
  //***********************************************************
  //setMake(String make)
  //This method will set the make of the car.
  //***********************************************************
  public void setMake(String make1)
  {
    make = make1;
  }
  
  //***********************************************************
  //getColor()
  //This method will get the color of the car.
  //***********************************************************
  public String getColor()
  {
    return color;
  }
  
  //***********************************************************
  //setColor(String color)
  //This method will set the color of the car.
  //***********************************************************
  public void setColor(String color1)
  {
    color = color1;
  }
  
  //***********************************************************
  //getYear()
  //This method will get the year that the car was made.
  //***********************************************************
  public int getYear()
  {
    return year;
  }
  
  //***********************************************************
  //setYear(int year1)
  //This method will set the year in which the car was made.
  //***********************************************************
  public void setYear(int year1)
    {
      year = year1;
    }

  
  //***********************************************************
  //getTopSpeed()
  //This method will get the top speed of the car.
  //***********************************************************
  public int getTopSpeed()
  {
    return topSpeed;
  }
  
  //***********************************************************
  //setTopSpeed(int topSpeed1)
  //This method will set the top speed of the car.
  //***********************************************************
  public void setTopSpeed(int topSpeed1)
  {
      topSpeed = topSpeed1;
  }
      
      //***********************************************************
      //getAvailability()
      //This method will get the availabilty of the car.
      //***********************************************************
      public boolean getAvailability()
    {
      return availability;
    }
    
    //***********************************************************
    //setMake(String make)
    //This method will set the make of the car.
    //***********************************************************
    public void setAvailability(boolean availability1)
    {
      availability = availability1;
    }
    
    //***********************************************************
    //getStickerPrice()
    //This method will get the sticker price of the car.
    //***********************************************************
    public double getStickerPrice()
    {
      return stickerPrice;
    }
    
    //***********************************************************
    //setStickerPrice(double stickerPrice)
    //This method will set the sticker price of the car.
    //***********************************************************
    public void setStickerPrice(double stickerPrice1)
    {  
        stickerPrice = stickerPrice1;
    }
    
    //***********************************************************
    //getTax()
    //This method will get the tax on the car.
    //***********************************************************
    public double getTax()
    {
      return tax;
    }
    
    //***********************************************************
    //setMake(String make)
    //This method will set the tax on the car.
    //***********************************************************
    public void setTax(double tax1)
    {
      tax = tax1;
    }
    
    
    //***********************************************************
    //getDiscount()
    //This method will get the discount on the car.
    //***********************************************************
    public double getDiscount()
    {
      return discount;
    }
    
    //***********************************************************
    //setDiscount(double discount1)
    //This method will set the discount on the car.
    //***********************************************************
    public void setDiscount(double discount1)
    {
      discount = discount1;
    }
    
    //************************************************************
    //used()
    //This method if the car is used or not.
    //************************************************************
    public void used(int year)
    {
      if (year < 2009)
      {
        JOptionPane.showMessageDialog("The car is used.");
      }
      else
        JOptionPane.showMessageDialog("The car is new.");
    }
    
    //***************************************************************
    //discount(int percentOff)
    //This method puts a discount on the sticker price.
    //***************************************************************
    public double discount(int percentOff)
    {
      percentOff *= .01;
      stickerPrice = (stickerPrice*percentOff);
      return stickerPrice;
    }
    
    public double totalCost(int stickerPrice, int discount)
    {
  
      
    
          
    }
  }

Sorry about all the commenting in the code, but I find it helps a lot. By the way, this is supposed to be a stand alone class. I'll have to write a driver for it later.

I'm not exactly sure how I'd go about to calculate the final cost or how to change the availability.

Also, an error that I'm getting is that the JOptionPane isn't being found.

Thanks for the help guys, I really really do appreciate it.

Sorry about all the commenting in the code, but I find it helps a lot.

A lot of commenting is a good thing, its a great habit to have, because it helps others read your code and makes it more organized when you have very large projects, comment away.

I'm not exactly sure how I'd go about to calculate the final cost or how to change the availability.

final cost is just stickerPrice - discount right? To change the availability you already have a setAvailability() method, so just call that method, it's either going to be setAvailability(true); or setAvailability(false);

Also, an error that I'm getting is that the JOptionPane isn't being found.

That is because you have to tell the JOptionPane what Swing Component is its parent, in your case, just set it to null JOptionPane.showMessageDialog(null, "The car is used."); Two more things, I was looking at your discount() method, honestly I'm not sure what you're doing there, the math doesn't seem logical. Second thing, in your used() method, a car can be used and still have a year of 2009.

A lot of commenting is a good thing, its a great habit to have, because it helps others read your code and makes it more organized when you have very large projects, comment away.

final cost is just stickerPrice - discount right? To change the availability you already have a setAvailability() method, so just call that method, it's either going to be setAvailability(true); or setAvailability(false);


That is because you have to tell the JOptionPane what Swing Component is its parent, in your case, just set it to null JOptionPane.showMessageDialog(null, "The car is used."); Two more things, I was looking at your discount() method, honestly I'm not sure what you're doing there, the math doesn't seem logical. Second thing, in your used() method, a car can be used and still have a year of 2009.

Thanks for the input. A question I have though is for the final cost, would I have to instantiate a new variable to hold the place of the final cost? And if I do, would it have to be in the method itself?

What I was thinking was this

public double totalCost(int stickerPrice, int discount)
    {
      discount *= .01;
      double finalCost = stickerPrice - (1 - stickerPrice*discount);
      tax = .0875;
      int deliveryFee = 500;
      finalCost = (finalCost * 1.0875) + 500;
      return finalCost;
    }
  }

Does the code make sense?

In the discount method, the user would be asked to input what percent off to be applied to the sticker price of the car. Because it will be an int, I had to multiply it by .01 to get the decimal value of the discount (to make the multiplication simple).

Also, I was looking over the code and I don't think I need to have the discount method return the discounted price as it wouldn't serve any purpose. Or would it?

Oh man, how could I miss something so simple for the JOptionPane. I'm only a few months into my Highschool AP Java course; thanks again for the help.

The way your totalCost() method is now makes sense except for

double finalCost = stickerPrice - (1 - stickerPrice*discount);//why are you subtracting from 1??

If you are going to be multiplying by .01, or any decimal, you need to make your variable a double, not an int, otherwise it just rounds up. So change discount to a double.


And no, I don't see the purpose in having discount() return anything. I think set and get methods are all you need for discount.

The way your totalCost() method is now makes sense except for

double finalCost = stickerPrice - (1 - stickerPrice*discount);//why are you subtracting from 1??

If you are going to be multiplying by .01, or any decimal, you need to make your variable a double, not an int, otherwise it just rounds up. So change discount to a double.


And no, I don't see the purpose in having discount() return anything. I think set and get methods are all you need for discount.

Ah right, I was thinking of something else while writing the equation. It doesn't make sense to subtract 1 in this case.

Thanks for the catch on the data type too.

I'll be writing a driver class soon, and I'll post how it runs and if I need help, otherwise I'll post that everything went smoothly.

Thanks for all the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.