Question

Write a static method called printSequenceTo that takes a target value as a parameter and then...

Write a static method called printSequenceTo that takes a target value as a parameter and then prints terms from a particular numerical sequence until they add up to a value greater than or equal to the target and that returns the number of terms that were included. For example, if the following calls are made:

int n1 = printSequenceTo(3.0);
int n2 = printSequenceTo(5.5);

The following output should be produced:

1/2 + 2/3 + 3/4 + 4/5 + 5/6 = 3.5500000000000003
1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746

The variable n1 is set to 5 because it took 5 terms from the sequence to get a sum that is at least 3.0. The variable n2 would be set to 8 because it took 8 terms to get a sum that is at least 5.5. You are to exactly reproduce the format of this output. Notice that the sum is not rounded.

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

Java Code:

class Main {

public static int printSequenceTo(double n)

{

int count = 1;

double sum = ((double)count/(count+1));

System.out.print(count + "/" + (count+1));

while(sum < n)

{

count++;

sum += ((double)count/(count+1));

System.out.print(" + " + count + "/" + (count+1));

}

System.out.println(" = " + sum);

return count;

}

public static void main(String[] args) {

int n1 = printSequenceTo(3.0);

System.out.println("Sequence took " + n1 + " terms");

int n2 = printSequenceTo(5.5);

System.out.println("Sequence took " + n2 + " terms");

}

}

OUTPUT:

1/2 2/3 + 3/4 +4/5 +5/6 3.5500000000000003 Sequence took 5 terms 1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746 Sequence took 8 terms

Add a comment
Know the answer?
Add Answer to:
Write a static method called printSequenceTo that takes a target value as a parameter and then...
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
  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in);...

    import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In:"); int nn = inp.nextInt(); int n0, n1, n2, n3, n4;    //Change only the code below //Set n0, n1, . . . n4 to each digits in nn by replacing //the blanks _A_, _B_, ect //Hint: the ones digit of 1234 is 1234%10 // the hundredth digit of 31415 is (31415/100)%10 n0 = _A_; n1 = _B_; n2 = _C_; n3 = _D_;...

  • Description The purpose of this challenge is to various flow looping and control structures. This challenge...

    Description The purpose of this challenge is to various flow looping and control structures. This challenge displays a menu and asks the user to perform various tasks. Requirements Show a menu to the user with the following choices: a, b, c, q (Show the menu once and use a do-while statement to prompt the user for a menu command): a – Display all odd numbers between n1 and n2. Ask the user to enter two numbers, n1 and n2, using...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...

    Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will receive a stack of "int" and output the sum of the squares of the elements in the stack. b- int plus_minus_rec(stack<int> stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c- void prt_chars_rev_rec(stack<char>...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

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