Java programming exercises for newcomers unpopular with active engineers

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.

Premise

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.

Challenge. 1

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.

Challenge. 2

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."

Challenge. 3

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 is one line (excluding line breaks) that is output as standard by the "practice" method of "exam.Exam1" and "exam.Exam2" created in "Issue.1, Issue.2". Make it an element. --For the argument n, if there is no standard output line in the "practice" method, an empty list is returned.

java:exam.FizzBuzz.java


    package exam;
//    import … (abridgement);
    public interface FizzBuzz {
        public List<String> fizzBuzz(final int n);
    }

Challenge. 4

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".

Challenge.5-1

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"; }
    }

Challenge .5-2

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
        }
    }

Execution example:

        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

Java programming exercises for newcomers unpopular with active engineers
Getting Started with Ruby for Java Engineers
C # cheat sheet for Java engineers
Enable OpenCV with java8. (For myself)
Practice of Java programming basics-I want to display triangles with for statements ①
Practice of Java programming basics-I want to display triangles with for statements ②
Learn Java with "So What" [For beginners]
Java Programming Style Guide for the Java 11 Era
Interoperability tips with Kotlin for Java developers
[Java] How to test for null with JUnit
Links & memos for getting started with Java (for myself)
Ask for n business days later with JAVA
Simple obstacle racing made with processing for Java
For Java engineers starting Kotlin from now on
Docker Container Operations with Docker-Client API for Java
An introduction to Groovy for tedious Java engineers
Programming with direct sum types in Java (Neta)