To Lecture Notes

IT 372 -- Apr 16, 2025

Review Exercises

  1. Translate these formulas into Java:
    1. a = √(s + 3 t3n)
      Answer:
      double a = Math.sqrt(s + 3 * Math.pow(t, 3 * 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));
      
  2. 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 when rotated 90 degrees counterclockwise. The goal of this operator is to express an if..else statement in one line. Here is an example if..else statement:
    int n = 476;
    String s = "";
    if (n >= 500) {
        s = "large"; 
    }
    else {
        s = "small";
    }
    
    Here is this if..else statement rewritten using the Elvis operator:
    int n = 476;
    String s = (n >= 500) ? "large" : "small";
    
  3. Create an app with a radio button group containg four radio buttons with text Freshman, Sophomore, Junior, Senior. Answer:
          activity_main.xml  MainActivity.java
  4. Set up an app with an EditText widget that only accepts decimal numbers. Answer:
          activity_main.xml 
  5. 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

BeerAdvisor Example

Array Adapters

The Android Toast

The Activity Lifecycle

Android Platform