Skip to main content

Lab 3: Conditionals & Strings

Objectives:

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

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

Exercise 2.1: Draw a decision diagram corresponding to this if statement.






2.2 Result of code

Exercise 2.2.1: What does the above code print out when colorName = "purple"


Exercise 2.2.2: What does the above code print out when colorName = "red"


Exercise 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. If the number is not between 1 and 12, print an error message.

Requirement: Make sure you use a switch/case statement here rather than an if/else.

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.5: 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.

Wrap up

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

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.