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.
To gain experience in
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("*");
The relational operators in Java are == != < > <= and >=
import java.awt.geom.Point2D; public class Slope { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.println("Input x coordinate of the first point:"); double xcoord = console.readDouble(); System.out.println("Input y coordinate of the first point:"); double ycoord = console.readDouble(); Point2D.Double p1 = new Point2D.Double(xcoord, ycoord); System.out.println("Input x coordinate of the second point:"); xcoord = console.readDouble(); System.out.println("Input y coordinate of the second point:"); ycoord = console.readDouble(); 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); } }
/* paste program here */
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.
According to your program, how much will it cost to buy
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.
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) { ConsoleReader console = new ConsoleReader(System.in); System.out.println("Radius 1:"); double radius1 = console.readDouble(); double xcenter1 = 0; double ycenter1 = 0; System.out.println("Radius 2:"); double radius2 = console.readDouble(); double xcenter2 = 40; double ycenter2 = 0; /* Your work goes here */ } }
Java has three logical operations, &&, || and !.
Using these operations, express the following:
Formulate the following conditions on the date that is given by the variables day and month
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; ConsoleReader console = new ConsoleReader(System.in); System.out.println("From Address:"); String fromAddress = console.readLine(); System.out.println("From City:"); String fromCity = console.readLine(); System.out.println("From State/Province:"); String fromState = console.readLine(); System.out.println("To Address:"); String toAddress = console.readLine(); System.out.println("To City:"); String toCity = console.readLine(); System.out.println("To State/Province:"); String toState = console.readLine(); 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) System.out.println("Package eligible for Northwest Urban Rate Discount!"); else System.out.println("Package eligible for Standard Rate"); } }
According to the following program, what color is the resulting mixture under the following inputs?
public class ColorMixer { public static void main(String[] args) { String mixture = ""; boolean red = false; boolean green = false; boolean blue = false; String input; ConsoleReader console = new ConsoleReader(System.in); System.out.println("Include red in mixture? (Y/N)"); input = console.readLine(); if (input.toUpperCase().equals("Y")) red = true; System.out.println("Include green in mixture? (Y/N)"); input = console.readLine(); if (input.toUpperCase().equals("Y")) green = true; System.out.println("Include blue in mixture? (Y/N)"); input = console.readLine(); 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); } }
Don't forget to send your answers when you're finished.