Question

In: Computer Science

Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...

Understanding if-elseStatements

Summary

In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78.

Instructions

  1. Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate.
  2. Write the rest of the program using assignment statements, if statements, or if-else statements as appropriate. There are comments in the code that tell you where you should write your statements. The output statement is written for you.
  3. Execute the program. Your output should be:
The largest value is 78

The smallest value is −50

Grading

When you have completed your program, click the Submit button to record your score.

// LargeSmall.java - This program calculates the largest and smallest of three integer values.


public class LargeSmall

{

public static void main(String args[])

{

// This is the work done in the housekeeping() method

// Declare and initialize variables here.

int largest; // Largest of the three values.

int smallest; // Smallest of the three values.

// This is the work done in the detailLoop() method

//Write assignment, if, or if else statements here as appropriate.

// This is the work done in the endOfJob() method

// Output largest and smallest number.

System.out.println("The largest value is " + largest);

System.out.println("The smallest value is " + smallest);

}

}

Solutions

Expert Solution

import java.util.Scanner;

public class LargeSmall {
    public static void main(String args[]) {
        // This is the work done in the housekeeping() method
        // Declare and initialize variables here.
        Scanner in = new Scanner(System.in);
        int n1, n2, n3;
        int largest; // Largest of the three values.
        int smallest; // Smallest of the three values.
        // This is the work done in the detailLoop() method
        System.out.print("Enter three integers: ");
        n1 = in.nextInt();
        n2 = in.nextInt();
        n3 = in.nextInt();
        largest = n1;
        if (n2 > largest) largest = n2;
        if (n3 > largest) largest = n3;

        smallest = n1;
        if (n2 < smallest) smallest = n2;
        if (n3 < smallest) smallest = n3;
        // This is the work done in the endOfJob() method
        // Output largest and smallest number.
        System.out.println("The largest value is " + largest);
        System.out.println("The smallest value is " + smallest);
    }
}


Related Solutions

Summary In this lab, you complete a prewritten Python program that computes the largest and smallest...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest of three integer values. The three values are -50, 53, 78. Instructions Two variables named largestand smallest are assigned for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if statements, or elifstatements as appropriate....
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus...
Summary In this lab, you complete a prewritten Java program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. Bonuses are calculated based on an employee’s productivity score as shown below. A productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked. Productivity Score Bonus <=30 $50 31–69 $75 70–199 $100 >= 200 $200 Instructions Ensure the file...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that:...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that: -Loads chemical-element data by scanning through the file (accessed via file path or URL) exactly once. FILENAME: Elements.txt -Expects one or more command-line arguments that constitute a chemical formula, as described below. *A chemical formula, for the sake of this program, shall be defined as: One or more whitespace-delimited tokens Each token consists of either: An element symbol, implying one atom of that element...
Summary In this lab, you will make additions to a C++ program that is provided. The...
Summary In this lab, you will make additions to a C++ program that is provided. The program is a guessing game. A random number between 1 and 10 is generated in the program. The user enters a number between 1 and 10, trying to guess the correct number. If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise, the program asks the user if he or she wants to guess...
Write the following java program. Desc: The program computes the cost of parking a car in...
Write the following java program. Desc: The program computes the cost of parking a car in a public garage at the rate $5.00/hour. The client will always be charged for whole hours. For example, if a car parked for 2 hours and 1 minute, the client will be charged for 3 hours. Input: User inputs the entry time and exit time in 24-hr clock format (hh:mm) Output: The enter and exit times, the length of time the car is parked...
Summary In this lab, you add the input and output statements to a partially completed Java...
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method...
Searching an Array for an Exact Match in Java Summary In this lab, you use what...
Searching an Array for an Exact Match in Java Summary In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten Java program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user of the program to enter a city name; your program then searches the array for that city name. If it is not found, the program should print...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial of a specified number, or estimates the value of 'e' using the specified number of iterations (of a Taylor series). Please feel free to use the Internet to find resources that explain how to estimate the value of 'e' using a Taylor series. In the case of no or invalid number of parameters, the program should show help instructions that looks like: --- Assign...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT