Evaluation Problems Using Conditional Testing
Problem: Positive Integer Test
Write a JAVA program named IsPositiveInteger that prompts the user to enter a positive integer.
The program is to test the user input value to identify whether or not it is a positive integer (a value greater than or equal to zero).
As output, the program will display a message indicating that the user input value is or is not a positive integer.
Sample input/output:
user input: 7 display message: The value 7 is positive
user input: -33 display message: The value -33 is not positive
user input: 0 display message: The value 0 is positive
Initial Questions
- What are my variables and/or objects?
- a variable to store user input
- a Scanner object to allow data to be entered from the keyboard
- What task or tasks is the problem asking my code to perform?
- prompt the user to enter a positive integer
- the above implies storing the user input value within a variable
- test the user input value to see if it is positive
- display a message corresponding to a characteristic of the user input value
- What program logic must the code perform?
- test to see if the user input value is positive (or negative); this can be accomplished by comparing the value to zero; since the problem does not address this, consideration must be given to the value zero; common consideration may allow the code to compare the user value by seeing if the user value is greater than or equal to zero.
Algorithm
Define a class named IsPositiveInteger
Provide method main( )
//Variable Declarations
int num; //store user input
Scanner kbd; //provide input from keyboard
//User Prompt
Prompt user to enter an integer
Store user input in variable num
//Program logic
IF num >= 0
Display message “The value in num is positive”
ELSE
Display message “The value in num is not positive”
//End Program
Solution
1 /*
2 Write a JAVA program named IsPositiveInteger that prompts
3 the user to enter a positive integer.
4 The program is to test the user input value to identify
5 whether or not it is a positive integer (a value greater than or equal to zero).
6 As output, the program will display a message
7 indicating that the user input value is or is not a positive integer.
8
9 Sample input/output 1:
10
11 user input: 7 display message: The value 7 is positive
12 user input: -33 display message: The value -33 is not positive
13 user input: 0 display message: The value 0 is positive
14
15 */
16 import java.util.Scanner;
17
18 public class IsPositiveInteger
19 {
20 public static void main(String [] args)
21 {
22 //variable declarations
23 int userValue = 0;
24 Scanner kbd = new Scanner(System.in);
25
26 //user prompt
27 System.out.println("Please enter an integer: ");
28 userValue = kbd.nextInt();
29
30 //solution logic and output
31 if(userValue >= 0)
32 System.out.println("The value " + userValue + " is positive.");
33 else
34 System.out.println("The value " + userValue + " is negative.");
35 }
36 }
Video
Not Yet Available
Problem: Absolute Value
Write a JAVA program named GetAbsVal. The program prompts the user to enter an integer (negative or positive). As output, the program will display the absolute value of that integer.
Note: You may not use the math library.
Note: Absolute value is always represented as a positive number. For instance, the absolute value of a positive number is that number, the absolute value of a negative number is the positive of that number.
examples:
abs(10) = 10 notated |10| = 10
abs(-10) = 10 notated |-10| = 10
Initial Questions
- What are my variables and/or objects?
- a variable to store user input
- a Scanner object to allow data to be entered from the keyboard
- What task or tasks is the problem asking my code to perform?
- prompt the user to enter an integer
- store the user input value within a variable (an implied step)
- test the user input value to see if it is positive
- display the absolute value of the user input value
- What program logic must the code perform?
- test to see if the user input value is positive (or negative); this can be accomplished by comparing the value to zero to see if the user value is greater than or equal to zero;
Algorithm
Define a class named GetAbsVal
Provide method main( )
//Variable Declarations
int num; //store user input
Scanner kbd; //provide input from keyboard
//User Prompt
Prompt user to enter an integer
Store user input in variable num
//Program logic
IF num >= 0
Display message “The absolute value of num is: num”
ELSE
Display message “The absolute value of num is: (positive)num”
//End Program
Solution
1 /*
2 Write a JAVA program that prompts the user to enter an integer
3 (negative or positive). As output, the program will display the
4 absolute value of that integer.
5 Note: You may not use the math library.
6 Note: Absolute value is always represented as a positive number.
7 For instance, the absolute value of a positive number is that number,
8 the absolute value of a negative number is the positive of that number.
9
10 e.g. abs(10) = 10 notated |10| = 10
11 abs(-10) = 10 notated |-10| = 10
12
13 */
14 import java.util.Scanner;
15
16 public class AbsValue
17 {
18 public static void main(String [] args)
19 {
20 //variable declarations
21 int userValue = 0;
22 Scanner kbd = new Scanner(System.in);
23
24 //user prompt
25 System.out.println("Please enter an integer: ");
26 userValue = kbd.nextInt();
27
28 //solution logic and output
29 if(userValue >= 0)
30 System.out.println("The absolute value of " + userValue + " is: " +
31 userValue);
32 else
33 System.out.println("The absolute value of " + userValue + " is: " +
34 (-1 * userValue));
35
36 }
37 }
Sample Input/Output
Sample Run 1:
Please enter an integer (negative or positive):
13
The absolute value of 13 is: 13
Sample Run 2:
Please enter an integer (negative or positive):
-37
The absolute value of -37 is: 37
Sample Run 3:
Please enter an integer (negative or positive):
0
The absolute value of 0 is: 0
Video
Not Yet Available
Problem: Square Test
Write a JAVA program named IsSquare that prompts the user to enter four integers representing the lengths of the four sides of a possible square.
As output, the program will display a message indicating whether or not the four sides are those of a square.
Sample input/output
Sample 1:
Input values: s1 = 34, s2 = 34, s3 = 34, s4 = 34
Output: The four sides represent those of a square.
Sample 2:
Input values s1 = 34, s2 = 15, s3 = 34, s4 = 15
Output: The four sides do not represent those of a square.
Initial Questions
- What are my variables and/or objects?
- four variables to store user input
- a Scanner object to allow data to be entered from the keyboard
- What task or tasks is the problem asking my code to perform?
- prompt the user to enter four integers representing each side of a square
- store the user input values within each of four variables
- test the user input values for equality to see if their values are those of a square
- display a message indicating whether or not the values represent the sides of a square
- What program logic must the code perform?
- test to see if all four values are equal
Algorithm
Define a class named IsSquare
Provide method main( )
//Variable Declarations
int s1, s2, s3, s4; //store user input
Scanner kbd; //provide input from keyboard
//User Prompt
Prompt user to enter four integers
Store user input in s1, s2, s3, s4
//Program logic
IF s1 == s2 AND s3 == s4 AND s1 == s3
Display message “The four sides represent those of a square.”
ELSE
Display message “The four sides do not represent those of a square.”
//End Program
Solution
1 import java.util.Scanner;
2
3 public class IsSquare
4 {
5 public static void main(String [] args)
6 {
7 //variable declarations
8 int s1, s2, s3, s4;
9 Scanner kbd = new Scanner(System.in);
10
11 //user prompt and data input
12 System.out.println("Please enter four integers: ");
13 s1 = kbd.nextInt();
14 s2 = kbd.nextInt();
15 s3 = kbd.nextInt();
16 s4 = kbd.nextInt();
17
18 //business logic/calulations
19 if(s1 == s2 && s3 == s4 && s1 == s3) //test for a square
20 System.out.println("The four sides represent those of a square.");
21 else
22 System.out.println("The four sides do not represent those of a square.”");
23 }
24 }
Video
Not Yet Available
Problem: Rectangle Test
Write a JAVA program named IsRectangle that prompts the user to enter four integers representing the lengths of the four sides of a possible rectangle.
As output, the program will display a message indicating whether or not the four sides are those of a rectangle.
Note: for purposes of this program, a square (in which the lengths of all four sides are equal) will not be considered to be a rectangle.
Note: for purposes of this program, the degrees of each of the four angles need not to be taken into consideration.
Sample input/output
Sample 1:
Input values: s1 = 34, s2 = 34, s3 = 96, s4 = 96
Output: The four sides represent those of a rectangle.
Sample 2:
Input values: s1 = 34, s2 = 34, s3 = 34, s4 = 34
Output: The four sides do not represent those of a rectangle.
Sample 3:
Input values: s1 = 34, s2 = 44, s3 = 63, s4 = 71
Output: The four sides do not represent those of a rectangle.
Initial Questions
- What are my variables and/or objects?
- four variables to store user input
- a Scanner object to allow data to be entered from the keyboard
- What task or tasks is the problem asking my code to perform?
- prompt the user to enter four integers representing each side of a rectangle
- store the user input values within each of four variables
- test the user input values for equality and inequality to see if their values are those of a rectangle;
- test to see that the data values do not represent those of a square
- display a message indicating whether or not the values represent the sides of a rectangle
- What program logic must the code perform?
- test to see if two pairs of values are equal but all four values are not equal
Algorithm
Define a class named IsRectangle
Provide method main( )
//Variable Declarations
int s1, s2, s3, s4; //store user input
Scanner kbd; //provide input from keyboard
//User Prompt
Prompt user to enter four integers
Store user input in s1, s2, s3, s4
//Program logic
//test to eliminate a square
IF s1 == s2 AND s3 == s4 AND s1 == s3
Display message “The four sides do not represent those of a rectangle.”
//if not a square test for equality of each pair of sides
ELSE IF s1 == s3 && s2 == s4 OR s1 == s2 && s3 == s4 OR s1 == s4 && s2 == s3
Display message “The four sides represent those of a rectangle.”
ELSE
Display message “The four sides do not represent those of a rectangle.”
//End Program
Solution
1 import java.util.Scanner;
2
3 public class IsRectangle
4 {
5 public static void main(String [] args)
6 {
7 //variable declarations
8 int s1, s2, s3, s4;
9 Scanner kbd = new Scanner(System.in);
10
11 //user prompt and data input
12 System.out.println("Please enter four integers: ");
13 s1 = kbd.nextInt();
14 s2 = kbd.nextInt();
15 s3 = kbd.nextInt();
16 s4 = kbd.nextInt();
17
18 //business logic/calulations
19 if(s1 == s2 && s3 == s4 && s1 == s3) //eliminate a square
20 System.out.println("The four sides do not represent those of a rectangle.");
21 else if(s1 == s3 && s2 == s4 || s1 == s2 && s3 == s4 || s1 == s4 && s2 == s3)
22 System.out.println("The four sides represent those of a rectangle.");
23 else
24 System.out.println("The four sides do not represent those of a rectangle.");
25
26
27 //alternate for else if(s1 == s3 && s2 == s4 || s1 == s2 && s3 == s4 || s1 == s4 && s2 == s3)
28 /*
29 else if(s1 == s3 && s2 == s4)
30 System.out.println("The four sides represent those of a rectangle");
31 else if(s1 == s2 && s3 == s4)
32 System.out.println("The four sides represent those of a rectangle ");
33 else if(s1 == s4 && s2 == s3)
34 System.out.println("The four sides represent those of a rectangle ");
35 */
36
37 }
38 }
Video
Not Yet Available