Advertisement
Guest User

solution of car

a guest
Nov 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. solution of car Class
  2. ============================
  3.  
  4. public class Car {
  5.     private String carNumber;
  6.     private int carSpeed;
  7.    
  8.     public String getCarNumber() {
  9.         return carNumber;
  10.     }
  11.     public void setCarNumber(String carNumber) {
  12.         this.carNumber = carNumber;
  13.     }
  14.     public int getCarSpeed() {
  15.         return carSpeed;
  16.     }
  17.    
  18.     public void incSpeed()
  19.     {
  20.         carSpeed+=1;
  21.     }
  22.    
  23.     public void decSpeed()
  24.     {
  25.         if (carSpeed>0)
  26.         {
  27.             carSpeed-=1;
  28.         }
  29.     }
  30.    
  31.     public void carStop()
  32.     {
  33.         while (carSpeed>0)
  34.         {
  35.             carSpeed-=1;
  36.         }
  37.     }
  38.    
  39.     public void getInfo()
  40.     {
  41.         System.out.println("Car Number: "+carNumber+" car speed:"+carSpeed);
  42.     }
  43. }
  44.  
  45.  
  46.  
  47. Tester.java
  48. =================
  49. import java.util.Scanner;
  50.  
  51.  
  52. public class Tester {
  53.  
  54.     public static void main(String[] args) {
  55.         Scanner s = new Scanner(System.in);
  56.        
  57.         //create new instance of Car class
  58.         Car myCar = new Car();
  59.         //set car number
  60.         System.out.println("What is car number:");
  61.         String myCarNum=s.nextLine();
  62.         myCar.setCarNumber(myCarNum);
  63.         //print car info
  64.         myCar.getInfo();
  65.        
  66.         System.out.println("To which speed you want to reach:");
  67.         int mySpeed=s.nextInt();
  68.        
  69.         for (int counter=myCar.getCarSpeed();counter<mySpeed;counter+=1)
  70.         {
  71.             myCar.incSpeed();
  72.         }
  73.         myCar.getInfo();
  74.        
  75.         myCar.decSpeed();
  76.         myCar.decSpeed();
  77.         myCar.getInfo();
  78.        
  79.         myCar.carStop();
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement