To Lecture Notes

IT 372 -- Apr 17, 2019

Review Questions

  1. Translate these formulas into Java:
    1. a = √(s + 3 t3n)
    2. b =
          2 s t1 + n
      1 - 5(u + 2)2n

      Answers:
      a = Math.sqrt(s + 2 * Math.pow(t, 3 * n)
      b = (2 * s * Math.pow(3 * n)) / (1 - 5 * Math(u + 2, 2 * n))
      
  2. Suppose you have a hashmap collection:
    HashMap<String, Integer> map = new HashMap<String, Integer>( )
    
    How do you add an Integer object to the collection? How do you retrieve the Integer object stored using the key "K123"? We will use a similar concept when we store information from an activity in the storedInstanceState object. Ans:
    // Put the value 35 into the Hashmap using the key "C123".
    map.put("C123", 35);
    // Retrieve the value stored at the key "C123" from the hashmap.
    int n = String.valueOf(map.get("C123"));
    
  3. How do you allow the text in a button to include lowercase letters?
    Ans: include this attribute:
    android:textAllCaps="false"
    
  4. Where do you place the image that is used for the background image in a layout?
    Ans: In the app/src/main/res/drawable folder
  5. Explain the difference between android:gravity and android:layout-gravity.
    Ans: gravity determines where the contents of the widget are positioned. layout_gravity determines where the widget is positioned within its parent.
  6. How do you get the text out of an EditText widget?
    EditText editText1 = (EditText) findViewById(R.id.edittext1);
    String s = editText1.getText( ).toString( );
    

TempConverter Project

The Activity Lifecycle