Big Java / Java Concepts Lab 6

The if Statement

1.

The if statement is used to implement a decision. The 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

1) with n = 6 ?

2) with n = 20 ?

3) with n = 2 ?

4) with n = -1 ?

Relations and Relational Operators

2.

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

Using relational operators, formulate the following conditions in Java:

1) x is positive

2) x is zero or negative

3) x is at least 10

4) x is less than 10

5) x and y are both zero

6) x is even


3.

Formulate the following conditions for the Rectangle object r:

1) The width of r is at most 10

2) The area of r is 100

Input Validation

4.

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

import java.awt.geom.Point2D;
import java.util.Scanner;

public class Slope
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Input x coordinate of the first point: ");
        double xcoord = in.nextDouble();

        System.out.print("Input y coordinate of the first point: ");
        double ycoord = in.nextDouble();

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

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

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

        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 is " + slope);
    }
}


5.

Correct and rebuild the program to disallow a vertical line (denominator = 0). What change did you make to the program?


6.

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


7.

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

The if/else Statement

8.

In the previous example, your program probably responded to user input by ignoring cases that would result in a division by zero. You can use the if/else format to explicitly specify the action to be taken, rather than designing the program to ignore certain input.
if (test_expression)
/* do something . . . */
else
/* do something different . . . */

A wholesaler advertises a volume discount on widgets of 10 cents off the price per widget for every thousand widgets purchased over 10,000. The price before the discount is $950.00 per thousand, or 95 cents per widget.

First write a class Order with methods void addWidgets(int quantity) and double getPrice().

What is the code for the Order class?


9.

Write a program that receives the number of widgets in an order and calculates and displays the total cost.


10.

According to your program, how much will it cost to buy:


1) 2,000 widgets?


2) 15,000 widgets?


3) 18,000 widgets?



Multiple Alternatives

11.

The if/else decision in the preceding example can be extended to select from more than two possible outcomes. The if . . . else . . . else syntax is used to select only one of several possible actions.
if (test expression 1)
/* do something . . . */
else if (test expression 2)
/* do something different . . . */
else
/* do something generic . . . */

Reimplement the Order class. This time there will be no discount on the first 10,000 widgets, 5 cents off per widget for the second 10,000 widgets, 10 cents off per widget for an order over 20,000 widgets, and 20 cents off per widget on any order over 50,000 widgets.

What is the code for the modified Order class?

Nested Branches

12.

If multiple conditions exist, a conditionally executed block may contain 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 concentric.



public class CircleOverlap
{
    public static void main(String[] args)
    {
       Scanner in = new Scanner(System.in);

       System.out.print("Input the radius of the first circle: ");
       double radius1 = in.nextDouble();
       double xcenter1 = 0;
       double ycenter1 = 0;
       System.out.print("Input the radius of the first circle: ");
       double radius2 = in.nextDouble();
       double xcenter2 = 40;
       double ycenter2 = 0;

       /*
          Your work goes here
       */
       }
}

Logical Operations

13.

Java has three logical operations, &&, ||, and !. Using these operations, express the following:

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

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

2) The date is in the second quarter of the year.

3) The date is the last day of the month. (Assume February always has 28 days.)

4) The date is before April 15.


14.

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

public class Shipment
{
public boolean isDiscount()
{
boolean first;
boolean second;

if (fromState.equals("OR"))
{
if (fromAddress.substring(0,11).equals("Rural Route")
first = false;
else
first = true;
}
else if(fromState.equals("WA"))
{
if (fromAddress.substring(0,11).equals("Rural Route"))
first = false;
else
first = true;
}
else if (fromState.equals("BC"))
{
if (fromAddress.substring(0,11).equals("Rural Route"))
first = false;
else
first = true;
}
else
first = false;

if (toState.equals("OR"))
{
if (toAddress.substring(0,11).equals("Rural Route"))
second = false;
else
second = true;
}
else if (toState.equals("WA"))
{
if (toAddress.substring(0,11).equals("Rural Route"))
second = false;
else
second = true;
}
else if (toState.equals("BC"))
{
if (toAddress.substring(0,11).equals("Rural Route"))
second = false;
else
second = true;
}
else
second = false;

if (first && second)
return true;
else
return false;
}
. . .
private String fromAddress;
private String fromCity;
private String fromState;
private String toAddress;
private String toCity;
private String toState;
}

Using Boolean Variables

15.

According to the following program, what color results when using the following inputs?

1) Y N Y

2) Y Y N

3) N N N


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

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

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

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

       if (!red && !blue && !green)
          mixture = "BLACK";
       else if (!red && !blue)
          mixture = "GREEN";
       else if (red)
       {
          if (green || blue)
          {
             if (green && blue)
                mixture = "WHITE";
             else if (green)
                 mixture = "YELLOW";
              else
                 mixture = "PURPLE";
           }
           else
              mixture = "RED";
       }
       else
       {
          if (!green)
             mixture = "BLUE";
          else
             mixture = "CYAN";
       }

       System.out.println("Your mixture is " + mixture);
    }
}