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 = 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 x - y;
                    }
                


  2.             
                    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;
                    }
                


  3.                 
                    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;
                    }
                


  4.                 
                    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);
                    }
                





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;
    				}
                






Writing Code

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

    Example:






  2. Write a method, countCo_e(), that receives a String, str 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:






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

    Example: