Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main
- import java.util.Scanner;
- public class objectsClassesFirstLesson {
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- Clock timer = new Clock();
- timer.setHours(23);
- timer.setMinutes(59);
- timer.setSeconds(59);
- timer.show(); // 23:59:59
- timer.tick(); //+1 second
- timer.show(); // 00:00:00
- Car bmw = new Car(); //car_id = null, current_speed = 0(default)
- char drive;
- bmw.setCar_id(1234567);// car_id = 1234567 current_speed = 0
- System.out.println("To increase speed write: +\nTo decrease speed write: -\nTo stop and see cars number write: stop!");
- do {
- drive = sc.next().charAt(0);
- switch (drive) {
- case '+':
- bmw.faster(); //current_speed+=1
- break;
- case '-':
- if (bmw.getCurrent_speed() > 0) { //if current_speed = 0 prints and gets out from the loop
- bmw.slower(); //current_speed-=1
- break;
- } else {
- drive = 's'; //withot break for the going to next case
- }
- case 's':
- bmw.stopAndPrint();
- break;
- }
- } while (drive != 's');
- }
- }
- //Targil 1
- public class Clock {
- private int hours, minutes, seconds;
- public boolean setHours(int hours) {
- if(hours<=0||hours>24){
- this.hours = 0;
- return false;
- }
- else{
- this.hours = hours;
- return true;
- }
- }
- public boolean setMinutes(int minutes) {
- if(minutes<=0||minutes>60){
- this.minutes = 0;
- return false;
- }
- else{
- this.minutes = minutes;
- return true;
- }
- }
- public boolean setSeconds(int seconds) {
- if(seconds<=0||seconds>60){
- this.seconds = 0;
- return false;
- }
- else{
- this.seconds = seconds;
- return true;
- }
- }
- public int getHours() {
- return hours;
- }
- public int getMinutes() {
- return minutes;
- }
- public int getSeconds() {
- return seconds;
- }
- public void tick() {
- this.seconds+=1;
- if (this.seconds>59){
- this.seconds = 0;
- this.minutes+=1;
- }
- if(this.minutes>59){
- this.minutes=0;
- this.hours+=1;
- }
- if(this.hours>23){
- this.hours=0;
- }
- }
- public void show() {
- if(this.hours<10)
- System.out.print("0");
- System.out.print(hours+":");
- if(this.minutes<10)
- System.out.print("0");
- System.out.print(minutes+":");
- if(this.seconds<10)
- System.out.print("0");
- System.out.print(seconds);
- System.out.println();
- }
- }
- //Targil 2
- public class Car {
- int car_id, current_speed = 0;
- public void setCar_id(int car_id) {
- this.car_id = car_id;
- }
- public int getCar_id() {
- return car_id;
- }
- public int getCurrent_speed() {
- return current_speed;
- }
- public void faster(){
- current_speed +=1;
- }
- public void slower(){
- if(current_speed>0){
- current_speed -=1;
- }
- else stopAndPrint();
- }
- public void stopAndPrint(){
- System.out.println("Starting to stop!");
- if(current_speed!=0) {
- for (int i = current_speed; i > 0; i -= 1) {
- System.out.print(i + " ");
- }
- System.out.println();
- }
- System.out.println(car_id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment