CSc 2310 Principles of Computer Programming I
Spring 1999
Programming Assignment #3
Due: 3 March 1999 (Wednesday)
For example, if the two numbers were 2367 and 1327, we have 2 BULLS (for the exact matches for digits 3 and 7) and 1 COW (for the remaining common digit 2 which is in different positions in the two numbers). If the numbers were 9852 and 8926, we have 0 BULLS and 3 COWS and if the numbers were 1234 and 4321, we have 0 BULLS and 4 COWS. It is quite clear that if the two numbers are the same, we have 4 BULLS and 0 COWS.
You need to write a Java program, called bulls.java, that prompts for and reads two numbers (between 1000 and 9999) from the user. If any one of the two numbers has repeating digits, the program prints a message to that effect and stops. If both the numbers do not have repeating digits, the program calculates the number of BULLS and COWS for these two numbers and prints them out.
It will be useful to break this problem into the following methods:
The main method will consist of reading two numbers from the user input, checking if they both have no repeating digits, and calling the methods to calculate BULLS and COWS if the numbers do not have repeating digits. In case any one or both of the numbers have repeating digits, the main program will write a message to that effect and stop.
The following are three sample runs of the program.
A:> java bulls Please enter first number (between 1000 and 9999): 1676 Please enter second number (between 1000 and 9999): 2344 1676 has repeating digits. 2344 has repeating digits. A:> java bulls Please enter first number (between 1000 and 9999): 2367 Please enter second number (between 1000 and 9999): 1327 BULLS = 2 and COWS = 1 A:> java bulls Please enter first number (between 1000 and 9999): 1678 Please enter second number (between 1000 and 9999): 2344 2344 has repeating digits.