forked from 79165815727/java_test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1
More file actions
126 lines (115 loc) · 4.13 KB
/
Copy pathtest1
File metadata and controls
126 lines (115 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package pro.sky.java.course1.homework6;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Scanner;
public class Homework6 {
public static void checkYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.printf("%d is a leap year\n", year);
} else {
System.out.printf("%d is not a leap year\n", year);
}
}
public static void checkOS(int deviceYear, int clientOS) {
int currentYear = LocalDate.now().getYear();
if (clientOS == 1) {
if (deviceYear < currentYear) {
System.out.println("Install the lite-version for iOS at the link");
} else if (deviceYear == currentYear) {
System.out.println("Install the full-version for iOS at the link");
} else {
System.out.println("Incorrect input!");
}
} else if (clientOS == 0) {
if (deviceYear < currentYear) {
System.out.println("Install the lite-version for Android at the link");
} else if (deviceYear == currentYear) {
System.out.println("Install the full-version for Android at the link");
} else {
System.out.println("Incorrect input!");
}
} else {
System.out.println("Incorrect input!");
}
}
public static int calcDeliveryTime(int deliveryDistance) {
int deliveryTime = 1;
if (deliveryDistance > 20) {
deliveryTime++;
}
if (deliveryDistance > 60) {
deliveryTime++;
}
if (deliveryDistance <= 0) {
System.out.println("Incorrect input!");
return 0;
}
return deliveryTime;
}
public static void checkDoubles(String str) {
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1)) {
System.out.printf("Duplicate character '%c' found.\n", str.charAt(i));
return;
}
}
System.out.println("Duplicate characters not found.");
}
public static void reverseArrayOrder(int[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
int t = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = t;
}
}
public static int[] generateRandomArray() {
java.util.Random random = new java.util.Random();
int[] arr = new int[30];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(100_000) + 100_000;
}
return arr;
}
public static double getAverage(int[] arr) {
double sum = getSum(arr);
double averageValue = sum / arr.length;
return averageValue;
}
public static double getSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
//task1
Scanner input = new Scanner(System.in);
System.out.print("Enter the year: ");
int y = input.nextInt();
checkYear(y);
//task2
System.out.print("Enter the year of manufacture of the device: ");
int deviceYear = input.nextInt();
System.out.print("Enter 0 if you have Android OS or 1 if iOS: ");
int deviceOS = input.nextInt();
checkOS(deviceYear, deviceOS);
//task3
System.out.print("Enter the distance: ");
int dist = input.nextInt();
System.out.printf("Delivery will take %d days.\n", calcDeliveryTime(dist));
//task4
System.out.print("Enter some line: ");
String s = input.next();
checkDoubles(s);
input.close();
//task5
int[] array = {7, 4, 5, 8, 1};
reverseArrayOrder(array);
System.out.println("The reversed array looks like this: " + Arrays.toString(array));
//task6
int[] arr = generateRandomArray();
double averageArr = getAverage(arr);
System.out.printf("The average expenditure was %.2f rubles. \n", averageArr);
}
}