Advertisement
Ekaterina_Slavova

Exam 2

Dec 19th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. package exam2;
  2.  
  3. // Да се дефинират класове за представяне на велосипед чрез модел и цена и на ски чрез дължина, широчина и цена.
  4. // Да се дефинира функция, която намира по‐малката от цените на две стоки, всяка от които може да бъде и велосипед, и ски.
  5. // Да се инициализират по един от двата вида обекти и да се изведат данните за тях и съобщение коя от двете цени е по‐малка.
  6.  
  7. public class Exam2 {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. Ski mySki = new Ski(5.4, 150.4, 234.00);
  12. Bicycle myBicycle = new Bicycle("Mountain bike", 689.95);
  13.  
  14. System.out.println(mySki);
  15. System.out.println(myBicycle);
  16. System.out.println("Lower price is: " + compareProducts(mySki, myBicycle));
  17. }
  18.  
  19. private static double compareProducts(Product pr1, Product pr2) {
  20. return (pr1.getPrice() < pr2.getPrice()) ? pr1.getPrice() : pr2.getPrice();
  21. }
  22. }
  23. -----------------------------------------------------------------------------------------------------------------------------------------
  24. package exam2;
  25.  
  26. public class Product {
  27.  
  28. protected double price;
  29.  
  30. public Product(double price) {
  31. this.price = price;
  32. }
  33.  
  34. public double getPrice() {
  35. return price;
  36. }
  37. }
  38. -----------------------------------------------------------------------------------------------------------------------------------------
  39. package exam2;
  40.  
  41. public class Bicycle extends Product {
  42.  
  43. private final String model;
  44.  
  45. public Bicycle(String model, double price) {
  46. super(price);
  47. this.model = model;
  48. }
  49.  
  50. @Override
  51. public String toString() {
  52. return "Bicycle (model=" + model + ", price=" + price + ")";
  53. }
  54. }
  55. -----------------------------------------------------------------------------------------------------------------------------------------
  56. package exam2;
  57.  
  58. public class Ski extends Product {
  59.  
  60. private final double width;
  61. private final double lenght;
  62.  
  63. public Ski(double width, double height, double price) {
  64. super(price);
  65. this.width = width;
  66. this.lenght = height;
  67. }
  68.  
  69. @Override
  70. public String toString() {
  71. return "Ski (width=" + width + ", height=" + lenght + ", price=" + price + ")";
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement