Skip to content

Commit 2e648da

Browse files
authored
[Lab_01] Added First Lab with Clocks (#1)
* [Lab_01] Added Increase, change time with tests, Readme * Added getters, setters and test for it
1 parent 663c84e commit 2e648da

File tree

8 files changed

+274
-2
lines changed

8 files changed

+274
-2
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
# java-network-apps
2-
Course Development Methods for Java Network Applications, spring 2021
1+
# Development Methods for Java Network Applications. Spring. 2021
2+
This repository stores laboratory works of Development Methods for Java Network Applications course of spring 2021
3+
4+
*Language*: Java
5+
*Tools*: Maven
6+
7+
### Themes of labs:
8+
1. lab_01 - *First steps in Java. Easy classes Clock and AdvancedClock*
9+
10+
11+
© Copyright Sidorova Alexandra, 2021

lab_01/lab_01.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="15" />

lab_01/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>lab_01.com</groupId>
8+
<artifactId>lab_01</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<version>3.8.1</version>
16+
<configuration>
17+
<source>15</source>
18+
<target>15</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
</project>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package lab_01;
2+
3+
import java.io.IOException;
4+
5+
public class Clock {
6+
protected String type;
7+
protected String brand;
8+
protected Integer cost;
9+
protected Integer hours, minutes;
10+
11+
public Clock(String brand, Integer cost, Integer hours, Integer minutes) throws Exception {
12+
this.type = "Common";
13+
this.brand = brand;
14+
this.cost = cost;
15+
16+
setHours(hours);
17+
setMinutes(minutes);
18+
}
19+
20+
protected void checkValue(Integer value) throws Exception {
21+
if (value < 0)
22+
throw new Exception("Value must be not negative!");
23+
}
24+
25+
public void setBrand(String brand) {
26+
this.brand = brand;
27+
}
28+
29+
public void setCost(Integer cost) {
30+
this.cost = cost;
31+
}
32+
33+
public void setHours(Integer hours) throws Exception {
34+
try {
35+
checkValue(hours);
36+
this.hours = hours % 24;
37+
} catch (IOException e) {
38+
System.out.println(e.getMessage());
39+
}
40+
}
41+
42+
public void setMinutes(Integer minutes) throws Exception {
43+
try {
44+
checkValue(minutes);
45+
setHours(this.hours + minutes / 60);
46+
this.minutes = minutes % 60;
47+
} catch (IOException e) {
48+
System.out.println(e.getMessage());
49+
}
50+
}
51+
52+
public Integer getHours() {
53+
return this.hours;
54+
}
55+
56+
public Integer getMinutes() {
57+
return this.minutes;
58+
}
59+
60+
public String getBrand() {
61+
return this.brand;
62+
}
63+
64+
public Integer getCost() {
65+
return this.cost;
66+
}
67+
68+
public void increaseTime(Integer value) throws Exception {
69+
try {
70+
checkValue(value);
71+
setMinutes((this.minutes + value));
72+
} catch (IOException e) {
73+
System.out.println(e.getMessage());
74+
}
75+
}
76+
77+
public void printInformation() {
78+
System.out.println("\tClock type: " + this.type + "\n\tBrand: " + this.brand +"\n\tCost: " + this.cost);
79+
printTime();
80+
}
81+
82+
public void printTime() {
83+
System.out.println("\tTime: " + this.hours + "h. " + this.minutes + "m.");
84+
}
85+
86+
87+
public static class AdvancedClock extends Clock {
88+
private Integer seconds;
89+
90+
public AdvancedClock(String brand, Integer cost, Integer hours, Integer minutes, Integer seconds) throws Exception {
91+
super(brand, cost, hours, minutes);
92+
93+
this.type = "Advanced";
94+
setSeconds(seconds);
95+
}
96+
97+
public void setSeconds(Integer seconds) throws Exception {
98+
try {
99+
checkValue(seconds);
100+
setMinutes(this.minutes + seconds / 60);
101+
this.seconds = seconds % 60;
102+
} catch (IOException e) {
103+
System.out.println(e.getMessage());
104+
}
105+
}
106+
107+
public Integer getSeconds() {
108+
return this.seconds;
109+
}
110+
111+
@Override
112+
public void increaseTime(Integer value) throws Exception {
113+
try {
114+
checkValue(value);
115+
setSeconds((this.seconds + value));
116+
} catch (IOException e) {
117+
System.out.println(e.getMessage());
118+
}
119+
}
120+
121+
@Override
122+
public void printTime() {
123+
System.out.println("\tAdvanced Time: " + this.hours + "h. " + this.minutes + "m. " + this.seconds + "s. ");
124+
}
125+
}
126+
}

lab_01/src/main/java/lab_01/Main.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package lab_01;
2+
3+
4+
import java.io.IOException;
5+
import java.util.Scanner;
6+
7+
public class Main {
8+
public static void main(String[] argv) throws Exception {
9+
System.out.println("======== CLOCKS ========");
10+
11+
try {
12+
System.out.println("\n###TEST_1 <Set start time>");
13+
test_start_time();
14+
} catch (IOException e) {
15+
System.out.println(e.getMessage());
16+
}
17+
18+
try {
19+
System.out.println("\n###TEST_2 <Change time>");
20+
test_change_time();
21+
} catch (IOException e) {
22+
System.out.println(e.getMessage());
23+
}
24+
25+
try {
26+
System.out.println("\n###TEST_3 <Increase time>");
27+
test_increase_time();
28+
} catch (IOException e) {
29+
System.out.println(e.getMessage());
30+
}
31+
32+
try {
33+
System.out.println("\n###TEST_4 <Getters and Setters>");
34+
test_getters_and_setters();
35+
} catch (IOException e) {
36+
System.out.println(e.getMessage());
37+
}
38+
}
39+
40+
public static void test_start_time() throws Exception {
41+
Clock clock = new Clock("common", 100, 0, 0);
42+
System.out.println("1. Information:");
43+
clock.printInformation();
44+
45+
Clock.AdvancedClock advancedClock = new Clock.AdvancedClock("casio", 2000, 0, 0, 0);
46+
System.out.println("2. Information:");
47+
advancedClock.printInformation();
48+
}
49+
50+
public static void test_change_time() throws Exception {
51+
Clock.AdvancedClock advancedClock = new Clock.AdvancedClock("casio", 2000, 0, 0, 0);
52+
System.out.println("Information:");
53+
advancedClock.printInformation();
54+
55+
System.out.println("Set 2 112 528 seconds:");
56+
advancedClock.setSeconds(2112528);
57+
advancedClock.printInformation();
58+
}
59+
60+
public static void test_increase_time() throws Exception {
61+
Clock.AdvancedClock advancedClock = new Clock.AdvancedClock("overwatch", 15000, 22, 58, 58);
62+
System.out.println("Information:");
63+
advancedClock.printInformation();
64+
65+
System.out.println("Add 1 second:");
66+
advancedClock.increaseTime(1);
67+
advancedClock.printInformation();
68+
69+
System.out.println("Add 120 seconds:");
70+
advancedClock.increaseTime(120);
71+
advancedClock.printInformation();
72+
73+
System.out.println("Add 120 seconds:");
74+
advancedClock.increaseTime(120);
75+
advancedClock.printInformation();
76+
}
77+
78+
public static void test_getters_and_setters() throws Exception {
79+
Clock.AdvancedClock advancedClock = new Clock.AdvancedClock("", 0, 0, 0, 0);
80+
System.out.println("Information:");
81+
advancedClock.printInformation();
82+
83+
Scanner scanner = new Scanner(System.in);
84+
System.out.println("Set brand: ");
85+
String brand = scanner.nextLine();
86+
advancedClock.setBrand(brand);
87+
System.out.println("Get brand: " + advancedClock.getBrand() + "\n");
88+
89+
System.out.println("Set cost: ");
90+
int cost = scanner.nextInt();
91+
advancedClock.setCost(cost);
92+
System.out.println("Get cost: " + advancedClock.getCost() + "\n");
93+
94+
System.out.println("Set hours: ");
95+
int hours = scanner.nextInt();
96+
advancedClock.setHours(hours);
97+
System.out.println("Get hours: " + advancedClock.getHours() + "\n");
98+
99+
System.out.println("Set minutes: ");
100+
int minutes = scanner.nextInt();
101+
advancedClock.setMinutes(minutes);
102+
System.out.println("Get minutes: " + advancedClock.getMinutes() + "\n");
103+
104+
System.out.println("Set seconds: ");
105+
int seconds = scanner.nextInt();
106+
advancedClock.setSeconds(seconds);
107+
System.out.println("Get seconds: " + advancedClock.getSeconds() + "\n");
108+
109+
System.out.println("New information:");
110+
advancedClock.printInformation();
111+
}
112+
}
Binary file not shown.
3.1 KB
Binary file not shown.
4.07 KB
Binary file not shown.

0 commit comments

Comments
 (0)