CS111 Lab 1b:

Java Basics - Review and Practice

Part I: ArrayLists


Identify the output produced by the segments of code below or indicate if an error occurs. Assume ArrayList and Arrays have been imported.
  1. public static void main(String[] args) { ArrayList<Integer> nums = new ArrayList<>(); fill(nums); System.out.println(nums.toString()); System.out.println("nums size is " + nums.size()); } public static void fill(ArrayList<Integer> nums) { for (int i = 10; i > 1; i = i - 2) { nums.add(i); } }

  2. public static void main(String[] args) { ArrayList<Double> multiples5 = new ArrayList<>(); fill(multiples5); System.out.println(multiples5.toString()); } public static void fill(ArrayList<Double> multiples5) { for (double i = 5; i <= 50; i += 5) { if (i % 2 != 0) { multiples5.add(i); } } }

  3. public static void main(String[] args) { ArrayList<String> colors = new ArrayList<>(); fillColors(colors); details(colors); } public static void fillColors(ArrayList<String> colors) { colors.add("blue"); colors.add("green"); colors.add("yellow"); colors.add("purple"); colors.add("green"); colors.add("red"); colors.add("orange"); colors.add("kiwi"); } public static void details(ArrayList<String>colors) { System.out.println(colors.get(4)); System.out.println("orange: " + colors.contains("orange")); System.out.println("black: " + colors.contains("black")); System.out.println("kiwi: " + colors.indexOf("kiwi")); }

  4. public static void main(String[] args) { ArrayList<String> words = new ArrayList<>(); fill(words); update(words); System.out.println(words.toString()); } public static void fill(ArrayList<String> words) { words.add("what"); words.add("where"); words.add("who"); words.add("when"); words.add("how"); } public static void update(ArrayList<String>words) { words.remove("what"); words.remove(2); words.set(2, "whatever"); }

Part II: Two-dimensional Arrays


Identify the output produced by the segments of code below or indicate if an error occurs. Assume ArrayList and Arrays have been imported.
  1.             
                    public static void main(String[] args) {
                        char [][] asciiArt = {
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'}
                                };
                        
                        for (int i = 0; i < asciiArt.length; i++) {
                            System.out.println(Arrays.toString(asciiArt[i]));
                        }
                    }
                


  2.             
                    public static void main(String[] args) {
                        char [][] asciiArt = {
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'X', 'X', 'X'}
                                };
                        
                        update(asciiArt);
                        display(asciiArt);
                    }
                    
                    public static void update(char [][] asciiArt) {
                        asciiArt[0][1] = 'O';
                        asciiArt[3][1] = 'O';
                        asciiArt[3][2] = 'O';
                    }
                    
                    public static void display(char [][] asciiArt) {
                        for (int i = 0; i < asciiArt.length; i++) {
                            System.out.println(Arrays.toString(asciiArt[i]));
                        }
                    }
                


  3.             
                    public static void main(String[] args) {
                        int [][] arr = new int[5][5];
                        fillTable(arr);
                        display(arr);
                    }
                    
                    public static void fillTable(int [][] arr) {
                        for (int row = 0; row < arr.length; row++) {
                            for (int col = 0; col < arr[row].length; col++) {
                                arr[row][col] = row + col;
                            }
                        }
                    }
                
                    public static void display(int [][] arr) {
                        for (int i = 0; i < arr.length; i++) {
                            System.out.println(Arrays.toString(arr[i]));
                        }
                    }
                






Part III: Solve

  1. Write a program that receives an ArrayList of integers and shuffles the numbers. Code the algorithm for shuffling.
  2. Write a method to build a multiplication table as a two-dimensional array containing multiples from 1 to n, where n is between 5 and 9.
  3. Write a program that creates an ArrayList containing 20 random integers between 0 and 500. Write a method that finds the largest and smallest integers stored in the ArrayList. Return these values as a two-cell array.




Part IV: File Input/Output and Exception Handling

  1. What does the program below do?
  2. Is the File class used to create a file object? Research this class and explain how it works.
  3. Research PrintWriter . Explain how a PrintWriter object is used to print to a text-output stream.
  4. How does this code catch any output exceptions and display an error message?
  5. Where is burger.txt located?

        public class MainApp {

          public static void main(String[] args) {
            // _______________________________________________
            // TASK: CREATE AN ASCII BURGER OF YOUR CHOICE
            // AND WRITE IT TO A TEXT FILE.
            // ________________________________________________

            File file = new File("burger.txt");
            try {
              PrintWriter printWriter = new PrintWriter (file);
              printWriter.println(makeTopBun());
              printWriter.println(makePattie());
              printWriter.println(makeCheese());
              printWriter.println(makePattie());
              printWriter.println(makeBottomBun());

              System.out.print(makeTopBun());
              System.out.print(makePattie());
              System.out.print(makeCheese());
              System.out.print(makePattie());
              System.out.print(makeBottomBun());


              printWriter.close();
            }
            catch (IOException e){
              System.out.println("This file could not be created.");
            }

          }

          public static String makePattie() {
            String str = "          .o[.[[    :::::::::::::::::```````]|   \n";
            str += "           [o```[;;;;;;;;;;;;;```::::::::]]:|'       \n\n";
            return str;
          }

          public static String makeBottomBun() {
            String str = "        {o{=======..==..=.````````````````'''''o}`\n";
            str += "        {o{{{ ssss{{{{{{oooooooooooooo}}}}}}o}}o}      \n";
            str += "         {.{{{sSS.sssSS:ss::SS:sssss.SS.SSsssso}:    \n\n";
            return str;
          }

          public static String makeCheese() {
            String str = "             ~o=`=...~~~~~~...~~~++`+++++=====++>>>\n";
            str += "      <<<~~s>'<,,,,,,,+==<<>o~~~~~>>~~~~~~>>>''      \n\n";
            return str;
          }



          public static String makeTopBun() {
            String str = "                ^ ^ '''......,.''                \n";
            str += "            `.://+++oooooooooooosssso+/-.              \n";
            str += "        `-/o++;;ooooooooooooooooosssss++s+:`           \n";
            str += "       `:+o++;o;oooooooooooooooooooosssssss++o:`       \n";
            str += "      :+o++o;o;oooooooooooooooooooooosssssssss++/`     \n";
            str += "     `/o++o;oo;ooooooooooooooooooooooosssssssss++o.    \n";
            str += "     +o++o;o;ooooooooooooooooooooossssssssssss;;s+/    \n";
            str += "     {{:+/:::-....:::::::::............oooo::::}::}} \n\n";
            return str;
          }


        }