To Lecture Notes

IT 372 -- Apr 17, 2024

Review Exercises

  1. Convert the BeerAdvisor Example to use Method 4 instead of Method 1 for the click event handler.
    Answer: Here are the modified app files.
        activity_main.xml  strings.xml  MainActivity.java  BeerAdvisor.java
    The files strings.xml and BeerAdvisor.java files are unchanged from the BeerAdvisor Example.
  2. Translate these formulas into Java:
    1. a = √(s + 3 t3n)
      Answer:
      double a = Math.sqrt(s + 3 * Math.pow(t, 1 + n));
      
    2. b =
          2 s t1+n
      1 - 5(u + 2)2n

      Answer:
      double b = (2 * s * Math.pow(t * (1 + n)) / 
                 (1 - 5 * Math(u + 2, 2 * n));
      
  3. How does the Java Elvis operator work? The formal name for this operator is the tertiary conditional operator. Answer: The ? operator looks like Elvis Presley's waivy hair. Here is an example.
    int n = 476;
    String s = (n >= 500) ? "large" : "small";
    
  4. Create an Android layout with a checkbox, a button, and a textview. When the button is clicked, the state of the checkbox (checked attribute) is displayed in the textview. Answer:
          activity_main.xml  MainActivity.java
  5. Create an app with a radio button group containg four radio buttons with text Freshman, Sophomore, Junior, Senior. Answer:
          activity_main.xml  MainActivity.java
  6. Set up an app with an EditText widget that only accepts decimal numbers. Answer:
          activity_main.xml 
  7. Create a RollDice Example that randomly displays these dice output images displayed in two ImageView widgets:
         face1.jpg  face2.jpg  face3.jpg   face4.jpg  face5.jpg  face6.jpg

    Use this RollDice class:
    public class RollDice {
        public static ArrayList<Integer> roll(int numDiceFaces) {
            ArrayList<Integer> rolls = new ArrayList<Integer>( );
            Random r = new Random( );
            int die1 = r.nextInt(numDieFaces) + 1;
            int die2 = r.nextInt(numDieFaces) + 1;
            rolls.add(die1);
            rolls.add(die2);
            return rolls;
        }
    }
    
    Use android:orientation="horizontal" for the linear layout.
    Answer: Here are the layout, RollDice class, and activity files:
         activity_main.xml   RollDice.java   MainActivity.java

Project 2

The Android Toast

Array Adapters

The Activity Lifecycle