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 when n has a value of 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("*");
Hint: work out which boolean conditions are true first.
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 (hint) |
The following program reads in two points, (x1, y1), and (x2, y2) and calculates the slope of a line formed between the two points. Create and run the program. Describe what happens when the two points have the same x coordinate?
public class Slope
{
public static void main(String [] args)
{
System.out.println("Input x coordinate of the first point:");
double x1 = Console.readDouble();
System.out.println("Input y coordinate of the first point:");
double y1 = Console.readDouble();
System.out.println("Input x coordinate of the second point:");
double x2 = Console.readDouble();
System.out.println("Input y coordinate of the second point:");
double y2 = Console.readDouble();
double slope = (y2 - y1) / (x2 - x1);
System.out.println("The slope of the line between Points 1 and 2 is " + slope);
}
}
What would happen if the coordinates were integers?
Modify the program to dissallow a vertical line (that is, denominator = 0 or x1 having the same value as x2).
What are the results when the both points have the same value, e.g. (4, 2)?
What are the results when the first point is (4, 2.5) and the second point (3, 1.5) ?
Write a program that reads in three integers, a, b, and c, representing the lengths of the sides of a triangle and say whether the resulting triangle is isosceles or not.
Test your program with the following values of a, b and c.
Write a program that reads in three integers, a, b, and c, representing the lengths of the sides of a triangle. The program should then say whether the resulting triangle is isosceles, equilateral, ordinary or impossible (i.e. the three sides cannot form any triangle.)
Test your program with the following values of a, b and c.
In each case, sketch the triangle, establish what type it is and see if your program classifies it properly.
Here is a hint.
Java has three logical operators, &&, || and !.
Using these operations, express the following:
Formulate the following conditions on the date that is given by the variables day and month
Assume that day is an int between 1 and 31 inclusive and that month is an int between 1 and 12 inclusive.
A) [Preamble] Extend the following code to test whether two circles - each having a fixed center point and a user-defined radius - are touching.
public class CircleOverlap
{
public static void main(String [] args)
{
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
}
}
Here are some questions from previous final exams concerning if statements.