DOC PREVIEW
STEVENS CS 115 - CS 115 Honework 6

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 115 homework 6, due 20 Oct 2008This homework should be done in groups of two students. Contact TA or instructorin advance if this is a problem for you.Objectives• Practice designing a program “from scratch” based on requirements.• Practice developing loops.• Become familiar with “batch processing” program architecture, i.e., transform-ing an input file to an output file.• Get used to relying on preconditions (when appropriate).• Use new submission proces s which is intended to be easier for students and alsoto facilitate rigorous testing.Follow the instructions carefully. If you have a problem with the new submissionprocess, discuss it in advance (on the mailing list, with TA, with instructor). Inreal life, programmers have to conform exactly to specified interfaces, or negotiate tochange the interface.ReadingRead Chapter 7. I strongly suggest working through it in PL first. Then go back andread carefully through the textbook version.The workImplement a c lass named HW6app with a main method that works as follows. It readsan input file1named HW6data.txt and writes an output file named HW6out.txt.Assume the input is a series of command lines as described below. For each commandline, print the line to the output file, followed by a response line.The five commands correspond to exercises E2, E3, E4, E6, and E12 on pages266–8 of the textbook. Your code must use loops with the invariants specified in theexercises. Include the invariant as a comment.Assume that the input file exists and has the proper format, including precondi-tions ab out the particular data values.Below are the possible command lines and their corresponding responses. Youmight want to glance at the sample input file (on page 4 of this assignment) beforereading further.1The file should contain plain text. Use drJava to create your test files. Or you can use MicrosoftWord but then save as “Text Only with Line Breaks”.1isDivisible: This command has the formisDivisible n first lastwhere n, first, and last are integers and first≤last. The response must look like--> trueor else--> falsedepending on whether n is divisible by some integer in the range from first to lastinclusive.This is based on Exercise E2. The Exercise specifies four different possible invari-ants to use . Pick any one, and make sure your loop is correct for that invariant. Theinvariant you use should appear as a comment.What ab out this input?isDivisable 7 3 2It violates the precondition that first should be at most last, and the commandword “isDivisible” is mis-spelled. Your program is not responsible for handlingthis input. It can even turn your computer into a flamingo, it doesn’t matter.2largestPow: This command has the formlargestPow nwhere n is an integer greater than 0. The response must look like--> kwhere k is the largest power of 2 that is at most n.See E3 —but note that there is a typo in the book. The condition P in E3 shouldbe 1 ≤ 2k≤ n, that is, 2 to the kth power, not 2 times k. (By the way, in codecomments, people usually write 2^k to indicate an exponent.)quotientRem: This command has the formquotientRem x ywhere x is a non-negative integer and y is a positive integer. The response must looklike--> quo = q rem = rwhere q and r are as specified in E4. Also, your code must not use the built-indivision or multiplication operators; otherwise it would not be a loop exercise.2Good software engineering absolutely requires thorough checking of input data. But it is quitewrong to check at every interface. And there are several di fferent ways to check preconditions. Youcannot learn good design for error-checking until you get comfortable with the making assumptions.2deleteVowels: This command has the formdeleteVowels swhere s is a string. (The entire string, up to the end of the line.) This is E6. Consider’y’ to be a vowel.compoundInterest: This is E12. The command has the formcompoundInterest init rate yearswhere both init and rate are of type double and years is an integer. Preconditions:years is in the range 1..30. The initial balance, init, is positive. And rate is positive.Now is a good time to start getting familiar with the online documentation forJava. I suggest that you start here:http://java.sun.com/javase/6/docs/api/In the index of “all classes” you c an find class Double which contains a method calledparseDouble that is useful. (It’s similar to Integer.parseInt describ ed in section5.2.4 of the textbook.)Now is also a good time to read section 6.4.1 about type double. Floating-point computations are inexact, and sometimes that matters a lot. For example, incalculations performed by a spaceship during a long voyage. But for this homework,you may ignore the issue.Our test data will not have astronomical interest rates or duration. The examplebelow uses 10% interest for 20 years. Changing it to 50% interest results in a finalbalance of 3.325256730079651E7. Note that Java uses “scientific notation” whenprinting large values of type double. For this homework, do not round off the valueor cast it to a different type.HintsA straightforward design is to use a loop in the main procedure. The loop reads oneline at a time and decides what to do based on the command name. For each kindof command, define a function to do the actual computation. For example, for thedivisibility command, your function might look as follows:/* = whether n is divisible by some integer in the range first..last,assuming first<=last */boolean isDiv(int n, int first, int last)The implementation of this function will consist of a loop (with initialization)followed by a return statement. You aren’t required to write out the checklist fordeveloping a loop, but you DO need to make sure the loop is correct in terms of aparticular invariant that you state as a comme nt. Always keep in mind the four loopyquestions (book p265).3Sample input and outputHere is a sample input file.isDivisible 17 2 10isDivisible 16 2 10isDivisible 16 7 9largestPow 5largestPow 65deleteVowels cat in the hatquotientRem 10 5quotientRem 12 5deleteVowels funny$input * * squirrels@playcompoundInterest 10000.0 0.1 20compoundInterest 50000.0 0.2 30Notice that the different kinds of commands may be in any order. For this inputthe output should look exactly as follows, except that my explanatory commentsshouldn’t be thereisDivisible 17 2 10--> false (the divisors of 17 are 1 and 17)isDivisible 16 2 10--> true (the divisors of 16 include 2, 4, and


View Full Document

STEVENS CS 115 - CS 115 Honework 6

Download CS 115 Honework 6
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 115 Honework 6 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 115 Honework 6 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?