Advertisement
deyanmalinov

01.Car Info - Defining Classes - Lab

Apr 25th, 2020
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         Car newCar = new Car();
  7.         int num = Integer.parseInt(scan.nextLine());
  8.         String[] line = scan.nextLine().split("\\s+");
  9.  
  10.         for (int i = 0; i < num; i++) {
  11.             newCar.setMake(line[0]);
  12.             newCar.setModel(line[1]);
  13.             newCar.setHorsePower(Integer.parseInt(line[2]));
  14.  
  15.             System.out.printf("The car is: %s %s - %d HP.%n",
  16.                     newCar.getMake(), newCar.getModel(), newCar.getHorsePower());
  17.             line = scan.nextLine().split("\\s+");
  18.         }
  19.     }
  20. }
  21. ---------------------------------------------------------------------------------
  22. ---------------------------------------------------------------------------------
  23. ---------------------------------------------------------------------------------
  24. package DPM;
  25. public class Car {
  26.         private String make;
  27.     public String getMake() {
  28.         return make;
  29.     }
  30.     public void setMake(String make) {
  31.         this.make = make;
  32.     }
  33.     public String getModel() {
  34.         return model;
  35.     }
  36.     public void setModel(String model) {
  37.         this.model = model;
  38.     }
  39.  
  40.     public int getHorsePower() {
  41.         return horsePower;
  42.     }
  43.     public void setHorsePower(int horsePower) {
  44.         this.horsePower = horsePower;
  45.     }
  46.     private String model;
  47.         private int horsePower;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement