In: Computer Science
Program #1: You will be writing code for several different examples of conditional execution. Each example should be written as a unique method. Each method should include a method header. So the basic outline of your code is: public class Branching { /** * This is an example method header. Notice how it's multi-line and written * using college-level English and not fragments - this is for the Human to * read and not the compiler to parse, so be verbose here. The purpose of this * main method is call the methods written for each of the parts of the *assignment. */ public static void main(String[] args) { methodA(); } public static void methodA() { /** *methodA header */ } A. Prompt the user with: "Enter a whole number". a. Determine if the number is negative and if so, print: "The number is negative." b. Determine if the number is positive and if so, print: "The number is positive." B. Ask the user for another whole number, and then determine if the number is even or odd. a. Hint: modulo might help. b. Print out a message that includes the number the user input as well as indicating if the number the user input was odd or even. C. Ask the user for a temperature, stored as an integer. Write an if/else statement that asks: Is the temperature higher than 78 degrees? If it is not, is the temperature less than (or equal to) 78 degrees? a. Describe the current temperature as “higher than 78 degrees” or “less than or equal to 78 degrees” D. Prompt the user for a letter grade and store this in a char. Then, write a multi-way if statement that ends in a tail-test, that asks the following questions: Is the grade an “A”? Else is the grade a “B”? Else is the grade a “C”? Else is the grade a “D”? Else is the grade an “F”? a. Notice the use of the word else in the sentence above, which provides for a more precise meaning. In spoken English we might use the word “or” instead, but of course, English is terribly ambiguous and Java needs to be discrete, deterministic, and precise. E. Rewrite your code for number D, but use a switch statement to accomplish the same thing. F. Prompt the user for two integer values and store them as “firstInteger” and “secondInteger”. a. Write an if/else statement that outputs either: “ firstInteger was larger than secondInteger” or “secondInteger was larger than firstInteger” on the console, based on the values provided by user that also includes the integer values in the output stament. b. If the values are equal, indicate this in an output that includes the values of the integers.
import java.util.*;
public class Branching
{
/***class Branching is for demonstrating different examples of conditional execution***/
public static void main(String[] args)
{
/***The purpose of this * main method is to call the methods written for each of the parts of the *assignment. ***/
Branching.negativePositive();
Branching.oddEven();
temparatureChecking();
gradePrinter();
gradeSwitch();
integerLarger();
}
public static void negativePositive()
{
/***Demonstration of if condition
* This method is for checking if a whole number inserted by user is negative or positive.
* It uses two if conditions one for checking negative another for checking positive.
* The whole number entered by user is stored in an integer varible named as wholeNumber***/
int wholeNumber;
System.out.println("Enter a whole number tocheck whether it is poitive or negative:");
Scanner sc = new Scanner(System.in);
wholeNumber = sc.nextInt();
if(wholeNumber<0)
System.out.println("The number is negative");
if(wholeNumber>0)
System.out.println("The number is positive");
}
public static void oddEven()
{
/***Demonstration of if else condition
* This method is for checking if a whole number inserted by user is odd or even.
It uses a modulo operator on the user input wholeNumber.
If number modulo 2 is 0 then it is an even number else it is an odd number ***/
int wholeNumber;
System.out.println("Enter a whole number to check whether it is even or odd:");
Scanner sc = new Scanner(System.in);
wholeNumber = sc.nextInt();
if(wholeNumber%2==0)
System.out.println("The number "+wholeNumber+ " entered by the user is even");
else
System.out.println("The number "+wholeNumber+ " entered by the user is odd");
}
public static void temparatureChecking()
{
/***Demonstration of else if branching
This method checks whether the temprature entered by user is less than 78 degrees or not
with the help of if else condition ***/
int temparature;
System.out.println("Enter current temparature:");
Scanner sc = new Scanner(System.in);
temparature = sc.nextInt();
if(temparature>78)
System.out.println("The current temparature "+temparature+" is higher than 78 degrees");
else if(temparature<=78)
System.out.println("The current temparature "+temparature+" is less than or equal to 78 degrees");
}
public static void gradePrinter()
{
/***Demonstration of else if ladder
This method prompts the user to enter a single letter as the grade of student and displays a message
The user input is stored in a character variable grade
An else if ladder is used to perform the comprisons
If finds the correct grade just print a message***/
char grade;
System.out.println("Enter your grade:");
Scanner sc = new Scanner(System.in);
grade = sc.next().charAt(0);
if(grade=='A')
System.out.println("Congratulations. Your grade is A");
else if(grade=='B')
System.out.println(" Your grade is B");
else if(grade=='C')
System.out.println(" Your grade is C. Try to improve your grade");
else if(grade=='D')
System.out.println(" Your grade is D. Try harder");
else if(grade=='F')
System.out.println(" Your grade is F. Improve it next time");
else
System.out.println("Please enter valid grade");
}
public static void gradeSwitch()
{
/***Demonstration of switch statement
This method prompts the user to enter a single letter as the grade of student and displays a message
The user input is stored in a character variable grade
A switch condition is used to perform the comprisons
If finds the correct grade just print a message***/
char grade;
System.out.println("Enter your grade:");
Scanner sc = new Scanner(System.in);
grade = sc.next().charAt(0);
switch(grade)
{
case 'A':
System.out.println("Congratulations. Your grade is A");
break;
case 'B':
System.out.println(" Your grade is B");
break;
case 'C':
System.out.println(" Your grade is C. Try to improve your grade");
break;
case 'D':
System.out.println(" Your grade is D. Try harder");
break;
case 'F':
System.out.println(" Your grade is F. Improve it next time");
break;
default: System.out.println("Please enter valid grade");
break;
}
}
public static void integerLarger()
{
/***Demonstration of if/else condition
This method accepts two user inputs firstInteger and secondInteger as integer variables.
Compares if firstInteger is larger than secondInteger then print a message
Else check if secondInteger is largerthan firstInteger else prints they are equal
***/
int firstInteger,secondInteger;
System.out.println("Enter two integer numbers:");
Scanner sc = new Scanner(System.in);
firstInteger=sc.nextInt();
secondInteger=sc.nextInt();
if(firstInteger>secondInteger)
System.out.println("first Integer "+firstInteger+"was larger than second Integer "+secondInteger);
else if(secondInteger>firstInteger)
System.out.println("second Integer "+secondInteger+"was larger than first Integer "+firstInteger);
else
System.out.println("Two user input integers hold same value "+firstInteger);
}
}
SAMPLE OUTPUT
Screen shots of code