Concept explainers
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
In other words, the method should generate the following sequence:
You may assume that the parameter n is nonnegative.
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?
Chapter 4 Solutions
Building Java Programs: A Back to Basics Approach (4th Edition)
Additional Engineering Textbook Solutions
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
Big Java Late Objects
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Survey of Operating Systems, 5e
- 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), for n = 1,2,3,⋯,10.arrow_forwardjava Write a recursive method largestDigitthat accepts an integer parameter and returns the largest digit value that appears in that integer. Your method should work for both positive and negative numbers. If a number contains only a single digit, that digit's value is by definition the largest. The following table shows several example calls: Call Value Returned largestDigit(14263203) 6 largestDigit(845) 8 largestDigit(52649) 9 largestDigit(3) 3 largestDigit(0) 0 largestDigit(-573026) 7 largestDigit(-2) 2 Obey the following restrictions in your solution: You may not use a String, Scanner, array, or any data structure (list, stack, map, etc.). Your method must be recursive and not use any loops (for, while, etc.). Your solution should run in no worse than O(N) time, where N is the number of digits in the number.arrow_forwarda- Write a method called even ToZero that takes as parameter an integer 'limit'. The method prints on the screen all the even numbers from 'limit' down to 0 (inclusive). Use the following method header: public static vold even ToZero(int limit) b- Usc the above method to write a test program which rcads from the uscr a positive integer and prints all the even numbers from this integer down to 0 via invoking the method even ToZero. Note: Assume that the user will enter a positive integer. (Do ror check if the inpul is positive of not). Sample Run 1: Enter a positive inLeger: 5 The even numbers from 5 down to-0 ace 4 2 0 Sample Run 2: Enter a positive integer: 9 The ever numbers from 9 down to 0 ace &6 nal Examarrow_forward
- : In the classic problem FizzBuzz, you are told to print the numbers from 1 to n. However,when the number is divisible by 3, print "Fizz''. When it is divisible by 5, print "Buzz''. When it isdivisible by 3 and 5, print"FizzBuzz''. In this problem, you are asked to do this in a multithreaded way.Implement a multithreaded version of FizzBuzz with four threads. One thread checks for divisibilityof 3 and prints"Fizz''. Another thread is responsible for divisibility of 5 and prints"Buzz''. A third threadis responsible for divisibility of 3 and 5 and prints "FizzBuzz''. A fourth thread does the numbers.arrow_forwardJava program I need help writing a program that has 3 methods: the main method, a method that swaps the digit pairs, and another method that swaps letter pairs. first you would ask the user for a number and call the method that swaps the digit pairs to swap the number shown in the picture. Then ask the user to input a string of letters and numbers and call the method that swaps lettter pairs to swap the letters as shown in the picture. in the method that swaps digit pairs, it accepts an integer for its parameter and then returns a new integer that has each pair of digits swapped in order. if it is an odd number of digits, leave the leftmost digit in its original place. the method that swaps digit pairs shouldnt use the string class. but for the swap letter pairs, it is similar operation as the swap digit pairs method but accepts a string as a parameter and return the swapped string. it can use string class.arrow_forwardWhat does this Method do? public static int myFun(int n) { if (n < 0) { } return 0; } else { } int currentNumber = n % 2 return currentNumber + myFun(n - 2); == 0 ? n n - 1; This method that takes an integer n and returns the sum of all odd positive integers less than or equal to n. This method that takes an integer n and returns the sum of all even positive integers less than or equal to n. This method that takes an integer n and returns the sum of all positive integers less than or equal to n. This method that takes an integer n and recursively find a value that is even or odd.arrow_forward
- Write a static method count Evens() that has four integer parameters, and returns the count of parameters where the value is an even number (i.e. evenly divisible by 2). Ex: If the four parameters are: 1 22 11 40 then the returned count will be: 2 Hint: Use the modulo operator % to determine if each number is even or odd. Your program must define the function: public static int count Evens (int num1, int num2, int num3, int num4)arrow_forwardWrite a python code using IntegerRoot class that calculates integer nth root of a number m ifits nth root is an integer. For example: (m,n) where m = 1881676372240757194277820616164488626666147700309108518161,n = 3, then the integer nth root of m (i.e. the integer cubic root of m ) is: rgives an out put r = 12345678900987654321.arrow_forward1. Write a program that takes three numbers (a,b,c) from the user as input and finds the median of the three. The program has a method called median that takes 3 numbers as the parameter and returns the median. Use if-then-else statements to find the median. You can assume the three numbers are not the same. Sample Example: Enter 3 numbers 10 13 The median is 10 public class Median{ public static void main(String[] args){ Scanner input = new int a = int b = int c =arrow_forward
- You are required to write a program to count the quantity of elements in a list of floating-point numbers that is larger than the average value of the floating-point numbers. To solve this problem, you need to write the following methods: 1) readNumOfElem(input) – the method asks the user to enter the quantity of floating-point numbers via console. This method receives a reference variable named input for the purpose of reading the quantity. This method returns the quantity of floating-point numbers. 2) readNumbers(input,numOfElem) - the method asks the user to enter floating-point numbers that will be stored in an array. The length of the array is the same value as the quantity of floating-point numbers which is numOfElem. This method receives a reference variable named input for reading the floating-point numbers. This method returns the array filled with the floating-point numbers inputted. 3) calcAverage(numArray) - the method calculates the average of the floating-point numbers…arrow_forwardWrite a static recursive method called mrecursiv that displays all of the permutations of the characters in a string passed to the method as its argument. For example, the character sequence abc has the following permutations: acb, bac, bca, cab, cba. Then Write a static method called getInput that get an input string from the user and passed it to the mrecursiv method written above in a method call.arrow_forwardIn Java (Use the isPrime Method) PrimeNumberMethod.java, provides the isPrime(int number) method for testing whether the number is prime. Use this method to find the number of prime numbers less than 10000. Below are some clarifications for the Java program (a) The lab is to COUNT the prime numbers less than 10000. Do NOT list the prime numbers. (b) A pair prime is two prime numbers whose difference is 2. For e.g. 3,5; 11,13; 17, 19; etc. We can use IsPrime function to count how many pair primes are there in a range from 2 to 100 for example. Create a function pairPrime, that takes one argument. pairPrime should return the number of pair primes between 2 and the argument passed. Use the pairPrime function to count the pair primes between 2 and 1000. Make use of the IsPrime function to solve this task. (This feature is worth 8 points). Use the compile/run button to test tour program. Include the output displayed. It will be similar to: command>javac…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education