CSC 120

Week 5


Sample Programs from Lectures:

Min2.java Min3.java Message2.java MemberVar.java FindRoot.java TwentyOnePickup.java Fcn.java

Reading Assignment:

Read Chapter 4

Lab/Homework Assignment:

  1. In the "dissection" style of our textbook, write a line-by-line dissection of the code below. Submit your dissection in a text file. Make sure to explain what each method does; explain what and why each println statement prints.
    class Fcn {
      static int foo(int a, int b) { return (a + b); }
      static int goo(int x) { return (x * x); }
      public static void main(String[] args) {
        int i = 2;
        System.out.println("foo = " + foo(i, 3));
        System.out.println("foo = " + foo(i, 4));
        System.out.println("goo = " + goo(i));
        System.out.println("goo = " + foo(goo(i), i++));
      }
    }
    

  2. Write a method that returns a letter grade corresponding to a given numerical grade on a grading scale:

    90 or above gets an A
    80 to 89  gets a B
    65 to 79 gets a C
    50 to 64 gets a D
    anything else gets an F
    

    Now use your method in a program that generates 100 random numeric grades between 30-100 and reports the number of A, B, C, D, F letter grades.

  3. It is an important skill of a programmer to be able to modify existing codes. Modify the following TwentyOnePickup.java we have discussed in class so that

    1. Program asks the user if he/she wants to go first
    2. At the end of one game, it askes the user if he/she wants to play again.
    3. EXTRA CREDIT: Report win/loss statistic at the end of each game

    This problem could take some time to get it right; think about your design before you begin to code. You can use all the methods as they are. You need to redesign main(). One way is to write two additional methods say playerFirst() and computerFirst(); these methods will be small modifications of what is the current main(). Use these methods in your new main() to drive your program. Do one thing at a time and test it before tackling the next feature.