QuestionQuestion

1. Write a Java program that consists of repeated calls to 2 methods to compute the sum and product of all integers between two bounds.
For example if the lower bound is 3 and the upper bound is 7, the first method will return the sum 25 (3 + 4 + 5 + 6 + 7) and the second method will return the product 2520 (3 * 4 * 5 * 6 * 7).
The program will include a loop (count controlled or sentinel controlled, your choice); inside the loop call the methods for various values of the lower bound and upper bound (user input - make sure you do input validation and check if the lower bound is less than the upper bound), and print the results.

The following specifications describe a method that computes the sum of all integers between 2 bounds:
Input parameters:
int lower, int upper (where lower is the lower bound and upper is the upper bound)
Return: the sum of all numbers between lower and upper

The following specifications describe a method that computes the product of all integers between 2 bounds:
Input parameters:
int lower, int upper (where lower is the lower bound and upper is the upper bound)
Return: the product of all numbers between lower and upper


2. Write a Java program that uses a value-returning method to identify the prime numbers between 2 bounds (input from the user).
The method should identify if a number is prime or not.
Call it in a loop for all numbers between the 2 bounds and display only prime numbers.
Check for errors in input.
Note: A number is prime if it is larger than 1 and it is divisible only by 1 and itself (Note: 1 is NOT a prime number)

Example: 15 is NOT prime because 15 is divisible by 1, 3, 5, and 15;
19 is prime because 19 is divisible only by 1 and 19.

Sample run:
How many times to test for prime numbers? -3
Error! Should be positive. Reenter: 3
Enter lower bound/upper bound: 5 3
Error! Lower bound should be larger. Reenter: 3 15
Prime numbers between 3 and 15 are: 3 5 7 11 13

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.Scanner;


/**
*
* @author
*/
public class question1 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       int lower;
       int upper;
       System.out.print("Enter lower bound: ");
       lower = sc.nextInt();
       System.out.print("Enter uppper bound: ");
       upper = sc.nextInt();
       System.out.println("Sum: " + first_method(lower, upper));
       System.out.println("Product: " + second_method(lower, upper));
    }...
$9.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats