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

Initial value of secret is: 22
The value of secret in changeSecret is: 11
The value of num in changeSecret is: 2
The value of names[0] in swapNames is Jennifer
The value of  num in swapNames is: 11
The value of names[0]  is Jennifer
The value of names[0] in swapNames is Maria
The value of  num in swapNames is: 11
The value of obj.names[0]  is Maria
The value of num  is: 11

* Each correct answer is worth 1pts

* -2 pts (overall) if only numbers are written

* -1 pts (overall) if some text is missing


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

correct return type  (boolean)                            [2pts]
correct type for n   (int)                                [2pts]
correct bound check in for loop (i<n)                     [2pts]
correct branch condition in "if" statement (n%i==0)       [2pts]
"return true" instruction outside for loop                [2pts]


Correct program:


	public static boolean isPrime(int n){
		for(int i=2; i<n;i++){
			if(n%i == 0){
				return false;
			}
		}
		return true;
	}

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

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

 Lines that will cause compilation errors:

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

    In a different package: the above plus

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



Exercise 4
----------
 
Block B: [1pts]
   
   private int population;

Block C: [1pts]

   this.name = "Göteborg";
   this.population = 500000;

Block D: [2pts]

   this.name = name;
   this.population = population;

Block E: [2pts]

   this(c.name, c.population);

Block F: [2pts]

   return this.name;

Block G: [1pts]

   return this.population;

Block H: [3pts]

   return this.name.equals(otherCity.name);  

Block I: [3pts]

   return this.population + otherCity.population;  

Block J: [2pts]

   return this.name + "has a population of "+ this.population + "inhabitants";

Block K: [2pts]

   private City city;
   private City[] eastLeague;

Block L: [2pts]

   super(name, population);
   this.city = city;
   this.eastLeague = eastLeague;

Block M: [3pts]

   super("Stockholm", 1000000);
   City stockholm = new City("Stockholm", 1000000);
   this.city = stockholm;
   City uppsala = new City("Uppsala", 100000);
   City[] eastLeague = {stockholm, uppsala};
   this.eastLeague = eastLeague;
		
Block N:  [3pts]

   for(int i=0; i<this.eastLeague.length; i++){
	if(this.eastLeague[i].sameCity(c)){
		return true;
	}
   }
   return false;

Block O: [3pts]

   String str ="The east league consists of the cities: ";
   for(int i=0; i<this.eastLeague.length; i++){
	str += this.eastLeague[i];
   }
   return str;	

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

a) 10 pts

	public boolean differAtLeastTwoLetters(City otherCity){
		int counter = 0;
		for(int i=0; i<this.name.length();i++){
			if(this.name.charAt(i)!=otherCity.name.charAt(i)){
				counter++;
			}
			
			if(counter >=2){
				return true;
			}
		}
		return false;
	}

b) 10 pts

	public EastCoastCity addToLeague(EastCoastCity ec){
		City[] newLeague = new City[ec.eastLeague.length+1];
		for(int i=0; i<newLeague.length-1; i++){
			newLeague[i] = ec.eastLeague[i];
		}
		newLeague[newLeague.length] = ec.city;
		return new EastCoastCity(ec.city,newLeague, ec.getName(), ec.getPopulation());
	}


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

public int[] shiftLeft(int[] nums) {  // correct signature and return parameters [5pts]

if (nums.length == 0){return nums;}   // check length 0 [5pts]

int tmp= nums[0];

for (int i=0; i<nums.length-1; i++){  // correct loop bound and assignment [5pts]
   nums[i] = nums[i+1];
}
nums[nums.length-1] = tmp;            // correct last element swap [5pts]   
return nums;
  
}

Alternative solutions are perfectly fine.

