Está en la página 1de 4

Sarah Lu

12/16/14
Wellesley High School
15.4 Problem Set
2. Write code segments that perform the following tasks:
a. Replace every blank space character in the string str with a newline character
('\n').
String str = The rain in Spain falls mainly on the plain;
System.out.println(str.replace( , \n));
b. Find the index of the first instance of the substring "the" in the string str.
String str = The rain in Spain falls mainly on the plain;
System.out.println(str.indexOf(the, 0);
c. Find the index of the first instance of the substring "the" after the midpoint of
the string str.
String str = The rain in Spain falls mainly on the plain;
System.out.println(str.indexOf(the, 22);
d. Count the number of instances of the whole word "the" in the string str.
String str = "The rain in Spain falls mainly on the plain";
int begin, end, count;
begin = 0; count = 0;
while(begin < str.length()){
end = str.indexOf(" ", begin);
if(end == -1)
end = str.length();
String word = str.substring(begin, end);
System.out.println(word);
if(word.equals("the")){
count++;
}

begin = end + 1;
}
System.out.println("number of instances of the word
'the': " + count);

Written Questions
1. List the three logical operators
&& (and)
|| (or)
!= (not)
2. Construct a truth table for the expression P or not Q
P
Q
P || (!=Q)
True
True
True
True
False
True
False
True
False
False
False
True
3. Suppose P is true and Q is false. What is the value of the expression P AND NOT Q?
True
4.

Write an if statement that displays whether or not a given number is between


a lower bound min and an upper bound max, inclusive. Use a logical operator
in the condition.
5. import java.util.Scanner;
6.
7. public class Problem_set {
8.
9.
public static void main (String args []){
10.
11.
//4. Write an if statement that displays
whether or not a given number is between a
12.
//lower bound min and an upper bound max,
inclusive. Use a logical operator in the condition.
13.
System.out.print("Enter a number: ");
14.
Scanner reader = new Scanner(System.in);
15.
double userNumber = reader.nextDouble();
16.
17.
//lower bound min, upper bound max
18.
double min = 0;
19.
double max = 100;
20.
21.
if(userNumber >= min && userNumber <= max){
22.
System.out.println("your number is
between or on the lower and upper bounds!");

23.
}
24.
25.
else{
26.
System.out.println("your number is not
between or on the lower and upper bounds");
27.
}
28.
}
29. }

5. Rewrite the if statement in Question 4 to use a nested if statement.


if (userNumber >= min){
if(userNumber <= max){
System.out.println("your number is between
or on the lower and upper bounds!");
}
else System.out.println("your number is not
between or on the lower and upper bounds");
}
else System.out.println("your number is not between or on
the lower and upper bounds");

6. Write a nested loop that displays a 10-by-10 square of asterisks.


7. for(int i = 0; i <10; i++){
8.
9.
for (int h = 0; h <=10; h++){
10.
System.out.print("*");
11.
}
12.
System.out.println("");
13.
}

7. Give an example of an assertion and show how it can be checked with Javas
assert statement.
int x = 0;
Assert x != 0
-this will halt the program because assert x != 0 means that it should be halted if
x=0

8. Explain the role that variant and invariant assertions play in showing that a
loop is correct.

A loop invariant is an assertion that expresses a relationship between variables


that remains constant throughout all iterations of the loop. It makes sure that the
constant variables remain that way so the loop runs correctly.
A loop variant is an assertion whose truth changes between the first and final
execution of the loop. The loop variant checks to make sure the loop can be exited.
This means that there must be a loop variable that increments or decrements so the
loop does not go on forever.

También podría gustarte