Recitation R7
- Methods and Data Scope
Fall 2013
CS160: Foundations in Programming
The purpose of this lab is to:
- Introduce you to the basic use of methods
- Get experience with the format of constructing a method
- Show how some variables can be accessed beween scopes, and some cannot.
You are going to write and call 4 simple methods. The point is not to do anything elaborate,
but to get you accustomed to the way that methods are written, called, and used.
0. Create new project
- Create a new project in Eclipse called R7, and a new class called R7.java.
- You should have a main; for now, don't put anything in it.
1. Create a global variable for pi
- Create a variable called pi with the following format:
- It is public, so "anyone" can use it
- It is static, which means that it doesn't need an object to be called
- It is
final
, which means that it cannot be changed after it is initialized
- It is a double value
- It goes to at least 5 decimal places
- Use the variable
pi
inside your main
method to check that it can be accessed, but not changed
- Print out the value of
pi
to see if it can be accessed from the current scope.
- 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
- Add a new method called circleArea with the following format:
- It is a public method, so "anyone" can use it.
- It is static, which means that it doesn't need an object to be called.
- It returns a double value to the calling function.
- It takes one parameter of type double.
- It has the following conditions:
Preconditions:
- the double is the radius of the circle.
Postconditions:
- The method returns the area of the circle as calculated by π*r2
3. Testing a 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.
- Call circleArea with 2 as its paramater.
- Print out the return value of this call in one of two ways:
- Assign the return value to a variable, then print that variable
or
- Make the circleArea call within the print statement
- Do the hand calculation of π*22 (hint: it's 12.56637061435917)
- 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 circumference of a circle
- Add a new method called circleCircumference with the following format:
- It is a public method, so "anyone" can use it.
- It is static, which means that it doesn't need an object to be called.
- It returns a double value to the calling function.
- It takes one parameter of type double.
- It has the following conditions:
Precondition:
- the double is the radius of the circle.
Postcondition:
- The method returns the circumference of the circle as calculated by 2*π*r
- Test circlCircumfrence in your main method
5. Write a method to calculate the volume of a sphere
- Add a new method called sphereVolume with the following format:
- It is a public method, so "anyone" can use it.
- It is static, which means that it doesn't need an object to be called.
- It returns a double value to the calling function.
- It takes one parameter of type double.
- It has the following conditions:
Preconditions:
- the double is the radius of the sphere.
Postconditions:
- The method returns the volume of the sphere as calculated by (4/3)*π*r3
- Test sphereVolume in your main method
6. Write a method to calculate the area of a triangle
- Add a new method called triangleArea with the following format:
- It is a public method, so "anyone" can use it.
- It is static, which means that it doesn't need an object to be called.
- It returns a double value to the calling function.
- It takes two parameters, both of type double.
- It has the following conditions:
Precondition:
- the first double is the base of the triangle.
- the second double is the height of the triangle.
Postcondition:
- The method returns the area of the triangle as calculated by (1/2)*base*height
- Test triangleArea in your main method
7. Write a method to round a value to the nearest integer value
- Add a new method called round with the following format:
- It is a public method, so "anyone" can use it.
- It is static, which means that it doesn't need an object to be called.
- It returns an int value to the calling function.
- It takes one parameter of type double.
- It has the following conditions:
Preconditions:
- the input is a positive value.
Postconditions:
- returns the value rounded to the nearest integer.
- Use the following technique to round:
Math.floor(value + 0.5)
- Test round in your main method
Show your R7.java program to the TA for grading and submit to RamCT to get credit for this lab.
© 2013 CS160 Colorado State University. All Rights Reserved.