## Computing Concepts with Java Essentials Laboratory Notebook Chapter 5 - Decisions

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

#### Lab Objectives

To gain experience in

• implementing simple decisions
• writing expressions with relational operators
• implementing repeated and nested branches
• using Boolean variables
• using Boolean operations

### R1. The if statement

In Java, decisions to do or not to do something can be made by using the statement if (test expression). The test expression must be an expression that can be evaluated as either true or false. If it evaluates to true, a succeeding statement or group of statements enclosed in { . . . }, called a block statement will be executed.

How many * will be printed when the following code is executed with n = 7 ?

```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("*");
```

### R2. Relations and Relational Operators

The relational operators in Java are == != < > <= and >=

• Formulate the following conditions in Java:
 x is positive x is zero or negative x is at least 10 x is less than 10 x and y are both zero x is even
• Formulate the following conditions for the Rectangle object r.
 The width of r is at most 10 The area of r is 100

### P1. Input validation

• Build and run the following program. Describe what happens when the two points have the same x coordinate?

```import java.awt.geom.Point2D;

public class Slope
{  public static void main(String[] args)

System.out.println("Input x coordinate of the first point:");

System.out.println("Input y coordinate of the first point:");

Point2D.Double p1 = new Point2D.Double(xcoord, ycoord);

System.out.println("Input x coordinate of the second point:");

System.out.println("Input y coordinate of the second point:");

Point2D.Double p2 = new Point2D.Double(xcoord, ycoord);

double slope = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());

System.out.println("The slope of the line between Points 1 and 2 is " + slope);
}
}
```
• Correct and rebuild the program to dissallow a vertical line (denominator = 0).

• What are the results when point1=(4,2) and point2 = (4,2) ?

• What are the results when when point1=(4,2.5) and point2 = (3,1.5) ?

### P2. The if/else statement

In the previous example, your program probably responded to user input by ignoring cases that would result in a divide by zero. Instead, you can use the if/else format to explicitly specify the action to be taken.

```if (test_expression)
/* do something ... */
else
/* do something different ... */
```

A wholesaler advertises a volume discount on blank CD ROMs of 10 cents/disk for each thousand purchased above 10000. (The regular price is \$950.00 per 1000, or 95 cents per disk). Write a program that receives the number of blank CD ROMs in an order, then calculates and displays the total cost.

 2000 disks? 15000 disks? 180000 disks?

### P3. Multiple alternatives

The if/else decision in the preceding example can be extended to select from more than twp possible outcomes. The if ... else if . . else syntax is used to select exactly one of several possible actions.

```if (test expression 1)
/* do something ... */
else if (test expression 2)
/* do something different ... */
else
/* do something generic ...   */
```

Write a program to compute the cost of a purchase of blank CD ROMs as above, but this time, have there be no discount on the first 10000, 5 cents/disk on the second 10,000, 10 cents/disk over 20,000 and 20 cents/disk on any over 50,000.

### P4. Nested Branches

If there are multiple conditions, it can happen that a conditionally executed block contains further decisions. Here is an example.

Extend the following code to test whether two circles - each having a fixed center point and a user-defined radius - are disjoint, overlapping or mutually contained.

```public class CircleOverlap
{  public static void main(String[] args)
double xcenter1 = 0;
double ycenter1 = 0;
double xcenter2 = 40;
double ycenter2 = 0;

/*
*/
}
}
```

### R3. Logical Operations

Java has three logical operations, &&, || and !.

Using these operations, express the following:

• x and y are both positive or neither of them is positive.

Formulate the following conditions on the date that is given by the variables day and month

• The date is in the second quarter of the year.
• The date is the last day of the month. (Assume February always has 28 days).
• The date is before April 15.
.

### P5. Logical Operations

The following program determines if a particular package is elligible for Unidentified Delivery Service's special Northwest Urban Rate Discount. Simplify the nested branches by using logical operations &&, ||, ! wherever possible.

```public class UPS
{  public static void main(String[] args)
{  boolean first;
boolean second;
System.out.println("From City:");
System.out.println("From State/Province:");

System.out.println("To City:");
System.out.println("To State/Province:");

if (fromState.equals("OR"))
first = false;
else
first = true;
}
else if(fromState.equals("WA"))
first = false;
else
first = true;
}
else if (fromState.equals("BC"))
first = false;
else
first = true;
}
else
first = false;

if (toState.equals("OR"))
second = false;
else
second = true;
}
else if (toState.equals("WA"))
second = false;
else
second = true;
}
else if (toState.equals("BC"))
second = false;
else
second = true;
}
else
second = false;

if (first && second)
System.out.println("Package eligible for Northwest Urban Rate Discount!");
else
System.out.println("Package eligible for Standard Rate");
}
}
```

### R4. Using Boolean variables

According to the following program, what color is the resulting mixture under the following inputs?

 Y N Y Y Y N N N N
```public class ColorMixer
{  public static void main(String[] args)
{  String mixture = "";
boolean red = false;
boolean green = false;
boolean blue = false;
String input;

System.out.println("Include red in mixture? (Y/N)");
if (input.toUpperCase().equals("Y"))
red = true;

System.out.println("Include green in mixture? (Y/N)");
if (input.toUpperCase().equals("Y"))
green = true;

System.out.println("Include blue in mixture? (Y/N)");
if (input.toUpperCase().equals("Y"))
blue = true;

if (!blue && !green)
mixture = "RED";
else if (!red && !blue)
mixture = "GREEN";
else if (!red && !green)
mixture = "BLUE";
else if (red)
{  if (green || blue)
{  if (green && blue)
mixture = "BLACK";
else if (green)
mixture = "YELLOW";
else
mixture = "PURPLE";
}
}
else
{  if (blue && green)
mixture = "CYAN";
else
mixture = "WHITE";
}
System.out.println("Your mixture is " + mixture);
}
}```