RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

hw05
if
nesting, if-else-if

Due Mar.06 (Fri) Mar.18 (Wed).
However I encourage you to finish most or all of it before the break; we'll also have other new topics to keep us busy when we return!

  1. (10pts; if-else) Write class Employee whose constructor takes one input: the employee's hourly wage. Then, write a method calcWeeklyPay which takes in the number of hours worked in a week, and returns how much the employee should be paid. All hours over 40hrs are paid at 1.5× their hourly wage.

    Your test driver1 (which you write before the rest of the code) should test at least three situations: working less than 40hrs, working exactly 40hrs, and working more than 40hrs.

  2. (10pts) P5.2. Be sure to indent if-else-ifs appropriately!

    Note that getDescription should spell out the suit, and should spell out the rank for face cards and aces; however (unlike the book) you can use digits for 2 through 10 (rather than spelling out the rank). You may use the following test class:

    import java.util.Scanner;
    
    public class CardPrinter {
      public static void main( String[] args ) {
        Card c1 = new Card("4S");
        Card c2 = new Card("QD");
    
        // Problem #3: Draw diagram for this point in time.
    
        System.out.println( "Expect \"4 of spades\", got\"" + c1.getDescription() + "\"." );
        System.out.println( "Expect \"Queen of diamonds\", got\"" + c2.getDescription() + "\"." );
        
    
        // Now, include the interactive test call (using System.in) from the book:
        System.out.println( "Please enter a card rank and suit (e.g. `4H` or `QD` or `10C`):" );
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        Card card = new Card(input);
        System.out.println( input + " is shorthand for the " + card.getDescription() );
        }
      }
      
    You can assume that only legal card-shorthand is entered. (So you can ignore the part of the problem asking you to return "Unknown" as the book states.)

  3. (5pts) Draw a diagram of what memory looks like for CardPrinter after the first two lines of code (at the indicated comment).
    Your answer should look similar to exam01, #4b.
  4. (15pts) P5.7: Write a class TresAmigos which contains three Strings, and has methods getFirst, getSecond, and getThird which returns the alphabetically2 first, second, and third name respectively. Work carefully, and indent well -- it is easy to get lost in the various cases. Don't wait until the last moment, to start this!

    Use nested ifs3. For example, if ami1,ami2,ami3 are all Strings, your code might contain some lines like:

            if (ami1.compareTo(ami2) < 0)
            {
                if (ami2.compareTo(ami3) < 0)
                {
                    // If we reach here, we know ami1 < ami2 < ami3.
                }
                else 
                { 
                    // If we reach here, we know ami2 is last (since ami1 < ami2, and ami3 <= ami2.)
                }
            }
            else  // ami2 <= ami1.
            {
               // ... compare ami1 vs ami3, maybe?
            }
    
    (Coloring is for readability only.)

    Note that your class TresAmigos should not involve println (or, Scanner). Here is a fairly comprehensive test-class, so you don't need to write any tests yourself. (While we haven't written programs quite like that test-class so far, we're getting close; if you're curious, pore over this code to see how it works, asking your prof or a PI if you have questions.)

    BlueJ tip: Make the terminal window active, and then select Options > Clear Screen at method call. This way, every time you call a method, it will erase any old (error) messages.

1 One question I received: Should the amount paid be printed with exactly two decimal places?
Answer: Don't worry about formatting. calcWeeklyPay should return an amount of money (not a piece of text). The test-driver should check that it's giving the correct number back, but formatting the number in a particular matter is not necessary for a test program.      

2Or, as the book calls it, “lexicographically”. See the book's discussion of the String method compareTo.      

3Alternately, you are welcome to use if-else-ifs along with &&, ||, !.      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Mar.20 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme