Goodiny777

Homework_OOP/targilim 1-2/clock and car classes

Jan 11th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. // main
  2. import java.util.Scanner;
  3. public class objectsClassesFirstLesson {
  4.     public static void main(String[] args){
  5.         Scanner sc = new Scanner(System.in);
  6.         Clock timer = new Clock();
  7.         timer.setHours(23);
  8.         timer.setMinutes(59);
  9.         timer.setSeconds(59);
  10.         timer.show(); // 23:59:59
  11.         timer.tick(); //+1 second
  12.         timer.show(); // 00:00:00
  13.  
  14.         Car bmw = new Car(); //car_id = null, current_speed = 0(default)
  15.         char drive;
  16.         bmw.setCar_id(1234567);// car_id =  1234567 current_speed = 0
  17.  
  18.         System.out.println("To increase speed write: +\nTo decrease speed write: -\nTo stop and see cars number write: stop!");
  19.         do {
  20.             drive = sc.next().charAt(0);
  21.             switch (drive) {
  22.                 case '+':
  23.                     bmw.faster(); //current_speed+=1
  24.                     break;
  25.                 case '-':
  26.                     if (bmw.getCurrent_speed() > 0) { //if current_speed = 0 prints and gets out from the loop
  27.                         bmw.slower(); //current_speed-=1
  28.                         break;
  29.                     } else {
  30.                         drive = 's'; //withot break for the going to next case
  31.                     }
  32.                 case 's':
  33.                     bmw.stopAndPrint();
  34.                     break;
  35.             }
  36.         } while (drive != 's');
  37.     }
  38. }
  39.  
  40. //Targil 1
  41. public class Clock {
  42.     private int hours, minutes, seconds;
  43.  
  44.     public boolean setHours(int hours) {
  45.         if(hours<=0||hours>24){
  46.             this.hours = 0;
  47.             return false;
  48.         }
  49.         else{
  50.             this.hours = hours;
  51.             return true;
  52.         }
  53.     }
  54.  
  55.     public boolean setMinutes(int minutes) {
  56.         if(minutes<=0||minutes>60){
  57.             this.minutes = 0;
  58.             return false;
  59.         }
  60.         else{
  61.             this.minutes = minutes;
  62.             return true;
  63.         }
  64.     }
  65.  
  66.     public boolean setSeconds(int seconds) {
  67.         if(seconds<=0||seconds>60){
  68.             this.seconds = 0;
  69.             return false;
  70.         }
  71.         else{
  72.             this.seconds = seconds;
  73.             return true;
  74.         }
  75.     }
  76.  
  77.     public int getHours() {
  78.         return hours;
  79.     }
  80.  
  81.     public int getMinutes() {
  82.         return minutes;
  83.     }
  84.  
  85.     public int getSeconds() {
  86.         return seconds;
  87.     }
  88.  
  89.     public void tick() {
  90.         this.seconds+=1;
  91.         if (this.seconds>59){
  92.             this.seconds = 0;
  93.             this.minutes+=1;
  94.         }
  95.         if(this.minutes>59){
  96.             this.minutes=0;
  97.             this.hours+=1;
  98.         }
  99.         if(this.hours>23){
  100.             this.hours=0;
  101.         }
  102.     }
  103.  
  104.     public void show() {
  105.         if(this.hours<10)
  106.             System.out.print("0");
  107.         System.out.print(hours+":");
  108.         if(this.minutes<10)
  109.             System.out.print("0");
  110.         System.out.print(minutes+":");
  111.         if(this.seconds<10)
  112.             System.out.print("0");
  113.         System.out.print(seconds);
  114.         System.out.println();
  115.     }
  116. }
  117.  
  118. //Targil 2
  119. public class Car {
  120.     int car_id, current_speed = 0;
  121.  
  122.     public void setCar_id(int car_id) {
  123.         this.car_id = car_id;
  124.     }
  125.  
  126.     public int getCar_id() {
  127.         return car_id;
  128.     }
  129.  
  130.     public int getCurrent_speed() {
  131.         return current_speed;
  132.     }
  133.  
  134.     public void faster(){
  135.         current_speed +=1;
  136.     }
  137.  
  138.     public void slower(){
  139.         if(current_speed>0){
  140.             current_speed -=1;
  141.         }
  142.         else stopAndPrint();
  143.     }
  144.  
  145.     public void stopAndPrint(){
  146.         System.out.println("Starting to stop!");
  147.         if(current_speed!=0) {
  148.             for (int i = current_speed; i > 0; i -= 1) {
  149.                 System.out.print(i + " ");
  150.             }
  151.             System.out.println();
  152.         }
  153.         System.out.println(car_id);
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment