Question

In: Computer Science

Program #1: You will be writing code for several different examples of conditional execution. Each example...

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.

Solutions

Expert Solution

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


Related Solutions

In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
Identify each of the following COMPOUND sentences as a conjunction, disjunction, conditional, or negation by writing...
Identify each of the following COMPOUND sentences as a conjunction, disjunction, conditional, or negation by writing the name on your answer sheet. 1. If it rains, Kellogg will not flood. 2. Either the legislature will pass the finance bill or they will run out of time, and they won’t run out of time. 3. Software is either reliable or cheap, but not both. 4. If one goes to the concert, one had better take earplugs and water. 5. It’s not...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string and number exception handling, as well as a user defined exception CREATE A NEW PROGRAM that demonstrates Any 2 of the following 3 choices: 1 numeric and 1 string exception make the program require 1 input argument and throw an exception if there are no command line arguments passed in. In the exception, inform the user that an input argument is required. (You can...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack. You will submit three...
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
1. What are examples of instances when you might use conditional formatting in an Excel spreadsheet?...
1. What are examples of instances when you might use conditional formatting in an Excel spreadsheet? Try to come up with an example that is not used in the chapter. 2.Provide an example of when you would use the COUNTIF function and one of when you would use the COUNTIFS function. Which of the following functions identified in this or the previous chapter do you think will be the most useful? Why? 3.Why you might want to use tables in...
Discuss the importance of planning out a program on paper before writing the actual code, and...
Discuss the importance of planning out a program on paper before writing the actual code, and why there are advantages to doing it this way. 300 words
Discuss the importance of planning out a program on paper before writing the actual code, and...
Discuss the importance of planning out a program on paper before writing the actual code, and why there are advantages to doing it this way. 300 words
1. Assume you are writing code to serialize a group of Student objects to an XML...
1. Assume you are writing code to serialize a group of Student objects to an XML document. What Java API method will you call to execute the serialization process? NOTE: You must use the XML serialization/deserialization method/concepts presented in this course. 2. Assume that the list of exception types below may occur within the scope of a method. In order to properly handle these exceptions, each type must be handled individually and separately.  Arrange the exception types in the order in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT