CIS110 Labs
Lab assignments are short exercises designed to get you thinking about what
you'll be talking about in lab for the week. They are
paper-based
assignments that should be turned in at the
beginning of each lab .
You will receive full credit for each lab assignment if it is clear that you
put some effort into the assignment, irrespective of its actual correctness.
You should spend 30 minutes (or less) doing each lab assignment. It is ok
if you cannot complete the assignment in time, but what you turn in should
demonstrate that you put in the 30 minutes of effort.
Lab 14 (12/7 + 12/8) - Dynamic dispatch
For the final week of labs, we're going to explore the mechanics of dynamic
dispatch. Like other reasoning problems we've encountered so far, it is
worthwhile to write things down as you are learning the concept rather than try
to keep it all in your head.
For the following classes:
pubilc class A {
public void f() { System.out.println("A.f"); }
public void g() { System.out.println("A.g"); }
}
public class B extends A {
public void f() { System.out.println("B.f"); }
}
And the following declarations:
A a1 = new A();
B b = new B();
A a2 = new B();
Fill out the following table of variables and method invocations with the
results of that method being called on that variable:
Turn in this table at the beginning of section.
Lab 13 (11/30 + 12/1) - Making blueprints
In lab, we will practice the basics of designing classes. Recall that
classes are blueprints for objects. Objects in turn contain state (fields) and
behavior (methods).
Imagine that you are writing a Nintendogs + cats clone. A
reasonable design might involve writing a class that represents the pet the
player takes care of in the game.
Please sketch out either a Dog or Cat class (your preference) that you might
use for this program. In your sketch, you should call out in bulleted-list form
the state and behavior of your class. You do not need to write any code, but
for the state, please include the type and name of the field. And for methods,
please include the name and a brief (one sentence) description of what the
method does.
For example, to get you started, you might start your sketch out for a Cat
class like this:
State:
Behavior:
meow: makes the cat meow (to the console)
In your design, you should include at least 3 pieces of state and 3 pieces
of behavior. In lab, we'll talk about how we take sketches like this and
translate them into code.
Lab 12 (11/23 + 11/24) - No lab
Exam #2
Lab 11 (11/16 + 11/17) - Midterm #2 review
Time to prep for the second midterm! Please review the sample midterm
briefly and write down the one question you most want to answer in section about
it.
Lab 10 (11/9 + 11/10) - Array Traversals
In lab this week, we are drilling hard on how to traverse and manipulate
arrays. As warm-up, consider the following problem:
Suppose we want to write a method shiftRight(arr) that takes an
array of ints and shifts all the elements of that array one position to the
right. The first element of the array becomes 0. The last element of the array
falls off the end of the array (i.e., is lost). For example if we called
shiftRight on the following array:
[1][2][3][4][5][6][7][8][9]
Then the result should be:
[0][1][2][3][4][5][6][7][8]
Now consider the following code that attempts to implement shiftRight:
public static void shiftRight(int[] arr) {
arr[0] = 0;
for (int i = 0; i < arr.length - 1; i++) {
arr[i+1] = arr[i];
}
}
Answer the following questions about this implementation:
Does this method work?
If not, what is broken about it?
We'll start off lab by discussing the answers to these questions.
Lab 9 (11/2 + 11/3) - Representing Data in Files
So far in the class we've focused on control issues in programming.
However, we've talked relatively little about the data we manipulate and
how we represent is in our programs. As we learn this week, files are one way
to organize large amounts of data. However, in order to access this data
effectively, we need to be mindful of how we layout our data in files.
In lab, we'll practice our file input and output skills, but as a warm-up
lets consider how we might arrange a file ourselves given a set of data. For
example, consider a file that contains a series of student and faculty records.
Each record contains the following information:
PennID #
PennID username
Student or faculty
If the record belongs to a student, then it also contains the following data:
If the record belongs to a faculty member, then it also contains the
following data:
Write down how you would layout this information in a file. Each line of
your file should contain a single record, so you should describe the components
of the record on each line including their expected types. For example, if the
file contained only PennID #s and GPAs, then I would specify something like:
<PennID #> <gpa>
where PennID # is a String and gpa is a double separated by a
single space.
Since there are two kinds of records, you will need a "tag" of sorts in your
record to differentiate between the two kinds.
Lab 8 (10/26 + 10/27) - Programming Assertions
Understanding programming assertions and establishing invariants is a
critical part of programming in an imperative style , i.e., where we can
change the contents of variables.
Consider the following piece of code:
Scanner in = new Scanner(System.in);
int val = 0;
for (int i = 0; i < 10; i += 2) {
// POINT A
val += i * 2;
}
val = val * in.nextInt();
// POINT B
Answer the following questions about the code fragment above, choosing one
of always , never , or sometimes for each part. You'll
discuss the results in labs along with other problems similar to this.
At point A:
Is i % 2 == 0 always/never/sometimes true?
Is i < 6 always/never/sometimes true?
Is val < 0 always/never/sometimes true?
At point B:
Is i < 10 always/never/sometimes true?
Is val >= 0 always/never/sometimes true?
Lab 7 (10/19 + 10/20) - Tackling Programming Problems
Sketch a solution to the following programming problem. Like the previous
problems we've tackled, you shouldn't start coding immediately, instead you
should take a step back, analyze the problem, and decompose it . Your
sketch should be an outline of how you break down this problem. The format of
the sketch is not particularly important. However, your sketch should call out
what static methods you need to make to solve the problem.
In section, you will take your sketch and create a complete solution from
it.
This program converts celsius to fahrenheit and vice versa.
Are you providing a celsius or fahrenheit value [C or F]? C
Provide the degree value to convert. 0
The converted amount is 32.0.
===
This program converts celsius to fahrenheit and vice versa.
Are you providing a celsius or fahrenheit value [C or F]? F
Provide the degree value to convert. 120.50
The converted amount is 49.166666666666664.
Lab 6 (10/12 + 10/13) - Post Exam #1 survey + dealing with user input
Please fill out the post-exam #1 survey found on SurveyMonkey .
Each of the following code snippets requests input from the user. Based off
of the names of the variables involved, come up with a list of possible "bad
inputs" that the user might provide. That is, create of list of example
inputs from the user that would be accepted by the code but would not be
"correct" in some sense. For each such bad input, suggest a check you could
write to detect such input.
For example, if we requested the age of the user, a bad input would be a
negative number like -50. The check we could make would be to ensure that age >
0.
Scanner in = new Scanner(System.in);
int monthOfBirth = in.nextInt();
Scanner in = new Scanner(System.in);
double amountToWithdrawFromBankAccount = in.nextDouble();
Scanner in = new Scanner(System.in);
String rockPaperOrScissors = in.nextLine();
Lab 5 (10/5 + 10/6) - No lab assignment
Exam #1
Lab 4 (9/28 + 9/29) - Method mystery
Assigned: Monday, September 26
Write down the output of the following program. (Note that this is problem
3.5 from the text if you're looking for more practice for the exam!)
public class MysteryTouch {
public static void main(String[] args) {
String head = "shoulders";
String knees = "toes";
String elbow = "head";
String eye = "eyes and ears";
String ear = "eyes";
touch(ear, elbow);
touch(elbow, ear);
touch(head, "elbow");
touch(eye, eye);
touch(knees, "Toes");
touch(head, "knees " + knees);
}
public static void touch(String elbow, String ear) {
System.out.println("touch your " + elbow + " to your " + ear);
}
}
Lab 3 (9/21 + 9/22) - Tabulate your loops
Assigned: Monday, September 19
Trace the execution of these loops.
// Loop #1
for (int i = 1; i < 256; i *= 2) {
System.out.println(i); // HERE
}
// Loop #2
for (int i = 0; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.println(i - j); // HERE
}
}
Create a table (ala lecture) for each loop showing the value of the loop
variables (i in #1, i and j in #2) and the text printed after executing the line
marked "HERE".
Lab 2 (9/14 + 9/15) - Redundant redundancy
Assigned: Wednesday, September 12
Here is a diagram similar to the ones we'll be drawing initially in lecture
and lab:
+-----+
| |
| |
+-----+
+-----+
|~~~~~|
|~~~~~|
+-----+
+-----+
| |
| |
+-----+
How can we can decompose this problem into multiple static methods? Do not
create the actual program and hand it in at lab time (although if you have the
time, it's a good practice problem). Instead, imagine the static methods you
might make to solve this problem, the method calls you would make in your main
method, and list those instead. For example, if you envision that your program
would require a method called foo twice, that would in turn call a method
bar , then you would write:
Please turn in this bulleted list of method calls at the beginning of lab.
Lab 1 (9/7 + 9/8) - No lab assignment
First week of class.