Package Title: Lab Questions Course Title: Big Java



Download 0.57 Mb.
Page1/4
Date29.06.2021
Size0.57 Mb.
#147166
  1   2   3   4

Package Title: Lab Questions

Course Title: Big Java

Chapter Number: 5 Decisions

Question type: Essay

1) The if statement is used to implement a decision. The simplest form of an if statement has two parts: a condition and a body. If the condition is true, the body of the statement is executed. The body of the if statement consists of a statement block.

Consider the following code:

if (n > 10)

{

System.out.print("*****");



}

if (n > 7)

{

System.out.print("****");



}

if (n > 4)

{

System.out.print("***");



}

if (n > 1)

{

System.out.print("**");



}

System.out.println("*");



How many * will be printed when the code is executed

a) with n = 6 ?

b) with n = 20 ?

c) with n = 2 ?

d) with n = –1 ?

2.1) An alternate form for an if statement has multiple parts: a condition that evaluates to true or false, a statement that is executed if the condition is true, the word else, and finally a statement that is executed when the condition is false. Each statement can be a simple statement consisting of a single Java instruction, a compound statement (such as another if statement) or a block statement (matching braces {} that surround one or more Java statements). We suggest using the brace notation in every case. Consider the code below that prompts the user to input a value for x and for y. It then prints the smallest value contained in the variables x and y.

import java.util.*;


public class SmallestInt

{

public static void main(String[] args)



{

Scanner scan = new Scanner(System.in);

System.out.println("Enter a value for x:");

int x = scan.nextInt();

System.out.println("Enter a value for y:");

int y = scan.nextInt();

if (x <= y)

{

System.out.println("The smallest value was " + x);



}

else


{

System.out.println("The smallest value was " + y);

}

}

}


Modify the code above so that it prompts the user to enter a third value for a variable z. Rewrite the logic so that the program prints out the smallest value contained in x, y, and z.

2.2) The code below is a more efficient solution to the problem in Lab 5.2.1.

import java.util.*;


public class SmallestInt

{

public static void main(String[] args)



{

Scanner scan = new Scanner(System.in);


System.out.println("Enter a value for x:");

int x = scan.nextInt();

int smallest = x; // x is the smallest value so far
System.out.println("Enter a value for y:");

int y = scan.nextInt();

if (y < smallest)

{


smallest = y; // Update smallest if necessary

}

System.out.println("Enter a value for z:");



int z = scan.nextInt();

if (z < smallest)

{

smallest = z; // Update smallest if necessary



}
System.out.println("The smallest value was " + smallest);

}

}


Modify the code so that it prompts the user for four integers (w, x, y, and z) and prints the smallest value contained in those variables. How hard would it be to modify the version of the program you wrote in Lab 5.2.1 (or that lab’s suggested solution) to solve the four-variable problem?

3) In the code below, the if statement evaluates the condition x < 10 and assigns the variable color either the value "red" or "blue". The condition is first examined and the corresponding alternative is taken. The strategy in this code is to wait until we know exactly which alternative to take before assigning a color.

String color = "";

if (x < 10)

{

color = "red";



}

else


{

color = "blue";

}

Often an alternate strategy (let’s call it “act first, decide later”) can be used to simplify the logic. If the actions in the true and false statements are reversible, we can go ahead and execute the false (or true) statement and then code an if statement that determines whether that action was correct. If the action was incorrect we can reverse it. We solve the problem posed above using this alternative strategy:

String color = "blue";

if (x < 10)

{

color = "red";



}
We “act first” by assuming blue is the right color to assign to the variable color. We correct it if that was wrong. The logic is simpler and involves coding one less alternative in the if statement.


Download 0.57 Mb.

Share with your friends:
  1   2   3   4




The database is protected by copyright ©essaydocs.org 2023
send message

    Main page