CS111 Lab 1a:

Java Basics - Review and Practice

Part I: Methods


Identify the output produced by the segments of code below or indicate if an error occurs.
  1. 
                    public static void main(String[] args ){
                        int x = 1, y = 2, z = 4;
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                        
                        z = p(x,y);
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                    }
      
                    public static int p (int x, int a ){
                        x = 7;
                        return a * 2 + x;
                    }
                


  2. 
                    public static void main(String[] args ){
                        int x = 2, y = 3, z = 0;
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                        
                        z = p(y,x);
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                    }
      
                    public static int p (int z, int y ){
                        int x = z;
                        return y * 2 + z * 3 + x;
                    }
                


  3. 
                    public static void main(String[] args ){
                        int x = 2, y = 3, z = 0;
                        x = p(x,y);
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                        y = p(x,y);
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                    }
      
                    public static int p (int x, int z ){
                        int y = z;
                        return x - z;
                    }
                


  4. 
                    public static void main(String[] args ){
                        int x = 2, y = 3;
                        x = p(x,y);
                        x = p(y,x);
                        x = p(x,y);
                        System.out.print( "x = " +  x +  "y = " +  y);
                    }
            
                    public static int p (int y, int x){
                        return 2 * x + y;
                    }
                


  5. 
                    public static void main(String[] args ){
                        int x = 2, y = 3, z = 0;
                        funcA (x, z);
                        System.out.print("INSIDE MAIN");
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                    }
    
                    public static void funcA (int x, int y){
                        int z = x;
                        System.out.print("INSIDE DISPLAY");
                        System.out.print( "x = " +  x +  "y = " +  y + "z = " +  z);
                    }
                


  6. The Farmer in the Dell is a children's song that most people have sung at one time or another. Write an efficient Java program that displays the lyrics of entire song.
      The farmer in the dell
      The farmer in the dell
      Hi-ho, the derro-o
      The farmer in the dell

      The farmer takes a wife
      The farmer takes a wife
      Hi-ho, the derro-o
      The farmer takes a wife

      The wife takes a child
      The wife takes a child
      Hi-ho, the derro-o
      The wife takes a child

      The child takes a nurse
      The child takes a nurse
      Hi-ho, the derro-o
      The child takes a nurse

      The nurse takes a cow
      The nurse takes a cow
      Hi-ho, the derro-o
      The nurse takes a cow

      The cow takes a dog
      The cow takes a dog
      Hi-ho, the derro-o
      The cow takes a dog

      The dog takes a cat
      The dog takes a cat
      Hi-ho, the derro-o
      The dog takes a cat

      The cat takes a rat
      The cat takes a rat
      Hi-ho, the derro-o
      The cat takes a rat

      The rat takes the cheese
      The rat takes the cheese
      Hi-ho, the derro-o
      The rat takes the cheese

      The cheese stands alone
      The cheese stands alone
      Hi-ho, the derro-o
      The cheese stands alone





PART II: Arrays




  1. What does the array nums contain after executing the following instructions?
    
                    int [] nums = {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12};
                    nums[0] = nums[10 - 1];
                    nums[10 - 3] = 7;
                


  2. What does the array nums contain after executing the following instructions?
    
                    int [] nums = {5, 10, 15, 20, 25, 30, 35};
                    int i = 4;
                    nums[i] = nums[i + 1];
                


  3. What does the array nums contain after executing the following segment of code?
    
                    int [] nums = {2, 3, 5, 1, 8, 4, 9, -3, 6, 3};
                    
                    for (int i = 0; i < nums.length; i++){
                        nums[i] = nums[i] + 1;
                    }
                


  4. What does the array nums contain after executing the following segment of code?
    
                    int [] nums = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
                    
                    for (int i = 0; i < nums.length - 2; i++){
                        nums[i] = nums[i + 1];
                    }
                


  5. What output is produced after executing the following segment of code?
    NOTE: Arrays.toString(arr) is an easy way to display the contents of an integer array.
    				public static void main(String[] args) {
    					int[] arr = { 1, 2, 3 };
    					System.out.println(Arrays.toString(arr));
    					arr = func(arr);
    					System.out.println(Arrays.toString(arr));
    				}
    
    				public static int [] func(int[] arr) {
    					int [] brr = new int [arr.length*2];
    					int j = 0;
    					for (int i = 0; i < arr.length; i++){
    						brr[j] = arr[i];
    						j++;
    						brr[j] = arr[i];
    						j++;
    					}
    					return brr;
    				}
                






Part III: Strings


  1. Write a method, noVowels(), that receives a String, str and removes all the vowels.

    Example:






  2. Write a method, shuffle(), that receives a String, str and returns it shuffled.

    Example:


    Use the Fisher-Yates (Knuth) Shuffle algorithm
    Implementation Tips:
    (a) Convert to Array: Use word.toCharArray() to get a mutable character array.
    (b) Iterate and Swap: Loop through the array visiting each character.
    (c) Random Index: For each character at position i, pick a random index in the word.
    (d) Swap: Exchange the characters at index i with the random. Use a temporary variable.







  3. Write a method, countCo_e(), that receives a String, str and counts and returns the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd'. This means we will count "cope", "coke", "come", "core", etc.

    Example: