IT 212 -- Nov 2, 2020

Review Exercises

  1. Explain how these Python re module methods work:
        match search findall split sub Ans:
    match -- Returns a Match object for a match to the regular expression if the match starts at the beginning of the input string,
    search
    -- Returns a Match object for the first match to the regular expression in the input string, None otherwise.
    findall -- Finds all matches to the regular expressons.
    split -- Extracts fields (substrings) using the matches as the delimiters.
    sub -- Replaces all matches with the substitution character.
  2. What is the difference between an interpreted language and a compiled language?
    Ans: the source code of the interpreted language is interpreted directly by the interpreter; the source code of a compiled language is compiled into a binary executable file, which can be run directly on the CPU (native executable for C and C++) or on a virtual machine (bytecode executable for Java, C#, Python). See the table a the end of the Oct 28 Notes.
  3. When is a Python script compiled? Where is the executable file located?
    Ans: A Python script is only compiled if it is an imported module; the compiled executable (.pyc extension) is placed in the __pycache__ folder.
  4. How do you compile and run the Java program HelloWorld.java?
    Ans:
    javac HelloWorld.java
    java HelloWorld
    
  5. What is the name of the Java startup method?
    Ans: main
  6. Look at the Greeter and Cel2Fahr Examples. See the section An Online Java Compiler below if you don't have Java installed on your machine.
  7. Write a Java application that inputs the rest mass (m0) and velocity (v), then computes and outputs the actual mass using Einstein's relativity formula:
          m = m0 / √(1 - (v / c)2)
    where c is the velocity of light 2.998 × 105 km/sec.  Validate the input to insure that the rest mass is non-negative and the input velocity is less than the speed of light. Translate this Python script into Java:
    import sys
    import math
    
    # Define speed of light as constant C.
    # The units are km/sec.
    C = 2.998e5
    m0 = input("Enter rest mass: ")
    
    if m0 < 0:
        print("Rest mass must be nonnegative.")
        sys.exit( )
    
    v = input("Enter velocity: ")
    if v > abs(c):
        print("Velocity cannot be > c.")
        sys.exit( )
    
    m = m0 / Math.sqrt(1 - (v / c) ** 2)
    print("Relativistic mass is", m)
    
    // Here is the translated Java version:
    import java.util.Scanner;
    public class Relativity
    {
        public static void main(String[ ] args)
        {
            Scanner input = new Scanner(System.in);
            final double c = 2.998e5;
            System.out.println("Enter rest mass: ");
            double m0 = Double.parseDouble(input.nextLine( ));  
            if (m0 < 0)
            {
                System.out.println("Rest mass must be nonnegative.");
                System.exit(0);
            }
            System.out.println("Enter velocidy: ");
            double v = Double.parseDouble(input.nextLine( ));  
            if (v > c)
            {
                System.out.println("Velocity must be < c.");
                System.exit(0);
            }
            m = m0 / Math.sqrt(1 - Math.pow(v / c, 2));
            System.out.println("Relativistic mass is " + m + ".")
        }
    }
    

An Online Java Compiler

Shell Commands

Java Constants

Java Variables

Java Primitive Datatypes

Datatype Meaning Size in Bytes Minimum Maximum Significant Digits
byte Integer 1 -128 127  
short Integer 2 -32,768 32,767  
*int Integer 4 -2.1×109 2.1×109  
long Integer 8 -9.2×1018 9.2×1018  
float Floating Point 4 -3.4×1038 3.4×1038            7
*double Floating Point 8 -1.8×10308 1.8×10308           15
*char Character 2 Any Unicode Character  
*boolean Logical 1 false true  

String Objects

Java Operators