Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 2: In this problem, you will solve the same problem using all 3 loops a) b) Create a class called Loops with no constructor

image text in transcribed

Problem 2: In this problem, you will solve the same problem using all 3 loops a) b) Create a class called Loops with no constructor or instance variables. The class will have 3 different method. The first method is a While loop method. Call it usingWhileLoops and it will have an integer argument called stop. Use a while loop to calculate the average of all numbers from 1 to 20. You wil need three local variables which should be declared outside the loop. You will need an integer called begin which is set to 1. Next you will need a double called sum which is set to 0. Lastly you will need an integer, count, which you set to O. I have specified the sum to be a double so that the result will be a double. You loop condition should be countstop. While you loop is executing, add 1 to count and add begin to sum. Increment begin. Make sure you increment begin after you have added it to sum. After the loop is over return sum/count. The return value should be a double. The second method will be a For loop method. Call it usingForLoops and it will have the same integer argument called stop. Remember that stop from the usingWhileLoops goes away at the end of the method so you will need a new stop value. This method should do the same thing as b. The code will be a little different. Remember the difference between a while and for loop. The last method will be a do loop. Call it usingDoLoops and it will also have the same integer argument called stop. Again, the code will perform the same operation but using a Do loop instead of a for loop. Create a LoopsTester class. Import Scanner. Create a Scanner object. Create a Loops object. Prompt the user to enter a stopping value. This will be used as input to each of the 3 methods. c) d) e) f) 8) h) i) Call the three methods and print the results

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Auditing The Master A Tax Collector Report

Authors: B. Cobbey Crisler

1st Edition

1912297108, 978-1912297108

Students also viewed these Databases questions