|
| 1 | +/* |
| 2 | +
|
| 3 | + Author |
| 4 | + Himanshu Sharma |
| 5 | + himanshusharma2972@gmail.com |
| 6 | + www.blaregroup.com |
| 7 | + |
| 8 | +
|
| 9 | +
|
| 10 | + =============================================================== |
| 11 | + Digital clock on terminal |
| 12 | + =============================================================== |
| 13 | +
|
| 14 | +
|
| 15 | + About |
| 16 | + This is a simple java program for practice purpose |
| 17 | + This program shows you Digital Clock on terminal |
| 18 | +
|
| 19 | +*/ |
| 20 | +import java.time.LocalDateTime; //importing module for time |
| 21 | +class DigitalClock |
| 22 | +{ |
| 23 | + |
| 24 | + //method to create digit pattern |
| 25 | + void repeat(int top,int bottom,int middle,int upperleft,int lowerleft,int upperright,int lowerright,int height,int i) |
| 26 | + { |
| 27 | + if(i%2==0 && i!=0) |
| 28 | + { if(height==4 || height==6) |
| 29 | + { |
| 30 | + System.out.print(" : "); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + System.out.print(" "); |
| 35 | + } |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + |
| 40 | + |
| 41 | + System.out.print(" "); |
| 42 | + |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + if((height==1 || height==2 || height==3 || height==4 || height==5) && upperleft==1) |
| 47 | + { |
| 48 | + System.out.print("*"); |
| 49 | + } |
| 50 | + else if((height==1 || height==2 || height==3 || height==4 || height==5 ) && upperleft==0) |
| 51 | + { |
| 52 | + System.out.print(" "); |
| 53 | + } |
| 54 | + |
| 55 | + if((height==6 || height==7 || height==8 || height==9 || height==10) && lowerleft==1) |
| 56 | + { |
| 57 | + System.out.print("*"); |
| 58 | + } |
| 59 | + else if((height==6 || height==7 || height==8 || height==9 || height==10 ) && lowerleft==0) |
| 60 | + { |
| 61 | + System.out.print(" "); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + for(int width=1;width<=5;width++) |
| 67 | + { |
| 68 | + if(height==1 && top==1) |
| 69 | + { |
| 70 | + System.out.print("*"); |
| 71 | + } |
| 72 | + else if(height==5 && middle==1) |
| 73 | + { |
| 74 | + System.out.print("*"); |
| 75 | + } |
| 76 | + else if(height==10 && bottom==1) |
| 77 | + { |
| 78 | + System.out.print("*"); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + System.out.print(" "); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + if((height==1 || height==2 || height==3 || height==4 || height==5) && upperright==1) |
| 88 | + { |
| 89 | + System.out.print("*"); |
| 90 | + } |
| 91 | + else if((height==1 || height==2 || height==3 || height==4 || height==5) && upperright==0) |
| 92 | + { |
| 93 | + System.out.print(" "); |
| 94 | + } |
| 95 | + |
| 96 | + if((height==6 || height==7 || height==8 || height==9 || height==10 ) && lowerright==1) |
| 97 | + { |
| 98 | + System.out.print("*"); |
| 99 | + } |
| 100 | + else if((height==6 || height==7 || height==8 || height==9 || height==10) && lowerright==0) |
| 101 | + { |
| 102 | + System.out.print(" "); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + public static void main(String args[]) |
| 113 | + { |
| 114 | + |
| 115 | + DigitalClock obj = new DigitalClock(); |
| 116 | + |
| 117 | + |
| 118 | + // useful variables to design pattern |
| 119 | + int top = 1; |
| 120 | + int lowerleft = 1; |
| 121 | + int upperleft = 1; |
| 122 | + int upperright = 1; |
| 123 | + int lowerright = 1; |
| 124 | + int bottom = 1; |
| 125 | + int middle = 1; |
| 126 | + int loop=0; |
| 127 | + |
| 128 | + |
| 129 | + //useful variable for taking time from system and store it in a string |
| 130 | + int hour,minute,second; |
| 131 | + String hourzero =""; |
| 132 | + String minutezero = ""; |
| 133 | + String secondzero = ""; |
| 134 | + String time=""; |
| 135 | + |
| 136 | + LocalDateTime now; //class object |
| 137 | + |
| 138 | + //main process |
| 139 | + while(loop!=1) |
| 140 | + { |
| 141 | + try{ |
| 142 | + Thread.sleep(70); //for holding screen |
| 143 | + System.out.print("\033[H\033[2J"); //for clearing screen |
| 144 | + System.out.flush(); |
| 145 | + |
| 146 | + } |
| 147 | + catch(Exception e) |
| 148 | + { |
| 149 | + } |
| 150 | + |
| 151 | + now= LocalDateTime.now(); //for taking time from system |
| 152 | + hour = now.getHour(); //for separating hours from time |
| 153 | + minute = now.getMinute(); //for separating minutes from time |
| 154 | + second = now.getSecond(); //for separating seconds from time |
| 155 | + |
| 156 | + |
| 157 | + //adjusting number of zero in time |
| 158 | + if(hour<10){ |
| 159 | + hourzero="0"; |
| 160 | + } |
| 161 | + else{ |
| 162 | + hourzero=""; |
| 163 | + } |
| 164 | + |
| 165 | + if(minute<9){ |
| 166 | + minutezero="0"; |
| 167 | + } |
| 168 | + else{ |
| 169 | + minutezero=""; |
| 170 | + } |
| 171 | + if(second<9){ |
| 172 | + secondzero="0"; |
| 173 | + } |
| 174 | + else{ |
| 175 | + secondzero=""; |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + //storing time in string for manupulation |
| 180 | + time =String.format(("%s"+Integer.toString(hour) + "%s"+Integer.toString(minute) + "%s"+Integer.toString(second)),hourzero,minutezero,secondzero); |
| 181 | + |
| 182 | + //creation of pattern |
| 183 | + for(int height=1;height<=10;height++) |
| 184 | + { System.out.println(); |
| 185 | + for(int i=0;i<time.length();i++) |
| 186 | + { |
| 187 | + switch(time.charAt(i)) |
| 188 | + { |
| 189 | + case '0':top=1;middle=0;bottom=1;upperleft=1;lowerleft=1;upperright=1;lowerright=1;break; |
| 190 | + case '1':top=0;middle=0;bottom=0;upperleft=0;lowerleft=0;upperright=1;lowerright=1;break; |
| 191 | + case '2':top=1;middle=1;bottom=1;upperleft=0;lowerleft=1;upperright=1;lowerright=0;break; |
| 192 | + case '3':top=1;middle=1;bottom=1;upperleft=0;lowerleft=0;upperright=1;lowerright=1;break; |
| 193 | + case '4':top=0;middle=1;bottom=0;upperleft=1;lowerleft=0;upperright=1;lowerright=1;break; |
| 194 | + case '5':top=1;middle=1;bottom=1;upperleft=1;lowerleft=0;upperright=0;lowerright=1;break; |
| 195 | + case '6':top=1;middle=1;bottom=1;upperleft=1;lowerleft=1;upperright=0;lowerright=1;break; |
| 196 | + case '7':top=1;middle=0;bottom=0;upperleft=0;lowerleft=0;upperright=1;lowerright=1;break; |
| 197 | + case '8':top=1;middle=1;bottom=1;upperleft=1;lowerleft=1;upperright=1;lowerright=1;break; |
| 198 | + case '9':top=1;middle=1;bottom=1;upperleft=1;lowerleft=0;upperright=1;lowerright=1;break; |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | + obj.repeat(top,bottom,middle,upperleft,lowerleft,upperright,lowerright,height,i); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + System.out.println(""); |
| 209 | + |
| 210 | + } |
| 211 | +} |
0 commit comments