Question

In this problem, we will implement an nth root finder. In particular, please fill in the...

In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may not use any library functions for this question.

In this question, our program expects 3 lines, where the first line is n (the degree of root to take), the second line is a number, and the third number represents the precision. The output is a single line containing the answer followed by a newline.

For example, the following input would generate the output 20.000: 2 400 3 Additionally, the following input would generate the output 6.86: 2

47

2

import java.util.Scanner;

public class NthRootFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// Read n (for taking the nth root)
int n = Integer.parseInt(sc.nextLine());
  
// Read number to take the nth root of
int number = Integer.parseInt(sc.nextLine());
  
// Read the desired precision
int precision = Integer.parseInt(sc.nextLine());
  
// Print the answer
System.out.println(findNthRoot(number, n, precision));
}
  
private static String findNthRoot(int number, int n, int precision) {
// TODO complete this method
double
return 0;
}
}

Homework Answers

Answer #1

import java.util.Scanner;
import java.text.*;
public class NthRootFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Read n (for taking the nth root)
System.out.println("enter nth root-");
double n = sc.nextDouble();

// Read number to take the nth root of
System.out.println("enter number-");
double number = sc.nextDouble();

// Read the desired precision
System.out.println("enter precision-");
double precision = sc.nextDouble();

// Print the answer
System.out.println(findNthRoot(number, n, precision));
}

private static String findNthRoot(double number, double n, double precision) {

String hash="#.0";
double result;
n=1/n;
result=Math.pow(number,n);
for(int i=2;i<=precision;i++)
{
hash=hash+"0";
}
DecimalFormat df = new DecimalFormat(hash);
return df.format(result);
}
}

Know the answer?
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
Similar Questions
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
In this problem, you will write an implementation of BubbleSort. Your function should take in a...
In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in ascending order. For example, if you receive the following input followed by a newline: 8 7 6 5 4 3 2 1 then you should display the following output followed by a newline: 1 2 3 4 5 6 7 8 Starter code for reading the input and...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 <...
* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 < input.txt * * Reads in a text file and for each line verifies whether the word has * unique characters. * * % cat input.txt * Hello * World * * % java Assignment1 < input.txt * False * True * ******************************************************************************/ import java.util.*; public class SortInput { /* a function for checking uniqueness of characters in a word */ private static boolean isUniqueChar(String...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
This is an intro to Java Question. My current solution is giving me bad outputs. Please...
This is an intro to Java Question. My current solution is giving me bad outputs. Please show me your way of solving this. Problem 4: Min/Max Search by Value Develop a program that, given a sequence S of integers as input, produces as two output values, the first is the minimum value that appears in the sequence and the second is the maximum value that appears in the sequence. Facts ● Scanner has a method that returns a boolean indicating...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT