Skip to content

Commit bae4e31

Browse files
committed
test task
0 parents  commit bae4e31

10 files changed

+357
-0
lines changed

MySQL8_ruslan.docx

112 KB
Binary file not shown.

full description of the task.JPG

176 KB
Loading

java16_ruslan/Task1.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Task1 {
2+
3+
private static String p;
4+
5+
static boolean reg(String S) { // just input control
6+
java.util.regex.Pattern p = java.util.regex.Pattern.compile("[0-9]+");
7+
java.util.regex.Matcher m = p.matcher(S);
8+
return m.matches();
9+
}
10+
11+
public static String method1(int n) { //via Sieve of Eratosthenes algorithm, O(N*log(log(N)))
12+
boolean[] prime = new boolean[n + 1];
13+
java.util.Arrays.fill(prime, true);
14+
prime[1] = false;
15+
int sum = 1;
16+
for (int i = 2; i * i <= n; i++)
17+
if (prime[i])
18+
for (int j = i * i; j <= n; j += i) // before sqrt
19+
if (prime[j]) {
20+
sum += 1;
21+
prime[j] = false;
22+
}; //subtract the number of non-primes from the total number of numbers:
23+
return (n - sum) + ", i. e. " + java.lang.Math.round(100.0 * (n - sum) / n) + "%";
24+
}
25+
26+
public static String method2(int n) { //via direct calculation, O(N^2). Just to compare and test the results
27+
int temporary = 0, sum = 0;
28+
for (int j = 2; j <= n; j++) {
29+
for (int i = 2; i <= java.lang.Math.ceil(j / 2.0); i++)
30+
if ((temporary = j % i) == 0) break;
31+
sum += (temporary == 0) ? (0) : (1);
32+
}
33+
return ((n > 2) ? (sum + 1) : 1) + ", i. e. " + java.lang.Math.round(100.0 * (sum + 1) / n) + "%";
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println("Input natural number N and press Enter:");
38+
while (true) {
39+
p = (new java.util.Scanner(System.in)).next();
40+
if (reg(p)) {
41+
break;
42+
} else {
43+
System.out.println("Input only digits:");
44+
}
45+
}
46+
int n = Integer.valueOf(p);
47+
System.out.println("The number of primes in the interval (0,N]:");
48+
System.out.print((n <= 1) ? "0, 0%" : method1(n));
49+
}
50+
}

java16_ruslan/inputForTask2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Belarus Minsk 2 Minsk 3 Minsk Minsk Minsk 10000 Borisov Borisov 5003 Molodechno Molodechno 1020 Vitebsk 3 Vitebsk Vitebsk Vitebsk 20000 Polotsk Polotsk 8100 Baranovichi Baranovichi 5000

java16_ruslan/task2/City.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package task2;
2+
3+
public class City {
4+
public String name;
5+
public City(String name) {
6+
this.name = name;
7+
}
8+
public City() {}
9+
public void setName(String name) {
10+
this.name = name;
11+
}
12+
public String getName() {
13+
return name;
14+
}
15+
@Override public String toString() {
16+
return name;
17+
}
18+
}

java16_ruslan/task2/Country.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package task2;
2+
3+
public class Country extends Region {
4+
public City Capital;
5+
public int numberr;
6+
public Region[] Regions = new Region[numberr + 5];
7+
8+
public Country() {}
9+
10+
public Country(String name, City capitalCity, int numberr, Region[] regions) {
11+
super(name, capitalCity);
12+
this.numberr = numberr;
13+
Regions = regions;
14+
}
15+
16+
public City getCapital() {
17+
return Capital;
18+
}
19+
20+
public void setNumberr(int numberr) {
21+
this.numberr = numberr;
22+
}
23+
24+
public void setCapital(City capital) {
25+
Capital = capital;
26+
}
27+
28+
public void setRegions(Region[] regions) {
29+
Regions = regions;
30+
}
31+
32+
public int getNumberr() {
33+
return numberr;
34+
}
35+
36+
public void getRegionCenters() {
37+
for (int i = 0; i < numberr; i++)
38+
System.out.println("\n"+Regions[i].getCapitalCity());
39+
}
40+
public void getRegionCount() {
41+
System.out.println("\nAnd the total number of regions is " + numberr +"\n*************************************");
42+
}
43+
44+
@Override
45+
public double CalculateSquare() {
46+
double s = 0;
47+
for (int i = 0; i < numberr; i++)
48+
s += Regions[i].CalculateSquare();
49+
return s;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
int result = super.hashCode();
55+
result = 29 * result + (name != null ? name.hashCode() : 0);
56+
return result;
57+
}
58+
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (this == obj) return true;
62+
if (obj == null || getClass() != obj.getClass()) return false;
63+
//if (!super.equals(obj)) return false;
64+
Country country = (Country) obj;
65+
return !(name != null ? !name.equals(country.name) : country.name != null);
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Country{" +
71+
"name='" + name + '\'' +
72+
", CapitalCity='" + CapitalCity + '\'' +
73+
", square=" + square +
74+
'}';
75+
}
76+
}

java16_ruslan/task2/District.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package task2;
2+
3+
public class District {
4+
public String name;
5+
public City CapitalCity;
6+
public double square;
7+
8+
public class SquareLogicException extends Exception {
9+
public SquareLogicException() {}
10+
public SquareLogicException(String message, Throwable exception) {
11+
super(message, exception);
12+
}
13+
public SquareLogicException(String message) {
14+
super(message);
15+
}
16+
public SquareLogicException(Throwable exception) {
17+
super(exception);
18+
}
19+
}
20+
21+
public District() {}
22+
23+
public District(String name, City capitalCity) {
24+
this.name = name;
25+
CapitalCity = capitalCity;
26+
}
27+
28+
public District(String name, City capitalCity, double square) throws SquareLogicException {
29+
if (square <= 0) {
30+
throw new SquareLogicException("incorrect square");
31+
}
32+
this.name = name;
33+
CapitalCity = capitalCity;
34+
this.square = square;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public City getCapitalCity() {
42+
return CapitalCity;
43+
}
44+
45+
public double getSquare() {
46+
return square;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public void setCapitalCity(City capitalCity) {
54+
CapitalCity = capitalCity;
55+
}
56+
57+
public void setSquare(double square) {
58+
this.square = square;
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "District{" +
64+
"name='" + name + '\'' +
65+
", CapitalCity='" + CapitalCity + '\'' +
66+
", square=" + square +
67+
'}';
68+
}
69+
70+
public double CalculateSquare() {
71+
return square;
72+
}
73+
}

java16_ruslan/task2/Region.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package task2;
2+
3+
public class Region extends District {
4+
5+
public int numberd;
6+
public District[] Districts = new District[numberd + 10];
7+
8+
public Region() {}
9+
10+
public Region(String name, City capitalCity) {
11+
super(name, capitalCity);
12+
}
13+
14+
public Region(String name, City capitalCity, int number, District[] districts) {
15+
super(name, capitalCity);
16+
this.numberd = number;
17+
Districts = districts;
18+
}
19+
20+
@Override
21+
public double CalculateSquare() {
22+
double S = 0;
23+
for (int i = 0; i < numberd; i++)
24+
S += Districts[i].CalculateSquare();
25+
return S;
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
int result = super.hashCode();
31+
result = 29 * result + (name != null ? name.hashCode() : 0);
32+
return result;
33+
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (this == obj) return true;
38+
if (obj == null || getClass() != obj.getClass()) return false;
39+
//if (!super.equals(obj)) return false;
40+
Region region = (Region) obj;
41+
return !(name != null ? !name.equals(region.name) : region.name != null);
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "Region{" +
47+
"name='" + name + '\'' +
48+
", CapitalCity='" + CapitalCity + '\'' +
49+
", square=" + square +
50+
'}';
51+
}
52+
}
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package task2;
2+
import java.io.*;
3+
import java.util.*;
4+
public class RunTheProgram {
5+
private static String p;
6+
static boolean reg(String S) { // just input control
7+
java.util.regex.Pattern p = java.util.regex.Pattern.compile("[1-5]");
8+
java.util.regex.Matcher m = p.matcher(S);
9+
return m.matches();
10+
}
11+
12+
public static void main(String[] args) throws District.SquareLogicException {
13+
try {
14+
Scanner in = new Scanner(new File("inputForTask2.txt"));
15+
Country country = new Country();
16+
String name = in .next();
17+
String capital = in .next();
18+
City capital0 = new City(capital);
19+
int numberr = in .nextInt();
20+
Region[] regions = new Region[numberr + 5];
21+
for (int i = 0; i < numberr; i++) {
22+
String namei = in .next();
23+
int numberdi = in .nextInt();
24+
String capitali = in .next();
25+
City capitali0 = new City(capitali);
26+
District[] districts = new District[numberdi + 5];
27+
for (int j = 0; j < numberdi; j++) {
28+
String nameij = in .next();
29+
String capitalij = in .next();
30+
City capitalij0 = new City(capitalij);
31+
double squareij = in .nextDouble();
32+
districts[j] = new District(nameij, capitalij0, squareij);
33+
}
34+
regions[i] = new Region(namei, capitali0, numberdi, districts);
35+
}
36+
country.setName(name);
37+
country.setCapital(capital0);
38+
country.setNumberr(numberr);
39+
country.setRegions(regions);
40+
//country.getName();
41+
int memory1 = 9, choice = -7;
42+
try {
43+
while (choice != 0) {
44+
System.out.print("\nSelect an option (method) and then press Enter:\n1. Show this capital\n2. Show this regions' centers and the total number of regions\n3. Compute this square\n4. Exit!\n");
45+
int[] mem1 = new int[java.lang.Math.abs(memory1)];
46+
memory1 *= (choice == 5) ? 100 : 1;
47+
if (choice != 5) // in console select option 5 just for testing out of memory
48+
while (true) { // just input control
49+
p = (new java.util.Scanner(System.in)).next();
50+
if (reg(p)) {
51+
choice = Integer.valueOf(p);
52+
break;
53+
} else {
54+
System.out.println("Enter only one natural number between 1 and 5:");
55+
}
56+
}
57+
switch (choice) {
58+
case 1:
59+
System.out.println("\n"+country.getCapital()+"\n*************************************");
60+
break;
61+
case 2:
62+
country.getRegionCenters();
63+
country.getRegionCount();
64+
break;
65+
case 3:
66+
System.out.println("\n"+country.CalculateSquare()+"\n*************************************");
67+
break;
68+
case 4:
69+
System.exit(0);
70+
break;
71+
default:
72+
break;
73+
}
74+
}
75+
} catch (OutOfMemoryError e) {
76+
System.out.println("out of memory");
77+
}
78+
} catch (FileNotFoundException e) {
79+
System.out.println("cannot open the file inputForTask2.txt");
80+
} catch (District.SquareLogicException e) {
81+
System.out.println("square is negative or incorrectly defined");
82+
}
83+
}
84+
}

java16_ruslan/task2_readme.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
open bash in the directory of this .txt file
2+
javac task2/RunTheProgram.java
3+
java task2.RunTheProgram

0 commit comments

Comments
 (0)