EBK BUILDING JAVA PROGRAMS
EBK BUILDING JAVA PROGRAMS
4th Edition
ISBN: 8220102719606
Author: Stepp
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 1E

Write a method called fractionSum that accepts an integer parameter n and returns as a double the sum of the first n terms of the sequence

i = 1 n 1 i

In other words, the method should generate the following sequence:

1 + 12 + 13 + 14 + 15 +

You may assume that the parameter n is nonnegative.

Expert Solution & Answer
Check Mark

Explanation of Solution

Given code snippet:

  //define the static function

        public static double fractionSum(int n) {

            //declare the required variables

            int denominator = 1;

            double sum = 0.0;

            //loop to generate the sequence

            for (int i = 1; i <= n; i++) {

                //evaluate the value

                sum += (double) 1 / denominator;

                //increment the value

                denominator++;

            }

            //return statement

            return sum;

        }

Explanation to the above code snippet:

  • In the above code, the static method named “fractionSum” is defined.
  • Required variables are declared and initialized.
  • For loop is initialized to generate the required sequence.
  • Evaluate the value and store it in the variable “sum”.
  • The value stored in the variable “sum” is returned.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a method that returns the sum of a range of numbers. It should take the starting and ending numbers of the range (inclusive) as parameters. (Use Java) For example: sumRange(0, 5) // returns 15 (0 + 1 + 2 + 3 + 4 + 5 = 15)sumRange(3,10) // returns 55 (3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 52)sumRange(5, 7) // returns 18 (5 + 6 + 7 = 18)
Write a method called min that takes three integers as parameters and returns the smallest of the three values; for example, a call of min(3,–2, 7) would return 22, and a call of min(19, 27, 6) would return 6. Use Math.min to write your solution.
Write a method called printPowersOfN that accepts a base and a maximum exponent as arguments. The method prints each power of the base from 0 up to that maximum exponent, inclusive.   For example: printPowersOfN(4, 3);    // should output 1 4 16 64 This calculates 40, 41, 42, and 43.   printPowersOfN(5, 6);    // should output 1 5 25 125 625 3125 15625This calculates 50, 51, 52, 53, 54, 55, and 56. printPowersOfN(–2, 8);   // should output 1 –2 4 –8 16 -32 64 –128 256   Both arguments are integers. You may assume that the maximum exponent passed to printPowersOfN has a value of 0 or greater.   The Math class may help you with this problem! If you use it, you may need to cast its results from double to int so that you don’t see a .0 after each number in your output. You may also write this program without using the Math class. Either solution is acceptable so long as it displays the correct output.   Please use the three sample calls in your submitted program run. You are welcome to…

Chapter 4 Solutions

EBK BUILDING JAVA PROGRAMS

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4;License: Standard YouTube License, CC-BY