The main goals for this lab are for you to get more comfortable with conditionals and string methods.
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");
}
Exercise 2.1: Draw a decision diagram corresponding to this if statement.
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"
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. 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.
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.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,
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.
In todays lab we covered conditionals and string methods (including comparisons).
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.