CS111 Lab 1b:

Java Basics - Review and Practice

Part 1: 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 = {
                                {'H', 'X', 'X', 'X', 'X'},
                                {'X', 'X', 'I', 'P', 'X'},
                                {'X', 'X', 'X', 'X', 'X'},
                                {'D', 'D', 'X', 'R', '2'},
                                {'4', '4', '3', 'X', 'X'},
                                {'3', '9', '3', 'D', 'Y'}
                                };
                        
                        System.out.println(asciiArt.length);
                        System.out.println(asciiArt[1].length);
                        for (int i = 0; i < asciiArt.length; i++) {
                            if (i % 2 == 1)
                                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 2: 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;
          }


        }

      



Part 3: 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. ArrayList<Integer> nums = new ArrayList<>(); for (int i = 10; i > 1; i = i - 2) { nums.add(i); } System.out.println(nums.toString());

  2. ArrayList<Double> multiples5 = new ArrayList<>(); for (double i = 5; i <= 50; i += 5) { if (i % 2 != 0) { multiples5.add(i); } } System.out.println(multiples5.toString());

  3. ArrayList<int> arrL = new ArrayList<>(); for (int i = 1; i < 4; i ++) { arrL.add(i); } System.out.println(arrL.toString());

  4. ArrayList<String> colors = new ArrayList<>(); colors.add("blue"); colors.add("green"); colors.add("yellow"); colors.add("purple"); colors.add("green"); colors.add("red"); colors.add("orange"); colors.add("kiwi"); 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"));

  5. ArrayList<String> words = new ArrayList<>(); words.add("what"); words.add("where"); words.add("who"); words.add("when"); words.add("how"); words.remove("what"); words.remove(2); words.set(2, "whatever"); System.out.println(words.toString());

Part 4: ArrayLists

  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 ArrayList containing multiples from 1 to n, where n is between 5 and 9. TIP: A 2-D ArrayList is an ArrayList of ArrayLists.
  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.