To Lecture Notes

IT 313 -- Jan 16, 2020

Review Exercises

  1. What does the IntelliJ shortcut key combination Alt-Enter do?
  2. What is the output?
    System.out.println(1 + 2 + "3" + 4 + 5);
    System.out.println(6 / 4 * 4);
    
    Ans: The order of precedance is the same for + when it means addition and + when it means concatenation. Remember that
    number + number --> number
    string + string --> string
    number + string --> string
    string + number --> string
    Output for first println statement:
    3345
    
    For the second println statement:
    int / int --> int
    double / int --> double
    int / double --> double
    double / double --> double
    Therefore, 6 / 4 * 4 == 1 * 4 == 4.
    
  3. What is the output?
    double[ ] s = new double[5];
    a[2] = 5.1;  a[0] = 3.8; a[4] = a[2];
    for(double x : a) {
        System.out.printf("%4.2f ", x);
    }
    System.out.println( )
    
  4. Explain the output:
    String s = "dog", t = "dog";
    String u = new String("dog");
    System.out.println(s.equals(t) + " " + s.equals(u));
    System.out.println((s == t) + " " + (s == u)); 
    // Output:
    true true
    true false
    
  5. Look at the ave4 module in the ComputeAverage Project. This example shows how to process multiple fields on each line of an input file.
  6. Convert the wordtonum Example module in the TerminalIO Example project so that it uses a String array instead of if..if else statements. Use this String array:
    String[ ] words = {"zero", "one", "two", "three", 
      "four", "five", "six", "seven", "eight", "nine"}
    
    // Ans:
    import java.util.Scanner;
    public class Main {
    
        public static void main(String[] args) {
            String[ ] words = {"zero", "one", "two", "three", 
                "four", "five", "six", "seven", "eight", "nine"};
            Scanner s = new Scanner(System.in);
            System.out.print(
                "Enter the word for a number from 0 to 9: ");
            String word = s.nextLine( );
            int i;
            for(i = 0; i < words.length; i++) {
                if(words[i].equals(word)) {
                    System.out.println(i);
                }
            }
            System.out.println("Unknown number");
            }
        }
    }
    
  7. Modify the Drawing Example to draw a picture like this. Look up the Color object in the Java class library to see how to create a custom color.
    Here is a Table of Extended HTML Colors if you want to use hex color codes.
    Ans: Here is the modified source code for the paintComponent method of the MyPanel class.

Writing your Own Classes

Project 3

Traditional Tests

Unit Tests