Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Clock {
- public int minutes, hours, seconds;
- public int getMinutes() {
- return minutes;
- }
- public boolean setMinutes(int minutes) {//set minutes
- if (minutes < 0 || minutes >= 60) {// checking if minutes input is correct
- minutes = 0;
- return false;
- } else {
- this.minutes = minutes;
- return true;
- }
- }
- public int getHours() {
- return hours;
- }
- public boolean setHours(int hours) {//set hours
- if (hours < 0 || hours >= 24) {// checking if hours input is correct
- hours = 0;
- return false;
- } else {
- this.hours = hours;
- return true;
- }
- }
- public int getSeconds() {
- return seconds;
- }
- public boolean setSeconds(int seconds) {//set seconds
- if (seconds < 0 || seconds >= 60) {// checking if seconds input is correct
- seconds = 0;
- return false;
- } else {
- this.seconds = seconds;
- return true;
- }
- }
- public void tick() {// method for inc the seconds in the clock
- seconds += 1;
- minutes += seconds / 60;
- hours += minutes / 60;
- seconds %= 60;
- minutes %= 60;
- hours %= 24;
- }
- public void show() {// method for showing the time
- System.out.print("The time is ");
- if (hours < 10) {
- System.out.print("0");
- }
- System.out.print(hours + ":");
- if (minutes < 10) {
- System.out.print("0");
- }
- System.out.print(minutes + ":");
- if (seconds < 10) {
- System.out.print("0");
- }
- System.out.print(seconds);
- }
- public void reset() {// method for reseting the time
- seconds = 0;
- minutes = 0;
- hours = 0;
- }
- }
- ----------------------------------------------------------------------------------------------------------------
- import java.util.Scanner;
- public class shaon {
- public static void main(String[] args) {
- Clock c1 = new Clock();
- boolean res;
- Scanner s = new Scanner(System.in);
- do {
- System.out.print("Enter the hours: ");
- res = c1.setHours(s.nextInt());// user input for the hour
- } while (res != true);// as long as the input is not correct the program will keep asking for the
- // hours
- do {
- System.out.print("Enter the minutes: ");
- res = c1.setMinutes(s.nextInt());// user input for the minutes
- } while (res != true);// as long as the input is not correct the program will keep asking for the
- // minutes
- do {
- System.out.print("Enter the seconds: ");
- res = c1.setSeconds(s.nextInt());// user input for the seconds
- } while (res != true);// as long as the input is not correct the program will keep asking for the
- // seconds
- c1.show();// showing the time
- System.out.println();
- c1.tick();// incrementing the seconds
- c1.tick();// incrementing the seconds
- c1.show();// showing the time
- System.out.println();
- c1.reset();// reseting the time
- c1.show();// showing the time
- }
- }
- ----------------------------------------------------------------------------------------------------------------
- public class Car {
- public int license, speed = 0;
- public int getLicense() {
- return license;
- }
- public void setLicense(int license) {// setting car's number
- this.license = license;
- }
- public int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {// setting car's speed
- this.speed = speed;
- }
- public void accelerate() {// method for accelerating the car
- speed += 1;
- }
- public void decelerate() {// method for decelerating the car
- if (speed > 0) {// checking if car is not already stopped
- speed -= 1;
- }
- }
- public void stop() {// method for stopping the car
- speed = 0;
- }
- public void show() {// method for showing car current data
- if (speed > 0) {// checking if car is moving
- System.out.println("Car number " + license + " is moving in " + speed + " MPH");
- } else {
- System.out.println("Car number " + license + " is not moving");
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------------
- import java.util.Scanner;
- public class oto {
- public static void main(String[] args) {
- Car c1 = new Car();
- Scanner s = new Scanner(System.in);
- int speed = 0;
- c1.setLicense(654751);// setting car number
- c1.show();// showing the car's data
- System.out.println("How fast you wanna go?");
- speed = s.nextInt();// input speed from user
- for (int i = 0; i < speed; i += 1) {// calling for accelerate by the speed the user wanted
- c1.accelerate();
- }
- c1.show();// showing the car's data
- c1.decelerate();// decelerating the car by 1
- c1.decelerate();// decelerating the car by 1
- c1.show();// showing the car's data
- c1.stop();// stopping the car
- c1.show();// showing the car's data
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment