To Lecture Notes

IT 212 -- Nov 16, 2020

Review Exercises

  1. Zoom Poll Quiz. Correct answers are marked with *:
    1. Translate this Python datatype into Java: str
      a. str        b. Str       c. string       *d. String
    2. Translate this Python datatype into Java: float
      *a. double        b. Double       c. float       d. Float
    3. Translate this Python datatype into Java: bool
      a. bool        b. Bool       *c. boolean       d. logical
    4. Translate this string into Java: '''abc'''
      a. abc        b. 'abc'         c. "abc"         d. \"abc\"
    5. Translate this for loop header into Java:
      for i in range(0, 100):
      
      *a. for(int i = 0; i < 100; i++) {
      b. for(int i = 0; i <= 100; i++) {
      c. for(int i : 0..99) {
      d. for(int i : 0..100) {
    6. Translate this standalone method header into Java:
      def inchesToCm(inches):
      
      a. public double static inchesToCm(double inches) {
      b. public double static inches_to_cm(double inches) {
      *c. public static double inchesToCm(double inches) {
      d. public static double inches_to_cm(double inches) {
    7. Translate this print statement into Java:
      print('Hello', end=' ')
      
      *a. System.out.print("Hello")
      b. System.out.print("Hello", end)
      c. System.out.println("Hello")
      d. System.out.println("Hello\n")
    8. Translate this list into Java: a = [1, 3, 5]
      a. int a = [1, 3, 5];
      b. int a = {1, 3, 5};
      *c. int[ ] a = {1, 3, 5};
      d. int a[ ] = (1, 3, 5);
    9. (Python) Which statement adds the value v to the dictionary d using the key k?
      a. d.put(k, v)   b. d.put(v, k)   *c. d[k] = v     d. d[v] = k
    10. What do you need to get this Python main method to execute?
      def main(  ):
            print("Hello, World.")
      
      a. Declare main method to be public.
      b. Declare main method to be static.
      c. Declare main method to be void.
      d. Put main( ) at bottom of script.
  2. What is the difference between these Python statements:
    a = [1, 3, 5]
    b = (1, 3, 5)
    c = {1 : 'one', 2 : 'two', 3 : 'three'}
    
  3. Try out this demo program, which uses the command line arguments defined by String[ ] args:
    public class Demo
    {
        public static void main(String[ ] args)
        {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            System.out.println("Sum: " + (a + b));
        }
    }
    
    Here are statements to compile and run this demo program:
    > javac Demo.java
    > java Demo 5 7
    Sum: 12
    
  4. How do you implement command line arguments in Python? Ans:
    import sys
    a = sys.argv[0]
    b = sys.argv[1]
    print(a, b)
    sum = int(a) + int(b)
    print("Sum: ", sum)
    
  5. Rewrite the Overtime Example so that the method named getTotalWages returns the total wages. Ans:
    # Python Version
    def get_total_wages(hours_worked, hourly_salary):
        if hours_worked > 40.0:
            return hourly_salary * (40.0 + (40.0 - hours_worked) * 1.5)
        else:
            return hourly_salary * hours_worked
    
    hw = 10.0
    hs = 10.0
    print(get_total_wages(hw, hs))
    
    // Java Version
    public class Overtime 
    {
         public static void main(String[ ] args)
         {
             double hoursWorked, hourlySalary, totalWages;
             hoursWorked = 10.0;
             hourlySalary = 10.0;
             System.out.println(
                 getTotalWages(hoursWorked, hourlySalary));
         }
    
         public static double getHourlySalary(
             double hoursWorked, double hourlySalary))
         {
             if (hoursWorked > 40.0)
             {
                 return theHourlySalary * 
                     (40.0 + (hoursWorked - 40.0) * 1.5);
             }
             else
             {
                 return theHoursWorked * theHourlySalary;
             }   
         }
    }
    
  6. Rewrite the LongSong Example so that a method named playSong prints the song. Pass in the number of verses as a parameter. Ans:
    public class LongSong
    {
        public static void main(String[ ] args)
        {
            // Print 99 verses of the song.
            playSong(99);
        }
    
        public static void playSong(int numVerses)
        {    
            for (int n = numVerses; n >= 1; n--)
            {
                System.out.println(n + 
                    " bottles of beer on the wall,");
                System.out.println(n + 
                    " bottles of beer.");
                System.out.println(
                    "Take one down and pass it around,");
                System.out.println((n - 1) + 
                    " bottles of beer on the wall.\n");
            }
        }
    }
    
  7. Translate the Python Card class into Java. Write a main method to test it. Ans:
    // Card Example
    // Source code file: Card.java
    // A Card object is a standard poker playing card.
    public class Card
    {
        public int rank;
        public String suit;
    
        public Card(int theRank, String theSuit)
        {
            this.rank = theRank;
            this.suit = theSuit;
        }
    
        public String color( )
        {
            if (this.suit.equals("C") || 
                this.suit.equals("S"))
            {
                return "black";
            }
            else
            {
                return "red";
            }
        }
    
        public String toString( )
        {
            String[ ] symbols = {"0", "1", "2", 
                "3", "4", "5", "6", "7", "8", "9",
                "10", "J", "Q", "K", "A"};
            return symbols[this.rank] + this.suit;
        }
    }
    
  8. Create a Python MiniYahtzee class with the instance variable _dice, which is a list of the outcomes of three six-sided dice. (Yahtzee is a dice game where five dice are used.) The instance methods are __init__, __str__, roll_dice, set_dice, and classify, where classify classifies the dice roll as NONE = 1, PAIR = 2, STRAIGHT = 4, TRIPLE = 3. Examples are:
    NOTHING: 3 2 6
    PAIR: 5 5 1
    STRAIGHT: 2 3 4
    TRIPLE: 4 4 4
    
    Also create the traditional test script test1.py. Finally create a simulation script test3.py that records the number of simulated rolls obtained of each roll type in 100,000 rolls. Here are the theoretical percentages for each roll type:

    Roll Type Fraction Percentage
    NOTHING   96/216     44.44%
    PAIR   90/216     41.67%
    STRAIGHT   24/216     11.11%
    TRIPLE     6/216       2.78%

    Ans: Here are the scripts miniyahtzee.py, constants.py, test1.py, and test3.py.

Project 4

Java Getters and Setters

This section was not discussed in class, and will not be covered on the multiple choice final exam.

In traditional software engineering, instance variables are private. Their values are accessed or modified using getter and setter methods. For example, if the instance variable is age, then the getter is named getAge; the setter is named setAge.