224 lecture note #1

[ch. 1-3] Java Basics

1. Beginning Basics

2. Import statements and Keyboard Input

3. Data Types, Variables, Arithmetic Operators

4. Brief overview of Classes and Objects

5. The java.lang.String Class

6. The javax.swing.JOptionPane Class

7. Relational and Logical Operators

8. Conditional Statements (if, switch)

9. Loop Statements (while, for, do-while)

10. More Exercises

  1. [Programming Project #2, p. 217]
    Write a program that allows the user to convert either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius.  Use the following formulas:

    Hint: You can receive the user input, in the form "20.6 C" in the following way.

      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the degree and the scale: "); // "20.6 C"
      double deg = sc.nextDouble(); // reads 20.6 as a double
      String sstr = sc.next();  // reads "C" as a String

    All degree values should be treated as double's.
    You can treat the scale as a string (e.g. "C") or take its 0th char (e.g. 'C'), whichever you like, as long as you accept both the upper case ("C", 'C') or lower case ("c", 'c') letters.


  2. Write a Java program which accepts a sequence of non-negative numbers from the user through the keyboard, where the user enters a negative number to terminate the input, and displays the total and the average.


  3. Write a Java program which tests whether or not a given string is a palindrome.  A string is a palindrome if it is the same when read forward or backward, such as "madam" and "ABBA".  Ask the user for the original string (entered through the keyboard), and display the result ("... is a palindrome" or "... is not a palindrome") to both the terminal (i.e., System.out) and a message dialog window.