Chinese University mooc- Zhejiang University Weng Kai (Java advanced) second weeks programming questions

Keywords: Java Programming less

Topic:

This week's programming questions need to be revised based on the clock program given in the course. But we don't give you the code of the clock program directly. Please input the code of Display and Clock classes of the clock program according to the video itself, and then do this topic.

We need to add a Dislay for seconds to the clock program, and then add the following public member functions to Clock:

public Clock(int hour, int minute, int second){
         Initialize time with hour, minute and second.
	}

public void tick(){
        "Check" for a second.
  	}
public String toString(){
        Returns a String value that represents the current time in the form of "hh:mm:ss".
        Each of these values occupies two places, less than two times to make up zero. For example, "00:01:22".
        Note that the colon is in western language, not in Chinese.
	}
	
* Tip: String.format() can format a string * in the same way as printf.

In addition, write a Main class whose main function looks like the following. Note that main function must remain intact as 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();

}

Be careful! At the time of submission, the code of the Main, Clock and Display classes is merged, in which the Main class is public, while the Clock and Display classes have no modifiers. Also, be careful that the first line cannot have a package statement.

The code is as follows:
class Clock {

	private Display hour = new Display(24);
	private Display minute= new Display(60);
	private Display second = new Display(60);
	
	public Clock(int h, int m, int s) {
		hour.setvalue(h);
		minute.setvalue(m);
		second.setvalue(s);
	}


	public void tick() {
		second.increase();
		if(second.getValue() == 60) {
			minute.increase();
				if(minute.getValue() == 60) {
					hour.increase();
				}
		}
	}

	public String toString() {
		String s = String.format("%02d:%02d:%02d",hour.getValue(), minute.getValue(), second.getValue());
		return s;
	}


	
//	public void start()
//	{
//		while (true)
//		{
//			minute.increase();
//			if (minute.getValue() == 0)
//			{
//				hour.increase();
//			}
//			System.out.printf("%02d:%02d\n",hour.getValue(),minute.getValue());
//		}
//	}
	
}
class Display {

		private int value = 0;
		private int limit = 0;
		
		public Display(int limit)
		{
			this.limit = limit;
		}
		
		public void setvalue(int value) {
			this.value = value;
		}
		
		public void increase()
		{
			value++;
			if (value == limit)
			{
				value = 0;
			}
		}
		
		public int getValue()
		{
			return value;
		}
	}	
	
	
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();

		}

}

Posted by Slashscape on Thu, 03 Oct 2019 18:36:19 -0700