Skip to content

Commit eb33ec6

Browse files
committed
cohort 42.1 hw #17, lesson #18
1 parent 877f6e9 commit eb33ec6

5 files changed

Lines changed: 151 additions & 2 deletions

File tree

BerlinAIT/Cohort42.1/src/extend/Interpreter.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package extend;
22

3-
import java.util.Arrays;
43
import java.util.Scanner;
54

65
/**
76
* AIT-TR, cohort 42.1, Java Basic, hw #10 ext
87
*
98
* @author Sergey Iryupin
10-
* @version 8-Feb-24
9+
* @version 8,23-Feb-24
1110
*/
1211
public class Interpreter {
1312

@@ -37,6 +36,10 @@ static void assignValue(String line) {
3736
String[] tokens = line.split("=");
3837
String varName = tokens[0].trim();
3938
String varValue = tokens[1].trim();
39+
// check variable name
40+
if (!validateVarName(varName)) {
41+
return;
42+
}
4043
// transform 'a' -> 0
4144
int idx = varName.charAt(0) - 'a';
4245
// transform "123" -> 123
@@ -46,7 +49,27 @@ static void assignValue(String line) {
4649
}
4750

4851
static void printValue(String varName) {
52+
// check variable name
53+
if (!validateVarName(varName)) {
54+
return;
55+
}
56+
// transform 'a' -> 0
4957
int idx = varName.charAt(0) - 'a';
58+
// print value of variable
5059
System.out.println(values[idx]);
5160
}
61+
62+
static boolean validateVarName(String varName) {
63+
// check variable name length
64+
if (varName.length() > 1) {
65+
System.out.println("Error: variable name is too long");
66+
return false;
67+
}
68+
if (varName.isEmpty()) {
69+
System.out.println("Error: variable name is empty");
70+
return false;
71+
}
72+
// TODO check if variable name in 'a'..'z'
73+
return true;
74+
}
5275
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package lesson17.homework;
2+
3+
public class Employee {
4+
private String name;
5+
private String position;
6+
private String email;
7+
private String phone;
8+
private int salary;
9+
private int age;
10+
11+
public Employee(String name, String position, String email, String phone, int salary, int age) {
12+
this.name = name;
13+
this.position = position;
14+
this.email = email;
15+
this.phone = phone;
16+
this.salary = salary;
17+
this.age = age;
18+
}
19+
20+
public int getAge() {
21+
return age;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Employee{" +
27+
"name='" + name + '\'' +
28+
", position='" + position + '\'' +
29+
", email='" + email + '\'' +
30+
", phone='" + phone + '\'' +
31+
", salary=" + salary +
32+
", age=" + age +
33+
'}';
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package lesson17.homework;
2+
3+
/**
4+
* AIT-TR, cohort 42.1, Java Basic, hw #17
5+
*
6+
* @author Sergey Iryupin
7+
* @version 23-Feb-24
8+
*/
9+
public class HomeWork17 {
10+
public static void main(String[] args) {
11+
Employee[] employees = {
12+
new Employee("a1", "d1", "e1", "t1", 1000, 40),
13+
new Employee("a2", "d2", "e2", "t2", 1200, 43),
14+
new Employee("a3", "d3", "e3", "t3", 800, 37)
15+
};
16+
for (Employee employee : employees) {
17+
if (employee.getAge() > 40) {
18+
System.out.println(employee);
19+
}
20+
}
21+
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package lesson18;
2+
3+
/**
4+
* AIT-TR, cohort 42.1, Java Basic, Lesson #18
5+
*
6+
* @version 23-Feb-24
7+
*/
8+
public class Lesson18 {
9+
public static void main(String[] args) {
10+
final int a;
11+
a = 10;
12+
//a = 5 + 1; compile error
13+
14+
System.out.println(Robot.COMPANY_NAME);
15+
16+
Robot robot = new Robot(8);
17+
System.out.println(robot);
18+
System.out.println(Robot.getCount());
19+
System.out.println(Robot.add(2, 2));
20+
robot.addAndSave(5);
21+
robot.addAndSave(12);
22+
System.out.println(robot.getSum());
23+
24+
Robot r2d2 = new Robot(2);
25+
System.out.println(r2d2);
26+
System.out.println(Robot.getCount());
27+
System.out.println(r2d2.getSum());
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package lesson18;
2+
3+
public class Robot {
4+
5+
public static final String COMPANY_NAME = "Boston Dynamics";
6+
7+
private int countCPU;
8+
private int sum;
9+
10+
private static int count;
11+
12+
public Robot(int countCPU) {
13+
this.countCPU = countCPU;
14+
this.sum = 0;
15+
count++;
16+
}
17+
18+
public static int getCount() {
19+
return count;
20+
}
21+
22+
public static int add(int a, int b) {
23+
return a + b;
24+
}
25+
26+
public void addAndSave(int a) {
27+
sum += a;
28+
}
29+
30+
public int getSum() {
31+
return sum;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "Robot{" +
37+
"countCPU=" + countCPU +
38+
'}';
39+
}
40+
}

0 commit comments

Comments
 (0)