CS256: Lab 4


Due: See Moodle for the due date and time

Goals:

More practice with nested loops and nested if statements.

Learn to create and call your own methods.

Learn to use constants.

Practice using variables with of all three scopes: instance, parameter, and local.

Requirements:

 0.  Get started by doing the following:

·         Create a directory to store your lab4 files, e.g., P:\cs256\lab4

·         Open demos/l4Starter.zip and extract the files Sums.java and Bank.java into this directory.

·         Follow the instructions for running the autograder found at http://cs.sou.edu/~nordquip/ag/ag.html.

 

If you download ag.jar and tools.jar to P:\cs256, and unzip the lab4Starter files into P:\cs256\lab4, just before you click the submit button, the first page of the autograder should look like:

 

After you click submit, you should see a screen that looks like:

 

At this point, select lab 4, enter your normal SOU user login and click submit again.

The autograder will compile and run the lab4 starter code and give you a score.  Not a very good one, but it is a successful run.

 

1. Write a class named Sums that contains a public method named sumOfWholeNums that accepts one parameter, num, whose type is int then calculates and returns a value, whose type is int, that is the sum of the whole numbers from 1 to numsumOfWholeNums may assume the input parameter num is a positive whole number.

 

2. Write the method main in class Sums, so that it does the following in a loop:

a) Reads an int from the user.

b) If the value read is greater than or equal to 1, calls the sumOfWholeNums method and prints the value returned by sumOfWholeNums in the following format:

Sum from 1 to <number Read From User> is <result Calculated By sumOfWholeNums>

 

If the value read from the user is < 1, main() must exit the loop and print the following message exactly as shown:

All Done

 

If the user enters the numbers 5 then 0, the output from main should be:

Sum from 1 to 5 is 15

All Done

 

Here is a sample run of my code running Sums.main:

  

The autograder requires you use TextIO.java, which you should save into the same directory where you saved Grades.java and use to read doubles from the user.

 

You may print anything you like using System.err.println(), but, in order to pass the autograder, you must print exactly the required output and nothing else using System.out.println().

 

 

Hints: main() does not check to see that the number is a whole number.  In this lab, main() may assume that the user will be well behaved and enter a whole number.

sumOfWholeNums() should not print anything.

 

3) Write a class named Bank that contains one method loanCost that accepts three parameters, in the following order: 1) a double  -- the amount of the loan principal, 2) an int -- the number of months a payment is to be made (the period), and 3) a double the annual interest rate.  The method should calculate and return the cost of the loan (the difference between the total amount to be paid back to the bank and the principal).  The formula is:

 

cost = period * payment - principal

 

where payment is the monthly payment.  The formula below shows how to calculate the monthly payment, but, in this formula, rate is the monthly interest rate, not the annual interest rate.  The monthly interest rate is simply the annual interest rate divided by 12.

 

Here are a couple of examples you can use for testing:

principal = 1000, period = 1, annual rate = .065, (monthly rate = .065 / 12), cost = 5.416666666682659

principal = 1000, period = 24, annual rate = .065, cost =  69.1100344042718

 

4) The Bank class written for requirement 3) above must contain a private constant double MAXLOANAMT that has the value 330000.0.  loanCost() must return 0.0 if the requested principal is greater than MAXLOANAMT.

 

Hints: The Bank class does not need a main method, though you may write one to test your loanCost() method.

You need the following method from the Math class, which we will talk about tomorrow in class:

Math.pow(number, power_to_raise_number_to)

No printing at all is required for loanCost()

 

5) In a comment at the top of your Bank class, write answers to the following synthesis questions:

a) Suppose my class contained the following method header:

public static int transformAValue( double d)

Show a java statement(s) that would declare a variable, call this method, and assign the return value to the variable.

 

b) Why might it be a good idea to write and call a separate method to accomplish a task instead of writing the code to accomplish this task directly in your main method?

 

c) When should I declare and use a constant instead of simply writing the literal value the constant represents in my methods?

 

 

6) Thoroughly test your methods, then turn in your lab using the autograder

The autograder advertises what your program’s inputs and outputs should be.  All outputs that the autograder advertises that it is expecting must be printed using System.out.println().   All other outputs, e.g., prompting the user for input must be printed using System.err.println() in order to pass the autograder.

 

Note:  The autograder will give a score of up to 100 for the ‘functional’ portion of the lab.  From this score, I will remove points for: missing name at the top of the code, poor indentation, missing or incorrect synthesis question answers.