Skip to main content

Lab 3: Conditionals, Strings, Recursion

Objectives:

The main goals for this lab are for you to get more comfortable with conditionals, string methods, and recursion.

Paired Programming rules

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.

Finishing the lab

Before leaving the lab, make sure you fill out the attendance sheet and go through your answers with a TA or instructor.

1. Expressions

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)

     

2. Colors

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");
}

2.1 Decision Diagram

TODO: Draw a decision diagram corresponding to this if statement.






2.2 Result of code

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"


3. Calendar

3.1 Month Number to Name

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.

3.2 Month Name to Quarter

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.

4. Strings

Write the following methods in a file named StringExamples.java. Test the methods in the main method.

4.1 Even Length

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,

4.2 Substrings

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,

If the length of the string is shorter than N, print an message telling the user.

5. Recursion

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:

  1. A base case that determines when to stop recursively invoking the method
  2. a step to perform some computation and make a recursive call to the method. The computation usually involves the results of the recursive call

Note, make sure to have gone through Chapter 6 before completing the next part of the lab.

Sum of numbers

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.

Overview of approach

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?




Base case

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.



Recursive step

TODO: Question: During each recursive step, what do we want to keep track off, and what do we want to punt down the line?

HINT What is the result of `1 + sum(234)`



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?

HINT Look at the example on page 105 of the textbook.



TODO: Question: Given the character 1, how can we get the integer value 1

HINT What is the ASCII value of `'0'`, what about `'9'`? You can look them up here: https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html



TODO: Question: Given the String 1234, how can we get the substring 234?

HINT Checkout the substring methods on the java docs: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html



Now we are ready to combine these answers to implement our recursive step right after our base case.

Testing

In your main method, test sum() by invoking the method with the following arugments:

Product of numbers

Given 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.

Wrap up

In todays lab we covered conditionals, string methods (including comparisons), and recursion.

Signing out

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.