Академический Документы
Профессиональный Документы
Культура Документы
JAIME R. C.
1 Regular expressions
Define a regular expression that matches the phrases “damage”, “minor damages” and
“heavy damage” but not the phrase “no damages”.
Write a method with a string as argument. The output has to be the result whether the
string matches or not.
package _1_regular_expressions;
import java.util.regex.Pattern;
return matchResult;
}
}
}
2 Objects
Define a class Student with the reference variables studentId and name, a
constructor and default methods. It is given:
ID Name
0054 Albert Einstein
1234 Gottfried Wilhelm Leibniz
5421 Carl Friedrich Gauss
Write code for a console output (Name and ID) that is sorted by the student names.
package _2_objects;
import java.util.Comparator;
@Override
public String toString() {
return "Student [studentId=" + studentId + ", name=" + name +
"]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 :
name.hashCode());
result = prime * result + studentId;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (studentId != other.studentId)
return false;
return true;
}
}
package _2_objects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// BEFORE SORTING
for (Student student : studentList) {
System.out.println(student.toString());
}
System.out.println("------------------");
// SORTING BY COMPARATOR
Arrays.sort(students, Student.stComparator);
}
3 Design pattern
What is a singleton pattern and when do you use it? Please use an example.
A: Is an object that can be instanced only once by setting its constructor as private and
providing a public method to manage the instance…
They are especially useful for factory classes, but lately they are considered as an anti-
pattern, not to be used.
When I was learning Java, I used Singleton to simulate Data Bases.
package _3_design_pattern;
public Clothing(String name, String desc, double price, int size, String material) {
super(name, desc, price);
this.size = size;
this.material = material;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + " _ " + size + " _ " + material + ".";
}
}
5 Threads
Imagine a simple robot that can randomly move forward and backward as well as left
and right.
The task is to show the movement of the robot in the form of a console output (e.g.,
System.out.println(“left...”), System.out.println(“right...”), ...).
Define two classes HorizontalThread and VerticalThread as threads. The class
HorizontalThread process the robots movement left and right, the class
VerticalThread process the robots movement forward and backward. The
movements have to be displayed in the console. Now, implement a class Robot that
starts both threads.
package _5_threads;
import java.util.Random;
@Override
public void run() {
System.out.println("Horizontally moving");
try {
if (new Random().nextBoolean()) {
System.out.println("left...");
} else {
System.out.println("right...");
}
Thread.sleep(300);
} catch (InterruptedException e) {
System.err.println("Horizontal move interrupted");
} finally {
System.out.println("Horizontal move ended.");
}
}
@Override
public synchronized void start() {
}
}
package _5_threads;
import java.util.Random;
@Override
public void run() {
System.out.println("Vertically moving");
try {
if (new Random().nextBoolean()) {
System.out.println("forward...");
} else {
System.out.println("backward...");
}
Thread.sleep(300);
} catch (InterruptedException e) {
System.err.println("Vertical move interrupted");
} finally {
System.out.println("Vertical move ended.");
}
}
@Override
public synchronized void start() {
@Override
public void run() {
robotHorizontal.start();
robotVertical.start();
}
6 Correct the code
1: public class carThread implements Thread {
2: final String brand;
3: final String model;
4: final double price;
5:
6: public carThread ( String brand, String model) {
7: this.brand = brand;
8: this.model = model;
9: }
10:
11: public void run() {
12: while(true) {
13: System.out.println(“hello my name is this.brand”);
14: Thread.sleep(300);
15: }
16: }
17:
18: public static void main(String[] args) {
19: new carThread(“Audi”).run();
20: new carThread(“BMW”).run();
21: System.out.println(“carThreads are running... “);
22: }
23:
What's wrong with this class? Correct all errors.
First, I would never write code on default package, second, java classes by convention
are named by CamelCase. Then the code could be this:
package _6_correct_the_code;
e.printStackTrace();
} finally {
flag = false;
}
}
}
package _7_bonus;
import java.util.Random;
System.out.println(Bonus.trinary(true));
System.out.println(Bonus.trinary(false));