chaibs

oop-hw1-2

Jan 14th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. public class Clock {
  2.  
  3.     public int minutes, hours, seconds;
  4.  
  5.     public int getMinutes() {
  6.         return minutes;
  7.     }
  8.  
  9.     public boolean setMinutes(int minutes) {//set minutes
  10.         if (minutes < 0 || minutes >= 60) {// checking if minutes input is correct
  11.             minutes = 0;
  12.             return false;
  13.         } else {
  14.             this.minutes = minutes;
  15.             return true;
  16.         }
  17.     }
  18.  
  19.     public int getHours() {
  20.         return hours;
  21.     }
  22.  
  23.     public boolean setHours(int hours) {//set hours
  24.         if (hours < 0 || hours >= 24) {// checking if hours input is correct
  25.             hours = 0;
  26.             return false;
  27.         } else {
  28.             this.hours = hours;
  29.             return true;
  30.         }
  31.     }
  32.  
  33.     public int getSeconds() {
  34.         return seconds;
  35.     }
  36.  
  37.     public boolean setSeconds(int seconds) {//set seconds
  38.         if (seconds < 0 || seconds >= 60) {// checking if seconds input is correct
  39.             seconds = 0;
  40.             return false;
  41.         } else {
  42.             this.seconds = seconds;
  43.             return true;
  44.         }
  45.     }
  46.  
  47.     public void tick() {// method for inc the seconds in the clock
  48.         seconds += 1;
  49.         minutes += seconds / 60;
  50.         hours += minutes / 60;
  51.         seconds %= 60;
  52.         minutes %= 60;
  53.         hours %= 24;
  54.     }
  55.  
  56.     public void show() {// method for showing the time
  57.         System.out.print("The time is ");
  58.         if (hours < 10) {
  59.             System.out.print("0");
  60.         }
  61.         System.out.print(hours + ":");
  62.         if (minutes < 10) {
  63.             System.out.print("0");
  64.         }
  65.         System.out.print(minutes + ":");
  66.         if (seconds < 10) {
  67.             System.out.print("0");
  68.         }
  69.         System.out.print(seconds);
  70.     }
  71.  
  72.     public void reset() {// method for reseting the time
  73.         seconds = 0;
  74.         minutes = 0;
  75.         hours = 0;
  76.     }
  77.  
  78. }
  79. ----------------------------------------------------------------------------------------------------------------
  80. import java.util.Scanner;
  81.  
  82. public class shaon {
  83.  
  84.     public static void main(String[] args) {
  85.         Clock c1 = new Clock();
  86.         boolean res;
  87.         Scanner s = new Scanner(System.in);
  88.  
  89.         do {
  90.             System.out.print("Enter the hours: ");
  91.             res = c1.setHours(s.nextInt());// user input for the hour
  92.         } while (res != true);// as long as the input is not correct the program will keep asking for the
  93.                                 // hours
  94.  
  95.         do {
  96.             System.out.print("Enter the minutes: ");
  97.             res = c1.setMinutes(s.nextInt());// user input for the minutes
  98.         } while (res != true);// as long as the input is not correct the program will keep asking for the
  99.                                 // minutes
  100.  
  101.         do {
  102.             System.out.print("Enter the seconds: ");
  103.             res = c1.setSeconds(s.nextInt());// user input for the seconds
  104.         } while (res != true);// as long as the input is not correct the program will keep asking for the
  105.                                 // seconds
  106.  
  107.         c1.show();// showing the time
  108.         System.out.println();
  109.  
  110.         c1.tick();// incrementing the seconds
  111.         c1.tick();// incrementing the seconds
  112.         c1.show();// showing the time
  113.         System.out.println();
  114.  
  115.         c1.reset();// reseting the time
  116.         c1.show();// showing the time
  117.  
  118.     }
  119.  
  120. }
  121. ----------------------------------------------------------------------------------------------------------------
  122.  
  123. public class Car {
  124.  
  125.     public int license, speed = 0;
  126.  
  127.     public int getLicense() {
  128.         return license;
  129.     }
  130.  
  131.     public void setLicense(int license) {// setting car's number
  132.         this.license = license;
  133.     }
  134.  
  135.     public int getSpeed() {
  136.         return speed;
  137.     }
  138.  
  139.     public void setSpeed(int speed) {// setting car's speed
  140.         this.speed = speed;
  141.     }
  142.  
  143.     public void accelerate() {// method for accelerating the car
  144.         speed += 1;
  145.     }
  146.  
  147.     public void decelerate() {// method for decelerating the car
  148.         if (speed > 0) {// checking if car is not already stopped
  149.             speed -= 1;
  150.         }
  151.     }
  152.  
  153.     public void stop() {// method for stopping the car
  154.         speed = 0;
  155.     }
  156.  
  157.     public void show() {// method for showing car current data
  158.         if (speed > 0) {// checking if car is moving
  159.             System.out.println("Car number " + license + " is moving in " + speed + " MPH");
  160.         } else {
  161.             System.out.println("Car number " + license + " is not moving");
  162.         }
  163.  
  164.     }
  165.  
  166. }
  167. ----------------------------------------------------------------------------------------------------------------
  168. import java.util.Scanner;
  169.  
  170. public class oto {
  171.  
  172.     public static void main(String[] args) {
  173.         Car c1 = new Car();
  174.         Scanner s = new Scanner(System.in);
  175.         int speed = 0;
  176.  
  177.         c1.setLicense(654751);// setting car number
  178.         c1.show();// showing the car's data
  179.  
  180.         System.out.println("How fast you wanna go?");
  181.         speed = s.nextInt();// input speed from user
  182.  
  183.         for (int i = 0; i < speed; i += 1) {// calling for accelerate by the speed the user wanted
  184.             c1.accelerate();
  185.         }
  186.         c1.show();// showing the car's data
  187.  
  188.         c1.decelerate();// decelerating the car by 1
  189.         c1.decelerate();// decelerating the car by 1
  190.         c1.show();// showing the car's data
  191.  
  192.         c1.stop();// stopping the car
  193.         c1.show();// showing the car's data
  194.     }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment