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

### Cay S. Horstmann Geof Pawlicki

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 circles will be drawn when the following code is executed with radius = 7 ?

```import ccj.*;

public class Phoenix extends GraphicsApplet
{  public void run()

{   Circle c1 = new Circle(new Point(0,0), radius - 5.0);
c1.draw();
}

Circle c2 = new Circle(new Point(0,0), radius);
c2.draw();
}
}
```

### 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
• Formulate the following conditions for the employee object e.
 The last name of e starts with the letter H The salary of e is at least \$50,000
• Formulate the following conditions on the variable harrysBirthday of type Time.
 Harry is at least 18 years old. Harry was born in January. Harry was born in a leap year (assume his birthday was between 1901 and 2099).

### P1. Input validation

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

```import ccj.*;

public class Slope
{  public static void main(String[] args)
{  System.out.print("Input x coordinate of the first point: ");

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

Point p1 = new Point(xcoord, ycoord);

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

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

Point p2 = new Point(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.

```void run()
Circle circle1 = new Circle(new Point(0,0), radius1);
circle1.draw();

Circle circle2 = new Circle(new Point(0,4), radius2);
circle2.draw();

/*
*/

}
```

### P5. 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.

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.

```import ccj.*;

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

System.out.print("To City: ");
System.out.print("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");
}
}
```

### R3. 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
```import ccj.*;

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

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

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

System.out.print("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);
}
}```