 Java: Compute Prime Numbers, Sum, Product of All Integers Between Two Bounds

Subject Computer Science Java Programming

Question

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 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));
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Directed Graph in Java \$40.00
Java
Programming
Codes
Algorithms
Graphs
Nodes
Edges
Dept-First-Search
Statements
Variables
Loops
Variables
Input
Output
Java Program For Longest Path in Graph \$40.00
Java
Programming
Codes
Algorithms
Graph Theory
Classes
Nodes
Edges
Paths
Distances
Loops
Statements
Variables
Input Files
Output Files
HashMap
Array
Integers
Methods
Maze Problem in Java \$50.00
Java
Programming
Codes
Algorithms
Maze Problem
Classes
Constructor
Stack
Positions
Points
Push
Pop
Symbols
Statements
Variables
Rows
Columns
Strings
Integers
Commands
Pseudo-Code
Live Chats