
Exercise 1
-----------

Initial value of a is 77
Initial value of aGlobal is 1
Value of a in swapInts is 55
Value of aGlobal in swapInts is 55
Final value of a is 77
Final value of aGlobal is 55
Initial value of o1.a is: 1
Value of o1.a in swapObjects is 0
Final value of o1.a 0
Final value of o2.a 1


* Each correct answer is worth 1pts

* -2 pts if only numbers are written

* -1 pts if some text is missing


Exercise 2
-----------

correct return type  (void)                            [2pts]
correct initialization for sumOdd (0)                  [2pts]
correct instruction in "if" condition (a[i])           [2pts]
correct instruction in "if" statement (a[i])           [2pts]
correct instruction in "else" statement (+=)           [2pts]


Correct program:

	public static void oddEven(int n){
		int sumEven=0;
		int sumOdd=0;
		int[] a = new int[n];
		
		for (int i=0; i<n; i++) {			
			a[i] = i+1;
		}

		for (int i=0; i<n; i++) {			
			if(a[i]%2==0){sumEven+=a[i];}
			else {sumOdd+=a[i];}
		}
		System.out.println("The sum of even numbers is "+ sumEven);
		System.out.println("The sum of odd numbers is "+ sumOdd);		
	}


Alternative solutions that do not change the program completely will
be considered.


Exercise 3
----------

 Lines that will cause compilation errors:

    5 and 6        accessing private members [3 pts]
    11             private method            [2 pts]

    In a different package: the above plus

    7               package-private variable [2 pts]
    12 and 13       package-private methods  [3 pts]



Exercise 4
----------
 
Block B: [1pts]
   
   if (n<0 && n>9){
	System.exit(0);
   }
   this.n=n;

Block C: [2pts]

   this.n=n.n;

Block D: [1pts]

   return this.n;

Block E: [2pts]

   return this.n == n.n;

Block F: [1pts]

   return this.n>n.n;

Block G: [3pts]

   return this.n+"";

Block H: [1pts]

   private Number n1;
   private Number n2;

Block I: [2pts]

   super();
   this.n1= new Number();
   this.n2= new Number();		

Block J: [1pts]

   super();
   this.n1= n1;
   this.n2= n2;		

Block K: [2pts]

   return this.n1.equals(n.n1) && this.n2.equals(n.n2);

Block L: [3pts]

   return this.n1.compare(n.n1) || (this.n1.equals(n.n1) && this.n2.compare(n.n2));

Block M: [2pts]

   return n1.toString()+n2.toString();

Block N:  [1pts]
   private Number n1;
   private TwoDigitNumber n2;

Block O: [1pts]

   super();
   this.n1 = n1;
   this.n2 = n2;

Block P: [2pts]

   return this.n1.equals(n.n1) && this.n2.equals(n.n2);

Block Q: [3pts]
   return this.n1.compare(n.n1) || (this.n1.equals(n.n1) && this.n2.compare(n.n2));	

Block R: [2pts]

   return n1.toString()+n2.toString();

Note: Call to super() is not required, but nice to have

Exercise 5
-----------

a) 10 pts

    public static Number[] genNums(){
       Number[] nums = new Number[100];
       int n;
       for(int i=0; i<100; i++) {
	 n = randonInt(100);
         if (n>=0 && n<10){
	    nums[i] = new Number(n);	    	
         }
	 else {
            nums[i] = new TwoDigitNumber(getFirstDigit(n), getSecondDigit(n)); 
	 }
       }
       
       return nums;	
    }


b) 10pts

	public static String printMax(ThreeDigitNumbers[] nums){
		if (nums.length==0) { return "Empty array"; }
		else {
			ThreeDigitNumbers max = nums[0];
			for (int i=0;i<nums.length;i++){
				if (nums[i].compare(max)){
					max = nums[i];
				}
			}
			return max.toString();
		}
	}


Exercise 6
-----------


public int sumDigits(String str) {   // correct signature and return parameters [5pts]

int result=0;

if (str.length()==0) return 0;  // check length 0 [5pts]

for (int i=0;i<str.length(); i++){ // correct bounds while loop and use of isDigit [5pts] 
   if(Character.isDigit(str.charAt(i))){
   result += Integer.parseInt(""+str.charAt(i));  //correct use of parseInt [5pts]
   }
}
 return result; 
}



Alternative solutions are perfectly fine.

