JamesMadisonUniversity

Computer Science Department


CS 139 Lab: Method Play


Objectives:

This lab will give students a chance to practice writing method bodies.

Background:

Methods allow us to break a problem up into smaller units. Each of these methods will do a single task. In some cases the task is to calculate and print a value; in other cases, its task is to do a calculation and return a new value.

New Terms:

stub
an empty method that has the header and return statement (if needed). Used to structure a program and allow for each method to be built individually while having a tester ready to test all of the code. Also used to allow one coder to work on the calling program while another works on the methods themselves.
driver
a program developed to test methods or to control the operation of an application. Drivers usually just contain a main method.

Materials:

All of these methods should be written from scratch. All should be stored in a .java file named Methods.java. You may download the driver to use to submit your program (Driver.java). All programs should be submitted to the Linux submit system. The Driver uses a command line argument to determine which method it is testing.
Acknowledgment From an original lab by Nancy Harris

1 General Instructions:

Download the Driver.java and the Methods.java files to your programming environment.

Methods.java is a set of stubs for each of the methods. A stub is simply the header and if necessary a return statement. These stubs are designed to let you compile the class and add code in as you develope it. The Driver includes all of the code needed to "exercise" each of the methods. You may create and test them one at a time. Driver takes in an argument corresponding to the number of the method you are testing (1 is reformName for example). You can test one method by typing the number in as an argument.  The instructor will demo the command line argument in JGrasp.

You may tackle problems in any order that you choose. You will likely see a variant of one of these problems on the upcoming exam.

Thoroughly test your problem.

When you are convinced that you have the correct solution, submit to stu with the Driver.java program. There will be one submit for each problem.

You get a grade of 65 for one correctly solved problem, 75 for 2, 85 for 3 and 100 for correctly solving four problems.

You do not need to print anything nor do you need to upload anything to Blackboard. Your grade will be based on the number of correctly solved problems in submit.

2 The Problems - you do not need to do any of these in any particular order.

Many of these  problems have come from the Programming Challenges section of chapter 2 in your book.

  1. Write a method called reformName that will accept a lastName, firstName, and middleName as Strings and return a new String which is formatted as: lastName, firstInitial.middleInitial. So if the name were passed as Harris, Nancy, Lea, the method should return Harris, N.L.  See the String methods in chapter 2 for help. 

  2. Write a method called calcAcreage which takes in a double representing the square footage of land and returns a double representing the number of acres. For this program, assume that 1 acre is 43,560 square feet.

  3. Write a method called calcStockPrice which takes in a number of shares as an integer and the price per share of the stock and returns the total price of the shares.

  4. Write a method called calcStockCommission which takes in a number of shares as an integer and the price per share of stock and returns the commission. The commission is a constant 2% for all transactions.

  5. Write a method called showChar that takes in a String and a position (0 - the length of the String) and prints the corresponding character followed by a newline character.  NOTE: The character should be in the leftmost position of the line and there should be no space after the character.

  6. Write a method called sumSquares which takes in two double numbers and returns the sum of the two numbers squared.

  7. Write a method called insertValue which takes in two String parameters and one integer value and returns a String that is the integer value embedded inside of the two Strings. For example, if the Strings were "%." and "f\n" and the value were 3, the method would return "%.3f\n".

  8. Write a method called calcTax which takes in a double number and returns the total tax on that amount. Use a tax rate of .05 in your program.   

Updated 09/29/10 (nlh)