THINK FIRST·CODE LATER

← All labs

MyInteger: the same question, asked two ways

Problem

Write a class MyInteger that wraps one int value, and a public class MyIntegerApp. The lab is about the difference between instance and static methods:

instance (about this object) static (about the argument)
isEven(), isOdd(), isPrime() isEven(int), isOdd(int), isPrime(int)
equals(int), equals(MyInteger) isEven(MyInteger), isPrime(MyInteger)
getValue() parseInt(char[]), parseInt(String)

parseInt converts digits to a number itself (do not call Integer.parseInt). The instance methods must reuse the static ones.

main reads numbers as text until the end of the input, converts each with MyInteger.parseInt, and describes it: even or odd; prime; twin prime (the number and the number + 2 are both prime); same as the previous number; and "the answer" when it equals 42. For the input

7 8 8 42 1 11 29

the program prints

7: odd, prime
8: even
8: even, same as the previous number
42: even, the answer
1: odd
11: odd, prime, twin prime with 13
29: odd, prime, twin prime with 31

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l12-6-data-1.txt

7 8 8 42 1 11 29
What your program must print for l12-6-data-1.txt
7: odd, prime
8: even
8: even, same as the previous number
42: even, the answer
1: odd
11: odd, prime, twin prime with 13
29: odd, prime, twin prime with 31

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l12-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l12-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.