CS335
Fall, 2008
Homework 2 (20 points)
Due: 9/16/08 (Tuesday)

**********************************************************************

1.  The "main" method of a class has always to be a "static" method.
    Why? What would happen if you define the "main" method as a
    non-static method? (4 points)

Solution:
    The main method must be declared static because it is being called on
    the class, not on a specific instance. Therefore, it must exist even
    before an instance of the class is created.
    If the main method is not defined as static, the JVM will not be able
    to execute it (since it does not exist), even though it will compile.


2.  (a) What is a "constructor"? Constructors can be overloaded.
        Why do you think it is necessary to have overloaded
        constructors? give me at least one reason. (2 points)

    (b) A constructor can not have return type or return value.
        Why? (2 points)
    
Solution:
    (a) A "constructor" is a method that has the same name as the class.
        The method executes automatically to do initialization work when
        an object of the class is created.
        The reason to have overloaded Constructors is to make the object
        creation process more flexible (i.e., giving users more flexibility
        in creating an object).

    (b) A constructor returns the constructed object itself so there is
        no need to indicate a return value or type.


3.  A user outside an object can accesse/mutate a private instance
    variable through its public methods. Does this mean there is no
    need to have private instance variables in a class? Why?
    (4 points)

Solution:
    No.
    Because a user's capability to access/mutate a private variable
    can be restricted/controlled by the public methods through some
    especially designed mechanism while one has no control on a user's
    capability to access/mutate a public variable at all.


4.  Write a Java program to perform the following task:

    The program maintains six digits on the same line of the
    screen, separated by space.
    Each digit is set to zero initially.
    If the user pushes the number i (1 <= i <= 6) key of the keyboard,
    the program changes the i-th digit by adding 1 to that digit.
    The digit 9 should change to 0.  When the six digits are set to
    display the digit sequence ``281562'', print out the message "Hey,
    Dude, welcome to java club."  All other combinations should print
    the message "xxxxxx is not the right sequence", where "xxxxxx" is
    the current digit sequence. (8 points)

Solution:

// h2n4.java
// Sample code for problem #4
import java.io.*;
import java.util.*;

public class h2n4 {
    private static int digit1 = 0;
    private static int digit2 = 0;
    private static int digit3 = 0;
    private static int digit4 = 0;
    private static int digit5 = 0;
    private static int digit6 = 0;
        
    public static int getDigit1()
    {    return digit1; }

    public static int getDigit2()
    {    return digit2; } 
    
    public static int getDigit3()
    {    return digit3; }
    
    public static int getDigit4()
    {    return digit4; }

    public static int getDigit5()
    {    return digit5; }
    
    public static int getDigit6()
    {    return digit6; }

    public static void printCode()
    {    System.out.print(getDigit1() + " " + getDigit2() +
                         " " + getDigit3() + " " + getDigit4() +
                         " " + getDigit5() + " " + getDigit6());
    }
    
    public static boolean checkCode()
    {   printCode();
        if (digit1 == 2 && digit2 == 8 && digit3 == 1 && digit4 == 5 && digit5 ==6 && digit6 == 2 ) {
            System.out.println();
            System.out.println("Hey, Dude, welcome to java club.");
            return true;
        }
        else {
            System.out.println(" is not the right sequence");
            return false;
        }
    }
    
    public static void main (String args[])
    {   Scanner sc = new Scanner(System.in);
        int x;
 
        printCode();
        System.out.print("\n");
        
        do {
            x = sc.nextInt();
            
            switch(x) {
                case 0:     break;
                case 1:     if (digit1 == 9) digit1 = 0;
                            else digit1++;
                            break;
                case 2:     if (digit2 == 9) digit2 = 0;
                            else digit2++;
                            break;
                case 3:     if (digit3 == 9) digit3 = 0;
                            else digit3++;
                            break;
                case 4:     if (digit4 == 9) digit4 = 0;
                            else digit4++;
                            break;
                case 5:     if (digit5 == 9) digit5 = 0;
                            else digit5++;
                            break;
                case 6:     if (digit6 == 9) digit6 = 0;
                            else digit6++;
                default:    System.out.println("Invalid key.  Please try again.");
                            break;
            } //switch          
        } while (x != 0 && !checkCode());
    }
}

**********************************************************************