Question

In this lab, you complete a partially written Java program that includes two methods that require...

In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to and including the entered number. The source code file provided for this lab includes the necessary variable declarations and the input statement. Comments are included in the file to help you write the remainder of the program. Instructions Ensure the file named SumAndProduct.java is open. Write the Java statements as indicated by the comments. Execute the program by clicking "Run Code." // SumAndProduct.java - This program computes sums and products. // Input: Interactive. // Output: Computed sum and product. import java.util.Scanner; public class SumAndProduct { public static void main(String args[]) { Scanner s = new Scanner(System.in); int number; String numberString; System.out.println("Enter a positive integer or 0 to quit: "); numberString = s.nextLine(); number = Integer.parseInt(numberString); while(number != 0) { // call sums() method here // call products() method here System.out.println("Enter a positive integer or 0 to quit: "); numberString = s.nextLine(); number = Integer.parseInt(numberString); } System.exit(0); } // End of main() method. // Write sums() method here. // Write products() method here. } // End of SumAndProduct class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE :

import java.util.Scanner;

class SumAndProduct {

static int sums(int x) {
int ans = 0;
for (int i = 1; i <= x; i++) {
ans += i;
}
return ans;
}

static int products(int x) {
int ans = 1;
for (int i = 1; i <= x; i++) {
ans *= i;
}
return ans;
}

public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int number;
String numberString;
System.out.println("Enter a positive integer or 0 to quit: ");
numberString = s.nextLine();
number = Integer.parseInt(numberString);
while (number != 0) {
// call sums() method here
int sum = sums(number);
int prod = products(number);
System.out.println("Sum = "+sum+" , Prod = "+prod);
// call products() method here
System.out.println("Enter a positive integer or 0 to quit: ");
numberString = s.nextLine();
number = Integer.parseInt(numberString);
}
// System.exit(0);
}
// End of main() method.
// Write sums() method here.
// Write products() method here.
// }
//
}

OUTPUT :

:rogram Files Java ljdk1.8.0 131Ibinljava Enter a positive integer or 0 to quit: Sum= 15 , Prod= ї20 Enter a positive intege

Add a comment
Know the answer?
Add Answer to:
In this lab, you complete a partially written Java program that includes two methods that require...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes methods that require multiple...

    In this lab, you complete a partially written Java program that includes methods that require multiple parameters (arguments). The program prompts the user for two numeric values. Both values should be passed to methods named calculateSum(), calculateDifference(), and calculateProduct(). The methods compute the sum of the two values, the difference between the two values, and the product of the two values. Each method should perform the appropriate computation and display the results. The source code file provided for this lab...

  • Complete a partially written C++ program that includes a function that return a value. The program...

    Complete a partially written C++ program that includes a function that return a value. The program is a simple calculator that prompts the user of 2 number and an operation (+, -, * or, /). The two number and the operator are passed to the function where the appropriate arithmetic operation is performed. The result is return to the main function () where the arithmetic operation and result are displayed. For example 3 * 4 = 12 The source code...

  • unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that...

    unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that includes a function with no parameters. The program asks the user if they have preregistered for art show tickets. If the user has preregistered, the program should call a function named discount() that displays the message "You are preregistered and qualify for a 5% discount." If the user has not preregistered, the program should call a function named noDiscount() that displays the message "Sorry,...

  • Summary In this lab, you add a loop and the statements that make up the loop...

    Summary In this lab, you add a loop and the statements that make up the loop body to a Java program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed. The inputs for this program are as follows: R, R, R, L, L, L,...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT