T-Th 9:05 |
CS 1110: Introduction to Computing Using Java Spring 2012 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Main
About: Announcements Staff Consultants Times & Places Calendar Materials: Texts DrJava VideoNote Terminology Handouts: Lectures Assignments Labs Assessment: Grading Exams Quizzes Resources: CMS Piazza (link) Piazza (about) AEWs FAQ Java API Style Guide Academic Integrity |
Loop ExercisesThere is a PDF version of these instructions, if you would prefer to have that instead. The purpose of this lab is to give you some practice with assertions and with loops that process a range of integers. It is a mixture of coding exercises and answers to be written on paper. Requirements For This LabThe very first thing that you should do in this lab is to download the file Loops.java. You should create a new directory on your hard drive for this file. You will notice that this file has several (static) function stubs. Part of your lab will be to complete these function stubs. We expect you to test that your code is correct, but we will not ask you to submit a JUnit test. You are all experienced programmers now, and should understand the value of testing without having to turn in your tests each time.
For this lab you will show your instructor the contents of If you get stuck, do not waste time. Ask the TA or consultant for help. Written ExercisesQuestions on Ranges
How many values are in the following ranges? The last one requires a formula in terms of
Assigning to Range VariablesEach line below asks you to write an assignment. We have done the first one for you to give you an idea of what we are looking for.
Completing AssertionsEach line below contains an assertion P that is guaranteed to be true. Each line also contains an assertion R, which we would like to be true. In the righthand column, put a boolean expression that, when true, allows us to conclude that R is true. We have filled in the first one for you.
Preserving Invariants
Below is a precondition P, an assignment to a variable, and the same assertion
P as a postcondition. At the place indicated, place a statement so that if P
is true initially, it will be true afterward (as indicated). The statement can be in English,
if you are not sure how to write it in Java, but make it a command to do something. In
the exercises below, Problem (a)
Problem (b)
Problem (c)
Problem (d)
Coding For-Loops
Start a new folder and download
Make sure each function is correct before proceeding to the next one. Do this by writing suitable
calls in DrJava's Interactions pane or making up a Junit test class (though we do not ask that you
turn in these tests). Use enough different test cases so that you really are sure that the
function is correct. If the function uses a String value, make sure that it works on an empty
string (one whose length is 0). File You may not finish all four functions during the lab. Near the end of the lab, show an instructor or consultant what you have done. If necessary, complete the lab during the next week and show what you have done to the instructor or consultant then. Below, we show you on paper the functions you will be writing. In addition, we ask several questions for you to write on this piece of paper. Function 1Complete the following function by filling in the underlined parts. Test your function to make sure it is right.
Function 2
Type char is a primitive type (see pp. 224-226 of the class text). Characters are represented by
integers (called ASCII codes), and the cast
Questions: Function 3
Write a function with the following specification.
For example, since `e' occurs twice in ``where'', the following statement stores 2 in x:
Questions: Function 4
Write a function with the following specification.
This function could be used as follows. The call
yields the number of times a vowel occurs in “Where is it?”. Note that duplicates in s1
are to be counted as many times as they occur in s1. For example,
returns 3.
The body of this function should contain a for-loop whose repetend calls the previous function that
you wrote (the other function noOfTimes) once for each character in
Questions: |