Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective The purpose of this lab is to: Introduce you to the basic use of methods. Get experience with defining and implementing methods. You are

Objective

The purpose of this lab is to:

  • Introduce you to the basic use of methods.
  • Get experience with defining and implementing methods.

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 R10, and a new class called R10.
  2. You should have a main method, for now don't put any code in it.
  3. The TA will show you how to create and test the first method below.
  4. Write the other 3 methods and test them.
  5. Show the results of the test below to the TA.

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

  1. Add a new method called circleArea with the following format:
    • It is a public method, so "anyone" can call 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.
  2. Preconditions:
    • The parameter is the radius of the circle.
  3. Postconditions:
    • The return value is the area of the circle as calculated by PI * radius2

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 parameter.
  2. 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
  3. Do the hand calculation of PI * 2.02
  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:
    • It is a public method, so "anyone" can call 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.
  2. Preconditions:
    • The parameter is the radius of the sphere.
  3. Postconditions:
    • The return value is the volume of the sphere as calculated by (4.0/3.0) * PI * radius3
  4. 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:
    • It is a public method, so "anyone" can call 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.
  2. Preconditions:
    • The parameter is a positive double.
  3. Postconditions:
    • The return value is the parameter rounded to the nearest integer.
  4. Use the following technique to round: Math.floor(value + 0.5)
  5. 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:
    • It is a public method, so "anyone" can call it.
    • It is static, which means that it doesn't need an object to be called.
    • It returns a String value to the calling function.
    • It takes one parameter of type String.
  2. Precondition:
    • The String parameter is not equal to null.
  3. Postcondition:
    • The method returns the same String, but uppercase letters have been made lowercase and vice versa
    • Digits and special characters should not be changed
  4. You may want to use the Character wrapper class, which has methods such as isUpperCase(), isLowerCase(), toUpperCase(), and toLowerCase().
  5. You must use a for or while loop to complete this method.
  6. Test reverseCase in your main method.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data With Hadoop MapReduce A Classroom Approach

Authors: Rathinaraja Jeyaraj ,Ganeshkumar Pugalendhi ,Anand Paul

1st Edition

1774634848, 978-1774634844

More Books

Students also viewed these Databases questions