To Lecture Notes

IT 212 -- Nov 9 , 2020

Review Exercises

  1. Translate this Python script into Java:
    score = float(input('Input a course score:'))
    if course_score >= 70.0:
        print('Pass')
    else:
        print('Fail')
    
    // Java Translation:
    public class CourseGrade 
    {
        public static main(String[ ] args)
        {
            Scanner s = new Scanner(System.in);
            System.out.print("Input a course score: ");
            double courseScore = Double.parseDouble(
                s.nextLine( ));
            if (courseScore >= 70.0)
            {
                System.out.println("Pass");
            }
            else
            {
               System.out.println("Fail");
            }
        }
    }
    
  2. List ways that Java is different than Python.
    1. All Java code must be contained in a class.
    2. A main method must be present to be the startup method.
    3. Every line (except those followed by a block) must end with a semicolon.
    4. // marks the beginning of a source code comment line.
    5. A Scanner object is need to read from the keyboard.
    6. Use { } to delimit a code block.
    7. n++ and n-- are short versions of n += 1 and n -= 1.
  3. System.out.println(output); and System.out.print(output); are used instead of
    print(output) and print(output, end="")

Java Control Structures

Java Methods

  1. What do each of the Java words mean in the main method header?
    public  static  void  main  args
    
    Ans:
    public: the method can be called from anywhere
    static: the method is not called from an object
    void: the method has no return value
    String[ ] args: args is the name of the command line arguments array.
  2. Translate these Python methods fragment into Java, then test them. Here is the form of a Java standalone method:
    public static returnType methodName(
        parameterDatatype1 param1, 
        parameterDatatype2 param2) {
    
        body of method;
        return output;
    }
    
    # h.
    # Use a for loop in Java.
    # Python Statements:
    def countDown(n):
        while n >= 0:
            print(n)
            n -= 1
        print("\nBlastoff")
    
    # Standalone script.
    countDown(10)
    
    // Java Statements:
    public class CountDown
    {
        public static void countDown(int n)
        {
            for(int i = n; i >= 0; i--)
            {
                System.out.println(i);
            }
            System.out.println("\nBlastoff");
        }
    
        public static void main(String[ ] args)
        {
            countDown(10);
        }
    }
    
    # i.
    # Python Statements:
    def celToFahr(cel):
        fahr = 9.0 * cel / 5.0 + 32.0
        return fahr
    
    # Standalone script:
    c = 20.0
    print(celToFahr(c))
    
    // Java Statements:
    public class Cel2Fahr
    {
        public static celToFahr(double cel)
        {
            double fahr = 9.0 * cel / 5.0 + 32.0;
            return fahr;
        }
    
        public static void main(String[ ] args)
        {
            System.out.println(celToFahr(20.0));
        }
    }
    
  3. Create a Java array that contains three strings and print it using the two methods to access array items "access by value" and "access by index".
    String[ ] a = {"abc", "def", "hij"};
    
    # Method 1: access by value:
    for(String s : a)
    {
        System.out.print(s + " ");
    }
    
    # Method 2: access by index:
    for(int i = 0; i < a.length; i++)
    {
        System.out.print(a[i] + " ");
    }
    

Random Number Generators

Review Exercise

Java Arrays

Java Classes