1 R1. The if statementIn Java, decisions to do something or not can be made by using the test expression if. The test expression must be an expression that can be evaluated as either true or false. If it evaluates to true, a block statement, a succeeding statement or group of statements enclosed in { . . . }, will be executed.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 executed1) with n = 6 ?2) with n = 20 ?3) with n = 1 ? 4) with n = -1 ? Answer:

 2 R2. Relations and Relational OperatorsThe relational operators in Java are ==, !=, <, >, <=, and >=.Using relational operators, formulate the following conditions in Java:1) x is positive2) x is zero or negative3) x is at least 104) x is less than 105) x and y are both zero6) x is even Answer:

 3 Formulate the following conditions for the Rectangle object r:1) The width of r is at most 102) The area of r is 100 Answer:

 4 P1. Input ValidationBuild and run the following program. Describe what happens when the two points have the same x coordinate?import java.awt.geom.Point2D; import javax.swing.JOptionPane;public class Slope {    public static void main(String[] args)    {         String input = JOptionPane.showInputDialog("Input x coordinate of the first point:");         double xcoord = Double.parseDouble(input);         input = JOptionPane.showInputDialog("Input y coordinate of the first point:");         double ycoord = Double.parseDouble(input);         Point2D.Double p1 = new Point2D.Double(xcoord, ycoord);         input = JOptionPane.showInputDialog("Input x coordinate of the second point:");       xcoord = Double.parseDouble(input);         input = JOptionPane.showInputDialog("Input y coordinate of the second point:");         ycoord = Double.parseDouble(input);         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);      } } Answer:

 5 Correct and rebuild the program to disallow a vertical line (denominator = 0). Answer:

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

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

 8 P2. The if/else StatementIn the previous example, your program probably responded to user input by ignoring cases that would result in a divide by zero. You can use the if/else format to explicitly specify the action to be taken, rather than allowing 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(). Then write a program that receives the number of widgets in an order and calculates and displays the total cost. Answer:

 9 Write a test class to feed various inputs into the Order class. You can omit this part if you use BlueJ. Answer:

10.

 1) 2,000 widgets?
 2) 15,000 widgets?
 3) 180,000 widgets?

 11 P3. Multiple AlternativesThe 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. Answer:

 12 P4. Nested BranchesIf 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)   {      String input = JOptionPane.showInputDialog("Input the radius of the first circle:");      double radius1 = Double.parseDouble(input);      double xcenter1 = 0;      double ycenter1 = 0;      input = JOptionPane.showInputDialog("Input the radius of the first circle:");      double radius2 = Double.parseDouble(input);      double xcenter2 = 40;      double ycenter2 = 0;      /*         Your work goes here      */   }} Answer:

 13 R3. Logical OperationsJava 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. Answer:

 14 P5. Logical OperationsThe 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;} Answer:

15.

R4. Using Boolean Variables

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;
String input = JOptionPane.showInputDialog("Include red in mixture? (Y/N)");
if (input.toUpperCase().equals("Y"))
red = true;

input = JOptionPane.showInputDialog("Include green in mixture? (Y/N)");
if (input.toUpperCase().equals("Y"))
green = true;

input = JOptionPane.showInputDialog("Include blue in mixture? (Y/N)");
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 = "BLACK";
else if (green)
mixture = "YELLOW";
else
mixture = "PURPLE";
}
else
mixture = "BLACK";
}
else
{
if (!green)
mixture = "BLUE";
else
mixture = "WHITE";
}
System.out.println("Your mixture is " + mixture);
}
}