CSCI 201: Intro to Programming (Java)

Midterm Exam I .   Due: Mon, Oct 23

The tasks have to be done strictly individually. You are not allowed to discuss the problems with anybody.
The first problem must be completed in class today.
Solutions to the other two problems must be submitted before 11:59pm of the day above.

  1. in-class part (30 points)   Design a Java application that takes a positive integer number n ≥ 2 from the user and checks whether or not it can be represented as a sum of two integer powers of 3 (powers of 3 are numbers of the form 3k with k ≥ 0 being an integer, such as 9 = 32 for k = 2 and 27 = 33 for k = 3). If such representation is possible, it must be printed out. Otherwise, the string "representation is impossible" must be printed.

    For example, if n = 12, then it can be represented as the sum 3 + 9 with both 3 and 9 being powers of 3, so your program must output 12 = 3 + 9. Also, if n = 10 is given, your program must output 10 = 1 + 9 (here 1 = 30). Your code must work for any given n, not just for the above examples.

    Implement the following method boolean isPowerOf3(int m) in your code to check if a given number m is a power of 3. If this is the case, the return value is true and false otherwise. Within this method divide the number several times by 3 till the quotient reaches 1 and check the remainder. The remainder must be zero in all steps.

    For example: if n = 27 then

    27 / 3 = 9 remainder = 0
    9 / 3 = 3 remainder = 0
    3 / 3 = 1 remainder = 0
    You stop here as the quotient reached 1.
    Since all remainders are 0, the number 27 is a power of 3.

    Another example: if n = 18 then

    18 / 3 = 6 remainder = 0
    6 / 3 = 2 remainder = 0
    2 / 3 = 0 remainder = 1
    You can stop here as the obtained remainder is not 0.
    Hence, 18 is not a power of 3.

    Your project class has to have 2 methods: main() (that takes the user input and prints the result) and isPowerOf3() to be called from main().

    Name the project Ex1_1
    Solution

  2. take-home part (35 points)   Design a Java application for solving a system of two linear equations with two unknowns:
    a·x + b·y = c
    d·x + e·y = f

    where a,b,c,d,e,f are given integer numbers and x,y are unknowns (real numbers). The solutions to this system are given by the following formulas:

    x = (c·e - b·f) / (a·e - b·d)
    y = (a·f - c·d) / (a·e - b·d)

    Your design has to have two classes: Ex1_2 and LinearSystem. The class LinearSystem should have a constructor accepting 6 parameters corresponding to the coefficients of the linear system. It has to have two public methods computeX() and computeY() for computing x and y, respectively. It also has to have a method isUnique() returning true or false depending on whether a unique solution exists (i.e., the denominators of the above formulas are non-zero). The coefficients of the system should be private instance variables.

    The class Ex1_2 must ask the user to enter 6 real numbers a,b,c,d,e,f from the keyboard, check whether a unique solution exists and if it does, output x and y. If solution is not unique (that is, if a=k·d, b=k·e, and c=k·f for some non-zero k), one of solutions must be output. Otherwise, if no solution exists, the corresponding message must be printed.

    Name the project Ex1_2
    Solution

  3. take-home part (35 points)   Write a Java application that asks a user for two integer numbers n and k (n in the range [3..10] and k in the range [0..n]) and passes them to custom method printSequences() which outputs to the screen all binary sequences of length n with exactly k ones, one sequence per line.
    For example, if n=5 and k=2, your program should produce the following output:
    00011
    00101
    00110
    01001
    01010
    01100
    10001
    10010
    10100
    11000
    

    10 extra points   for recursive implementation of method printSequences()

    Name the project Ex1_3

    Solution