CSc 2310 Principles of Computer Programming I
Spring 1999
Programming Assignment #3
Due: 3 March 1999 (Wednesday)


  1. This is another art program. Write a Java program, called forest.java, which draws 2 rainbows, one sun and a forest of 5 different sized trees. So that you do not duplicate code and also to improve the clarity of your program, you must develop the following three methods: The main method will draw the scene containing the sun, 2 rainbows and 5 different sized trees by invoking the above mentioned methods appropriately.

  2. Consider numbers in the range 1000 and 9999 which do not have repeating digits. Given two such numbers, we define the number of BULLS and COWS for the pair of numbers as follows:
    BULLS = number of digits that appear in the same positions in the two numbers.
    COWS = number of digits that appear in both the numbers, but at different positions.

    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.


Raj Sunderraman
Sat Feb 20 14:12:49 EST 1999