Returns & if/else Lab Worksheet

posted by: Mr. Bergquist 31 October 2011 No Comment

[For your reference: Returns & Conditionals (if/else) Presentation PDF]

Start with some Practice It! warm ups…
Returns:
Self-Check 3.12: min : Write a Method that takes three integers as parameters and returns the smallest of the three values (you might be able to use if/else’s for this)
Self-Check 3.13: countQuarters : Add a Return to the Count Quarters method you wrote earlier
if/else Conditionals:
Exercise 4.9: printTriangleType : Print the type of a Triangle based on the lengths of its three sides
Exercise 4.3: season : Returns the season of a date

Then in JGrasp…

  1. Modify your circles programfrom Math and Scanner practice to allow the user to color the circles. Give the user three choices:
    Pick a color: (1) red, (2) blue, (3) magenta.

    Draw a red circle when the user types 1, a blue circle when the user types 2 and a magenta circle when the user types 3. If the user types in an invalid number, print an angry message and draw a black circle.

  2. Write a method that accepts a real number for a programmer’s hourly wage and an integer for the number of hours the programmer worked this week, and returns how much money to pay the programmer. For example, the call pay(50.0, 6) should return 300.

    The programmer should receive “overtime” pay of 1 ½ normal wage for any hours above 8. For example, the call pay(35.0, 11) should return 35 * 8 + (35 * 1.5 * 3 or 437.5.
  3. Write a method that accepts a real number for a programmer’s hourly wage and returns a yearly salary assuming 48 weeks of work in a year and 8 hours of work in a day. Round the result to the nearest dollar. Make sure to use your previous method! For example, the call salary(35.0) should return67200.
  4. Put it all together! Your program should ask for two people’s hourly wage and compare the taxes they will be paying. Your output should be as follows (user input underlined):
    Person 1's hourly wage: 35
    You will make $67200 this year.
    And you will pay $13440 in taxes.
    
    Person 2's hourly wage: 20
    You will make $38400 this year.
    And you will pay $7680 in taxes.
    Person 1 is paying more taxes

    People with a yearly salary under $20,000 do not pay taxes. Those making over $100,000 pay 30% in taxes. Those making between the two pay 20%.

Pick some of these if you have the time…

In a separate class, do as many of these as you can! Alternately, play with what we have learned so far. Can you come up with a clever program to write?

  1. Write a graphics programs that prompts the user for the position and radius of two circles. If the two circles overlap, draw them filled in green. If they don’t, draw them filled in red.
  2. Write a method that counts a number’s factors and determines whether the number is prime. Use user input as follows:
    What is your favorite number? 24
    24 has 8 factors
    24 is not prime
    
    What is your favorite number? 31
    31 has 2 factors
    31 is prime
  3. Continue working in Practice-It and try to beat the guy from Green River Community College who has solved 882 problems.

(Big Thanks to Ms Martin for these excellent sample problems)

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.33 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>