Java-- There seconds time digital clock

Course: Object-oriented programming language --Java ( Weng Kai )


Title Contents:

This week's programming problem is you need to modify the clock is made on the basis of a program of courses given on. But we do not direct you to the code clock program, the code input clock program their own video and Clock Display category based on, and then do the subject.

We need to add a program to the clock represented seconds Display, and then add the following as the Clock public member functions:

 

public Clock(int hour, int minute, int second);

    With hour, minute and second initialization time.

public void tick();

    "Tick" look, time to go 1 second.

public String toString();

    String returns a value to represent the current time in the form "hh: ss: mm" is. Each value here occupy two, two make up less than 0:00. Such as "00:01:22." Note where the colon is Western, not the Chinese.

 

Tip: String.format () and printf may be formatted as a string manner.

 

Also write a Main class, its main function as something like, attention must be intact as Main's main functions:

	public static void main(String[] args) {

		java.util.Scanner in = new java.util.Scanner(System.in);

		Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());

		clock.tick();

		System.out.println(clock);

		in.close();

	}

Input formats:

Using the given input and output without regard to the main function.

Output formats:

Using the given input and output without regard to the main function.

Sample input:

Using the given input and output without regard to the main function.

Sample output:

Using the given input and output without regard to the main function.

Time limit: 500ms Memory Limit: 32000kb

Code:

public class Main{
	
	public static void main(String[] args) {

		java.util.Scanner in = new java.util.Scanner(System.in);
		Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());
		clock.tick();
		System.out.println(clock);
		in.close();

	}
}

class Clock{	
	private Display hourDP = new Display(24);
	private Display minuteDP = new Display(60);
	private Display secondDP = new Display(60);
	
	public Clock(int hour, int minute, int second) {
		
		hourDP.setValue(hour);
		minuteDP.setValue(minute);
		secondDP.setValue(second);
		
	}
	
	public void tick() {
		
		secondDP.increase();
		if(secondDP.getValue()==0) {
			minuteDP.increase();
			if(minuteDP.getValue()==0) {
				hourDP.increase();
			}
		}
	}
	
	@Override
	public String toString() {
		String time = String.format("%02d:%02d:%02d", hourDP.getValue(),minuteDP.getValue(),secondDP.getValue());
		return time;
	}
	
//	public void start() {
//		while(true) {
//			secondDP.increase();
//			if(secondDP.getValue()==0) {
//				minuteDP.increase();
//				if(minuteDP.getValue()==0) {
//					hourDP.increase();
//				}
//			}
//			System.out.printf("%02d:%02d\n",hourDP.getValue(),minuteDP.getValue());
//		}
//	}
}

class Display{
	private int limit = 0;
	private int value = 0;
	
	public Display(int limit) {
		this.limit = limit;
	}
	
//	public Display(int limit, int value) {
//		this.limit = limit;
//		this.value = value;
//	}
	
	public void increase() {
		value++;
		if(value == limit) {
			value = 0;
		}
	}
	
	public int getValue() {
		return value;
	}
	
	public void setValue(int value) {
		this.value = value;
	}
	
}

 

Published 37 original articles · won praise 47 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u013378642/article/details/103132460