The main goals for this lab are for you to get more comfortable with conditionals, string methods, and recursion.
This lab is a paired programming assignment. What exactly does that mean? You will be working in pairs on the CS lab computers. Each pair will be working on one computer. One person will be the driver and the other person will be the navigator. Here is the rule: the driver controlls the lab computer, but the driver can only type what the navigator tells them to type. For this to work well, each pair should be constantly talking among themselves. After each problem, you will switch roles, the navigator will become the driver and the driver will become the navigator.
Switching Partners This Week Please find a new partner. You will working with this partner for the next 3 weeks. In 3 weeks time we will switch partners again.
Before leaving the lab, make sure you fill out the attendance sheet and go through your answers with a TA or instructor.
Each row in the table below represents an expression. Please fill in the value and types for each expression.
int x = 5;
int y = 13;
int z = 15;
String message = "cat!";
boolean Done = false;
Expression | value | data type | |
---|---|---|---|
x < 10 && y < 10 |
|||
x < 10 || y < 10 |
|||
x < 10 && x > 0 |
|||
x > 10 || x < 0 |
|||
(5/x) > 7.0 |
|||
(z/2) > 7.0 |
|||
message.compareTo(“cats”) == 0 |
|||
!Done |
|||
Done || (x < 6 && y > 10) |
Consider the following code:
String colorName = "purple";
if (colorName.compareTo("red") != 0) {
System.out.println(colorName + " is not primary");
}
else if (colorName.compareTo("blue") != 0 {
System.out.println(colorName + " is not primary");
}
else if (colorName.compareTo("yellow") != 0) {
System.out.println(colorName + " is not primary");
}
else {
System.out.println(colorName + " is primary");
}
TODO: Draw a decision diagram corresponding to this if statement.
TODO: Question 2.2.1:
What does the above code print out when colorName = "purple"
TODO: Question 2.2.2:
What does the above code print out when colorName = "red"
TODO: Question 2.2.3:
What does the above code print out when colorName = "yellow"
Write a program that inputs a month as an integer and returns a string name for that month. For example, if we call the function with the number 1, the program should print “January”.
$ java Month
Enter an integer: 1
January
$ java Month
Enter an integer: 10
October
If the user inputs a number that is not between 1 and 12 (inclusive), then make sure to print out a message.
Write a program that inputs a month as a string and returns the quarter number that the month is in. For example, if we call the function with the month “January”, the program should print 1.
Requirement: Make sure you use a
switch/case
statement here rather than an if/else. Make sure to read section 5.4 of the textbook first.
Write the following methods in a file named StringExamples.java
. Test the methods in the main
method.
Write a method, isEvenLength
, which returns true if the given String has an even number of characters and false otherwise. Implement tests in main to check your answer. For example,
isEvenLength("cat")
returns false
isEvenLength("")
returns true
isEvenLength("a")
returns false
Make sure to read section section 7.9: Substring before completing this section.
Write a method called subStringN
with 2 paramaters, a string and an integer n
.
The method should print 2 substrings: 1) the first n
characters of the string and 2) the last n
characters of the string. For example,
subStringN("hello", 2)
should print he
on one line and then lo
on anothersubStringN("Antidisestablishmentarianism", 4)
should print Anti
on one line and then nism
on another.If the length of the string is shorter than N
, print an message telling the user.
As discussed in class, recursion is the process of a method invoking itself over and over again. There are two components to a recursive method:
Note, make sure to have gone through Chapter 6 before completing the next part of the lab.
Given a String of numbers, write a method called sum
that returns the sum of all the numbers in the String. sum
should have one parameter, the string of numbers
For example sum(1234)
should return 10.
Below we are going to implement this together. The following questions will help guide you.
TODO: In class we motivated recursion by discussing a lazy way to do the dishes, i.e. wash one dish, then ask someone to wash the rest of the dishes. Question: How would we extend that analogy to this situation here of finding the sum of all the numbers in a string?
TODO: Question: Let’s now think about our base case, i.e. when we no longer want to make a recursive call. What would be special about the value assigned to this parameter (the String that is passed in to the method) to tell us to stop making a recursive call?
Now implement that base case in the method.
TODO: Question: During each recursive step, what do we want to keep track off, and what do we want to punt down the line?
We are almost there but there are a few things we still need to do.
TODO: Question: Given the string 1234
, how can we extract the 1
?
TODO: Question: Given the character 1
, how can we get the integer value 1
TODO: Question: Given the String 1234
, how can we get the substring 234
?
Now we are ready to combine these answers to implement our recursive step right after our base case.
In your main method, test sum()
by invoking the method with the following arugments:
sum(234)
should return 9sum(2222)
should return 8sum(19802)
should return 20sum(00020)
should return 2Given a String of numbers, write a method called product
that returns the sum of all the numbers in the String. product
should have one parameter, the string of numbers
For example product(1234)
should return 12.
In todays lab we covered conditionals, string methods (including comparisons), and recursion.
Before leaving, make sure your TA/instructor have signed you out of the lab. If you finish the lab early, you are free to go.