CS 171 & 171L Introduction to Computer Science I & Lab Fall 2019 

Lab 2 - Java Methods

The purpose of this lab is to get you experimenting with and using Java methods. Whatever you do not complete in lab today, you should try and complete on your own or with your lab partner in the next few days.

Rules For The Lab

You will play two different roles in the lab. You will either be the driver or navigator.
  • driver: The person typing at the keyboard.
  • navigator: The person watching for mistakes, making suggestions, and thinking ahead.
Important: The navigator must not touch the keyboard or the mouse. If the navigator either types or moves the mouse, the navigator will get a zero for the lab.

You have been assigned identifiers s1 and s2 when you entered the lab. At the start of the lab, s1 will be the driver and s2 will be the navigator. Your roles will switch during the lab.

Warmup

Your first task is to introduce yourself to your lab partner. Tell your lab partner your name and how your semster is going so far.

The driver must start Drjava now. After every step, switch roles between driver and navigator.

Tasks

  1. Java allows one to create function which combine one or more Java expressions. For example, if we wanted to create a function named twice that doubles the argument sent to it, we can use the following:
    int twice(int x) {return x*2;}
    
    This method takes a single int parameter and returns an int. the keyword return sends out the computation. Enter this into DrJava's interactions pane. Verify it works using twice(3).
    Often we want to save methods for later use. Using the interactions pane does not save variables, expressions, or methods. To do this, we need to use the Editing Pane. Place the following code in the editing pane:
    // Your names go here
    public class Lab2 {
       public static int twice(int x) {return x*2;}
       }
    
    Save the file as Lab2.java and then compile it. We will talk about the keywords public and static later in the semester, but you need them for now. You can now run the twice method from Lab2 in the interations pane by using:
    Lab2.twice(5)
    
    which should return 10. We will repeat these steps for the remainder of the lab to create some interesting methods. Be careful that you use the correct case. Finally, change the first line in the lab file to reflect the names of the lab partners who worked together on this lab.
  2. Create a method named f2c that takes a single double parameter representing a fahrenheit temperature, converts it to celcius, and returns a double representing this value. Use the formula c = (f-32)*5.0/9.0;
  3. Create a method named c2f that takes a single double parameter representing a celcius temperature, converts it to fahrenheit, and returns a double representing this value. Use the formula f = 9.0*c/5.0 + 32.
  4. Create a method named lb2kg that takes a single double parameter representing a weight in pounds, converts it to kilograms, and returns a double representing this value. Use the formula kg = lb / 2.2.
  5. Create a method named kg2lb that takes a single double parameter representing a weight in kg, converts it to pounds, and returns a double representing this value. Use the formula lb = kg * 2.2.
  6. The polynomial p(n) = n2 + n + 41 gives primes for n=0 to 39. Write a method named primes that takes a single int parameter representing n and returns an int representing the computation of this function.
  7. Create a method named area that takes a single double parameter representing the radius of a circle, computes its area, and returns a double representing this value. Use the formula a = pi r2.
  8. Create a method named volume that takes a single double parameter representing the radius of a sphere, computes its volume, and returns a double representing this value. Use the formula v = (4 pi r3)/3.
  9. Write a method named day that takes four parameters: q is the day of the month; m is the month (3 = March, 4 = April, 5 = May, ...). January and February are considered the 13th and 14th months of the previous year; K the year of the century (0, 1, ..., 99); and J is the leading two digits of the year. Use this to compute the day of the week using Zeller's algorithm, h = (q + (26 (m+1))/10 + K + K/4 + J/4 + 5 J) mod 7.
  10. Java allows us to set the value of a variable based on a conditional value using the ternary operator. type the following into the interaction pane:
    int a = 1;
    int b = 2;
    int c;
    c = (a < b) ? a : b
    
    Observe the values of c and d. Because a=1 is less than b=2, c gets assigned the value of a=1. We can use this operator in many ways to select between two options.
    The () portion of the ternary operator is a boolean expression that is either true of false. There are several boolean operators that can be used to compare values:
    ==	equal to
    !=	not equal to
    >	greater than
    >=	greater than or equal to
    <	less than
    <=	less than or equal to
    

    Write a method named collatz that takes a single int as input and returns an int c as follows. Using the ternary operator, write a Java expression that assigns c the value of b/2 if b is even or 3*b+1 if b is odd. Hint: use the % operator.
  11. Write a method named zeller that takes three parameters: dd is the day of the month; mm is the month (1 = January, ...); yy is the year. Use the :? operator to compute m (the Zeller month 3-14) using mm. Use the :? operator to compute y (the zeller year, possibly one less than the current year) using mm and yy. use the mod operator to compute K from y. use the / operator to compute J from y. Finally, use these values to compute the day of the week using Zeller's algorithm, h = (q + (26 (m+1))/10 + K + K/4 + J/4 + 5 J) mod 7.
    Use multiple lines in this method, one line for each intermediate computation.
  12. Methods do not always need parameters or have a return value. For example, the main method (public static void main(String [] args) {...}) returns nothing, so its return type is void. The method
    void twoByTwo() {System.out.println("**\n**");}
    
    takes no arguments and prints a 2x2 grid of stars. Write a method named triangle that takes no parameters and prints a triangle that is five rows tall. You can be creative with the other aspects.
  13. The sum of the first n positive integers is given by the formula n(n+1)/2. Write a method called sum that takes a single int parameter and returns this value.
  14. Write as method called root1 that takes three double parameters (a, b, c) representing the coefficients of a quadratic p(x) = a x2 + b x + c and returns the root using the quadratic formula: r1 = (-b + sqrt(b2 - 4 a c )/ (2 a)
  15. Write as method called root2 that takes three double parameters (a, b, c) representing the coefficients of a quadratic p(x) = a x2 + b x + c and returns the root using the quadratic formula: r2 = (-b - sqrt(b2 - 4 a c )/ (2 a)
  16. Write a method named sum3 that takes three double parameters and returns the sum of them.
  17. Write a method named max that takes three int parameters and returns the value of the largest of the three.
  18. Write a method named min that takes three int parameters and returns the value of the smallest of the three.

Send your intructor the Lab2.java file as an email attachment (oen per group). Also make sure each lab partner has a copy. You are now done with the lab! Take a well deserved break.


Copyright © 2019, David A. Reimann. All rights reserved.