942,516 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1370
  • Java RSS
Nov 21st, 2009
1

Help - Making methods, error checking, writing class. Thanks :)

Expand Post »
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,
Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Car
  4. {
  5. //*******************************************************
  6. //Declaration of instance variables/fields of the class
  7. //*******************************************************
  8. private String make, color;
  9. private int year, topSpeed;
  10. private boolean availability;
  11. private double stickerPrice, discount, tax;
  12.  
  13. public Car()
  14. {
  15. make = "";
  16. color = "";
  17. year = 0;
  18. topSpeed = 0;
  19. availability = true;
  20. stickerPrice = 0;
  21. discount = 0;
  22. }
  23.  
  24. public Car(String make1, String color1, int year1, int topSpeed1, boolean availability1, double stickerPrice1, double tax1, double discount1)
  25. {
  26. make = make1;
  27. color = color1;
  28. year = year1;
  29. topSpeed = topSpeed1;
  30. availability = availability1;
  31. stickerPrice = stickerPrice1;
  32. tax = tax1;
  33. discount = discount1;
  34. }
  35.  
  36. //***********************************************************
  37. //getMake()
  38. //This method will get the make of the car.
  39. //***********************************************************
  40. public String getMake()
  41. {
  42. return make;
  43. }
  44.  
  45. //***********************************************************
  46. //setMake(String make)
  47. //This method will set the make of the car.
  48. //***********************************************************
  49. public void setMake(String make1)
  50. {
  51. make = make1;
  52. }
  53.  
  54. //***********************************************************
  55. //getColor()
  56. //This method will get the color of the car.
  57. //***********************************************************
  58. public String getColor()
  59. {
  60. return color;
  61. }
  62.  
  63. //***********************************************************
  64. //setColor(String color)
  65. //This method will set the color of the car.
  66. //***********************************************************
  67. public void setColor(String color1)
  68. {
  69. color = color1;
  70. }
  71.  
  72. //***********************************************************
  73. //getYear()
  74. //This method will get the year that the car was made.
  75. //***********************************************************
  76. public int getYear()
  77. {
  78. return year;
  79. }
  80.  
  81. //***********************************************************
  82. //setYear(int year1)
  83. //This method will set the year in which the car was made.
  84. //***********************************************************
  85. public void setYear(int year1)
  86. {
  87. year = year1;
  88. }
  89.  
  90.  
  91. //***********************************************************
  92. //getTopSpeed()
  93. //This method will get the top speed of the car.
  94. //***********************************************************
  95. public int getTopSpeed()
  96. {
  97. return topSpeed;
  98. }
  99.  
  100. //***********************************************************
  101. //setTopSpeed(int topSpeed1)
  102. //This method will set the top speed of the car.
  103. //***********************************************************
  104. public void setTopSpeed(int topSpeed1)
  105. {
  106. topSpeed = topSpeed1;
  107. }
  108.  
  109. //***********************************************************
  110. //getAvailability()
  111. //This method will get the availabilty of the car.
  112. //***********************************************************
  113. public boolean getAvailability()
  114. {
  115. return availability;
  116. }
  117.  
  118. //***********************************************************
  119. //setMake(String make)
  120. //This method will set the make of the car.
  121. //***********************************************************
  122. public void setAvailability(boolean availability1)
  123. {
  124. availability = availability1;
  125. }
  126.  
  127. //***********************************************************
  128. //getStickerPrice()
  129. //This method will get the sticker price of the car.
  130. //***********************************************************
  131. public double getStickerPrice()
  132. {
  133. return stickerPrice;
  134. }
  135.  
  136. //***********************************************************
  137. //setStickerPrice(double stickerPrice)
  138. //This method will set the sticker price of the car.
  139. //***********************************************************
  140. public void setStickerPrice(double stickerPrice1)
  141. {
  142. stickerPrice = stickerPrice1;
  143. }
  144.  
  145. //***********************************************************
  146. //getTax()
  147. //This method will get the tax on the car.
  148. //***********************************************************
  149. public double getTax()
  150. {
  151. return tax;
  152. }
  153.  
  154. //***********************************************************
  155. //setMake(String make)
  156. //This method will set the tax on the car.
  157. //***********************************************************
  158. public void setTax(double tax1)
  159. {
  160. tax = tax1;
  161. }
  162.  
  163.  
  164. //***********************************************************
  165. //getDiscount()
  166. //This method will get the discount on the car.
  167. //***********************************************************
  168. public double getDiscount()
  169. {
  170. return discount;
  171. }
  172.  
  173. //***********************************************************
  174. //setDiscount(double discount1)
  175. //This method will set the discount on the car.
  176. //***********************************************************
  177. public void setDiscount(double discount1)
  178. {
  179. discount = discount1;
  180. }
  181.  
  182. //************************************************************
  183. //used()
  184. //This method if the car is used or not.
  185. //************************************************************
  186. public void used(int year)
  187. {
  188. if (year < 2009)
  189. {
  190. JOptionPane.showMessageDialog("The car is used.");
  191. }
  192. else
  193. JOptionPane.showMessageDialog("The car is new.");
  194. }
  195.  
  196. //***************************************************************
  197. //discount(int percentOff)
  198. //This method puts a discount on the sticker price.
  199. //***************************************************************
  200. public double discount(int percentOff)
  201. {
  202. percentOff *= .01;
  203. stickerPrice = (stickerPrice*percentOff);
  204. return stickerPrice;
  205. }
  206.  
  207. public double totalCost(int stickerPrice, int discount)
  208. {
  209.  
  210.  
  211.  
  212.  
  213. }
  214. }
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.
Last edited by Ecliptical210; Nov 21st, 2009 at 7:11 pm. Reason: Not sufficient title for thread
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ecliptical210 is offline Offline
7 posts
since Nov 2009
Nov 21st, 2009
0
Re: Help please, thanks
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.
Last edited by jasimp; Nov 21st, 2009 at 7:44 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 21st, 2009
0
Re: Help please, thanks
Click to Expand / Collapse  Quote originally posted by jasimp ...
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
Java Syntax (Toggle Plain Text)
  1. public double totalCost(int stickerPrice, int discount)
  2. {
  3. discount *= .01;
  4. double finalCost = stickerPrice - (1 - stickerPrice*discount);
  5. tax = .0875;
  6. int deliveryFee = 500;
  7. finalCost = (finalCost * 1.0875) + 500;
  8. return finalCost;
  9. }
  10. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ecliptical210 is offline Offline
7 posts
since Nov 2009
Nov 21st, 2009
0
Re: Help please, thanks
The way your totalCost() method is now makes sense except for
Java Syntax (Toggle Plain Text)
  1. 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.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 21st, 2009
0
Re: Help please, thanks
Click to Expand / Collapse  Quote originally posted by jasimp ...
The way your totalCost() method is now makes sense except for
Java Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ecliptical210 is offline Offline
7 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can I write into a jar?
Next Thread in Java Forum Timeline: Round-Robin Scheduling Algorithm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC