CISC 1115
Introduction to Programming Using Java
Lab #7
Techniques I
Lab 7.1 — Family Tree (FamilyTree)
Write a program that reads four names (a parent and three children) from a file named
names.text
(using a 1 to 4
for
loop). The first name is the name of the parent and the next three names are the names of the children.
Using first-time logic, the program should print the names in a manner that indicates the parent/child relationship (see below).
Sample Test Run
For example, if the file names.text
contains:
Jim
Bob
Carol
Ted
the program should produce the following output:
Parent: Jim
Bob
Carol
Ted
This is an example of first-time logic -- the 'Parent:' prefix is only printed for the first name.
Lab 7.2 — Family Tree — Take 2 (FamilyTree) (Approval)
Write a program that reads four names (a parent and three children) from a file named
names.text
(using a 1 to 4
for
loop). The first name is the name of the parent and the next three names are the names of the children.
Using (not) first-time logic, the program should print the names in a manner that indicates the parent/children relationship (see below).
(The whitespace at the begiining of each child's line conists of three blanks.)
Sample Test Run
For example, if the file names.text
contains:
Jim
Bob
Carol
Ted
the program should produce the following output:
Jim
Bob
Carol
Ted
This is an example of 'not' first-time logic -- the indentation is only performed from the second name onwards. (Again, this involves
logi similar to first-time, in the sense of setting an isFirst boolean, but the actual action is only taken when isFirst is false.)
Lab 7.3 — Right Rotation (RightRotation)
Write a program that reads in four Strings from the keyboard, performs a right rotation on them, printing the result.
Sample Test Run
s1? Alpha
s2? Bravo
s3? Charlie
s4? Delta
Right rotation: Alpha Bravo Charlie Delta -> Delta Alpha Bravo Charlie
We did left rotation in class; you get to do right rotation.
Lab 7.4 — Left Shift (LeftShift)
Write a program that reads in four boolean values from the keyboard, and performs a left shift on them, printing the result.
A filler of
false
should be used.
(The Scanner
method used to read in a boolean is … ??? … see if you can guess / figure it out / look it up on the net /
ask a friend).
Sample Test Run
b1? true
b2? false
b3? true
b4? false
Left shift: true false true false -> false true false false
We did right shift in class; you get to do left shift
Lab 7.5 — 'Sorting' 3 numbers (SortOf3)
Write a program that reads in three integers from the keyboard into the variables
x
,
y
, and
z
(respectively), 'sorts' them so that
x
<
y
<
z
, and prints the result.
You may assume the three integers are unique. (
Hint: Rather than haphazard comparing of the numbers against each other, you might find
it easier to try finding the maximum value of the three numbers and swap it with the third number; then all you have to do is
compare the first two numbers and put them in the correct order.)
Sample Test Run
i1? 2
i2? 1
i3? 3
Sort: 2 1 3 -> 1 2 3
This exercise anticipates the sorting agorithms we will be presenting towards the end of the semester. Anything less than three elements
is trivial, and anything greater than three is too painful, given our current state of knowledge. With three numbers, you get to
see some of the issues facing us when sorting, and hopefully you will get some insights into the challenges facing us when sorting a general
sequence.
Lab 7.6 — Brute Force Max of 4 (MaxOf4) (Approval)
Write a program that reads in four integers from the keyboard and calls the method int max(int x, int y, int z, int w) that uses the
brute force method presented in class to return the max of the four variables. The result should be print by
main
.
Sample Test Run
Enter first number: 1
Enter second number: 4
Enter third number: 3
Enter fourth number: 2
The max of 1, 4, 3, and 2 is 4
These next three exercises build on the Max-Of-3 algorithms presented in class; the basic idea is to see how they scale to 4 elements.
The Semi-Brute-Force technique is ignored as it's clear from the case of 3 numbers that it does not scale well.
Lab 7.7 — Cascaded Max of 4 (MaxOf4) (Approval)
Write a program that reads in four integers from the keyboard and calls the method int max(int x, int y, int z, int w) that uses the
cascaded method presented in cass to return the max of the four variables. The result should be print by
main
.
Sample Test Run
Enter first number: 1
Enter second number: 4
Enter third number: 3
Enter fourth number: 2
The max of 1, 4, 3, and 2 is 4
It becomes clear from this and the previous lab that the cascaded method scales better than the brute force.Indeed, we will see when we get to
arrays, that what we are doing here is the foundation for obtaining the maximum of an arbitrarily large sequence of numbers.
Lab 7.8 — Composition Max of 4 (MaxOf4) (Approval)
Write a program that reads in four integers from the keyboard and calls the method int max(int x, int y, int z, int w) that uses the
method composition method presented in cass to return the max of the four variables. The result should be print by
main
.
Your
max
method should contain no conditionals (
if
statements), but rather should call
Math.max
(which
accepts two integers and returns their maximum).
Sample Test Run
Enter first number: 1
Enter second number: 4
Enter third number: 3
Enter fourth number: 2
The max of 1, 4, 3, and 2 is 4
The composition approach is different because it is dependent on an already implemented max algorithm for a simpler (i.e., smaller)
set of numbers. The approach is an important one (i.e., leveraging simpler and/or existing algorithms to create mre complex ones),
and this exercise simply gives you more practice in this fairly important technique.