Colorado State University

Recitation R7 - Methods and Data
Spring 2014

CS160: Foundations in Programming


The purpose of this lab is to: You are going to write and call four simple methods. The point is not to do anything elaborate, but to get you accustomed to the way that methods are defined and used.

1. Getting Started

  1. Create a new project in Eclipse called R7, and a new class called R7.
  2. You should have a main method, for now don't put any code in it.
  3. Create a variable called PI with the following format:
  4. Print the variable PI from inside your main method to show that it can be accessed.
  5. Question: Do you need to use the class name to access PI, or can you omit it?
  6. Try to assign a new value to PI. Eclipse should give you an error, this is good.
    We dont want people changing the value of PI willy-nilly now do we?

2. Write a method to calculate the area of a circle

  1. Add a new method called circleArea with the following format:
  2. It has the following conditions:
    Preconditions: Postconditions:

3. Testing the method

Anytime you finish writing a method, you should first test that it works before writing another. We can test circleArea in main by calling it with any value we choose, and comparing its return value with a hand calculated value.
  1. Call circleArea with 2.0 as its paramater.
  2. Print out the return value of this call in one of two ways:
  3. Do the hand calculation of PI * 2.02 (hint: it's 12.56637061435917)
  4. Check to see if the value printed is the same as, or reasonably close to, the hand calculated value?
    - If so, then the method probably works, and you can move on to the next method.
    - If not, then something needs to be fixed.
This is a fine approach to testing methods, however it is recommended that you try it with multiple values, to ensure that it works for not just that number, but a range of numbers as well.

4. Write a method to calculate the volume of a sphere

  1. Add a new method called sphereVolume with the following format:
  2. It has the following conditions:
    Preconditions: Postconditions:
  3. Test sphereVolume in your main method.

5. Write a method to round a value to the nearest integer value

  1. Add a new method called round with the following format:
  2. It has the following conditions:
    Preconditions: Postconditions:
  3. Use the following technique to round: Math.floor(value + 0.5)
  4. Test round in your main method.

6. Write a method to reverse the case of a string

  1. Add a new method called reverseCase with the following format:
  2. It has the following conditions:
    Precondition: Postcondition:
  3. You may want to use the Character wrapper class, which has methods such as isUpperCase(), isLowerCase(), toUpperCase(), and toLowerCase().
  4. You must use a for or while loop to complete this method.
  5. Test reverseCase in your main method.

Show your R7.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2014 CS160 Colorado State University. All Rights Reserved.