To Lecture Notes

IT 212 -- Nov 4, 2020

Zoom Poll 10

Java Primitive Datatypes

String Objects

Practice Problems

  1. Translate these Python code fragments into Java. Use this Java test class to test your translations:
    public class TestClass {
        public static void main(String[ ] args) {
            System.out.println("This is a test.");
        }
    }
    
    # a.
    print(a, " ", b)
    print(str(a) + " " + str(b))
    print(f"{a} {b}")
    print("{0:s} {1:s}".format(a, b))
    
    // Java Translation of #a:
    // Only the second the fourth print statements
    // can be translated into Java:
    System.out.println(a + " " + b);
    System.out.printf("%s %s\n", a, b);
    
    # b.
    print(str(a) + " " + str(b), end=" ")
    print(a + b)
    
    // Java Translation of #b:
    System.out.print(a + " " + b + " ");
    System.out.println(a + b);
    
    # c.
    gpa = input("Input GPA: ")
    print("GPA: " + gpa);
    
    // Java Translation of #c:
    Scanner scanner = new Scanner(System.in);
    System.out.print("Input GPA: ");
    double gpa = Double.parseDouble(scanner.nextLine( ));
    System.out.println("GPA: " + gpa);
    
    # d.
    x = a ** b
    
    // Java Translation of #d:
    x = Math.pow(a, b);
    
    # e. (Python duck typing)
    n = 5
    p = 12.3456
    x = 3.78e23
    s = "abc"
    b = True
    c = '*'
    print(n, p, x, s, b)
    
    // Java Translation of #e:
    int n = 5;
    double p = 12.3456;
    double x = 3.78e23;
    String s = "abc";
    boolean b = true;
    System.out.println(String.format("%d %f %s %n %s %c", 
        n, p, x, s, b, c);
    
    # f. 
    import sys
    sys.exit(1)
    
    // Java translation of #f.
    System.exit(1);
    
    To obtain the error code output from
    sys.exit or System.exit, enter this
    line in the Command Prompt Window:
    > echo %errorlevel%
    
    To obtain the error code on a Mac
    running Linux, ehter this line
    in the Terminal Window:
    > echo $?
    
    # g.
    n = 345
    if n >= 1000:
        print("Large")
    elif n >= 500:
        print("Medium")
    else:
        print("Small")
        
    # Java translation of #g:
    int n = 345;
    if (n >= 1000)
    {
        print("Large")
    else if n >= 500:
        print("Medium")
    else:
        print("Small")
    

Java Control Structures

Project 4