When I show it to an active engineer, he usually looks awkward and says, "I think it's difficult for newcomers." It seems that words and expressions may be difficult.
This was created as an educational exercise for newcomers who (think) understand the basics of Java. Since it is for educational purposes, it is not assumed that you can answer everything from the beginning. The time limit is appropriate. (People who can answer all without difficulty will teach as appropriate while leaving one function) I also want to find out if I can understand words I don't know, so I dare to use words that I probably don't know. This is to give guidance as needed while looking at the approach when answering, points to be blocked, how to make mistakes, etc., and to obtain reference information for deciding future educational policies.
Create a class "exam.Exam1" with an instance method "practice (final String n)" that behaves as follows.
--If the character string n is not a number greater than or equal to 1, the character string "error" is output to "standard error output" and processing is interrupted. --Count from 1 to n and output the numbers to "standard output" separated by line breaks. --When it is a multiple of 3, "Fizz" is output in one line instead of a number. --When it is a multiple of 5, "Buzz" is output in one line instead of a number. --If it is a multiple of 3 and a multiple of 5, "FizzBuzz" should be output in one line. --List the numbers with 3 separated by commas (,), add "are contains" 3 "." And output to the contains3.txt file.
Create a class "exam.Exam2" that is a modification of the class "Exam1" created in "Issue.1" to meet the following conditions.
--If the string n is not a number greater than or equal to 1, the exception "IllegalArgumentException" is raised. --When it is a prime number, "Fizz" is output in one line instead of a number. --When it is a multiple of 3, "Buzz" is output in one line instead of a number. --If it is a multiple of 3 and a prime number, "FizzBuzz" should be output. --List the prime numbers separated by commas (,) and output to a .txt file with "are the numbers of primes."
Implement the interface "exam.FizzBuzz" as shown in the following code, and create "exam3.Exam1" and "exam3.Exam2" so that the following conditions are met.
--The return value List
java:exam.FizzBuzz.java
package exam;
// import … (abridgement);
public interface FizzBuzz {
public List<String> fizzBuzz(final int n);
}
Create an abstract class "exam.AbstractFizzBuzz" that satisfies the following conditions, and "exam4.Exam1" and "exam4.Exam2" that inherit it.
--The abstract class "exam.AbstractFizzBuzz" should implement the "exam.FizzBuzz" interface of "Issue.3". --The "fizzBuzz" method of the interface "exam.FizzBuzz" shall be implemented by "exam.AbstractFizzBuzz", and Override is prohibited in "exam4.Exam1" and "exam4.Exam2".
Create "exam5.Exam1" and "exam5.Exam2" that inherit the abstract class "exam.AbstractFizzBuzz" as shown in the following code. However, the conditions for isFizz and isBuzz are the same as the conditions implemented in "Issue.1, Issue.2".
java:exam.FizzBuzzStrategy.java
package exam;
// import … (abridgement);
public interface FizzBuzzStrategy {
boolean isFizz(final int num); //If it becomes Fizz true
boolean isBuzz(final int num); //If it becomes Buzz true
String fizz(); //Returns Fizz
String buzz(); //Returns Buzz
String both(); //Returns FizzBuzz
}
java:exam.AbstractFizzBuzz.java
package exam;
// import … (abridgement);
public abstract class AbstractFizzBuzz implements FizzBuzzStrategy {
@Override
String fizz() { return "Fizz"; }
@Override
String buzz() { return "Buzz"; }
@Override
String both() { return "FizzBuzz"; }
}
Consider an "exam.FizzBuzzRunner" as shown in the following code. Please implement the run method so that the execution result will be as shown in the "Execution example" below. If necessary, you can change the class created in "Issue.5-1".
java:exam.FizzBuzzRunner.java
package exam;
// import … (abridgement);
public class FizzBuzzRunner {
public static void main(String[] args) {
final FizzBuzzRunner runner = new FizzBuzzRunner();
final List<String> exam1FizzBuzz = runner.run(21, new exam5.Exam1());
print(exam1FizzBuzz);
System.out.println("----------");
final List<String> exam2FizzBuzz = runner.run(16, new exam5.Exam2());
print(exam2FizzBuzz);
}
private static void print(final List<String> src) {
for (String s : src) {
System.out.println(s);
}
}
public List<String> run(final int n, final FizzBuzzStrategy strategy) {
//* Implemented here
}
}
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
----------
1
Fizz
FizzBuzz
4
Fizz
Buzz
Fizz
8
Buzz
10
Fizz
Buzz
Fizz
14
Buzz
16
17
Buzz
Fizz
20
Buzz
Recommended Posts