Building Java Programs: A Back to Basics Approach (4th Edition)
Building Java Programs: A Back to Basics Approach (4th Edition)
4th Edition
ISBN: 9780134322766
Author: Stuart Reges, Marty 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 program that reads a student’s name together with his or her test scores. The program should thencompute the average test score for each student and assign the appropriate grade. The grade scale is asfollows:90-100, A; 80-89, B; 70-79, C; 60-69, D; 0-59, FYour program must use the following methods:(a) A value-returning method, calculateAverage, to determine and return the average of fivetest scores for each student. Use a loop to read and sum the five test scores. (This method doesnot output the average test score. That task must be done in the method main.)(b) A value-returning method, calculateGrade, to determine and return each student’sgrade. (This method does not output the grade. That task must be done in the method main.)Test your program on the following data. Read the data from a file and send the output to a file. Do notuse any global variables. Use the appropriate parameters to pass values in and out of methods.Johnson 85 83 77 91 76Aniston 80 90 95 93 48Cooper 78…
b) Write a main method that calls the method inclusive 10 per line. This should output the following numbers: 10 13 16 19 20 23 26 29 30 31 32 33 34 35 36 37 38 39 40 43 46 49 50 productDivisibleByThree for the values between 10 and 50
The nth harmonic number is defined non-recursively as: H(n)=1+1/2+1/3+1/4+...+1/n Come up with a recursive definition and use it to guide you to write a method definition for a double-valued method named “harmonic” that accepts an int parameter n and recursively calculates and returns the nth harmonic number. Write a test program that displays the harmonic numbers,  H(n)=1,2,3,4...10

Chapter 4 Solutions

Building Java Programs: A Back to Basics Approach (4th Edition)

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