Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rooter.html Home Grades Modules Announcements Syllabus Download Rooter.html (1.6

ID: 3592611 • Letter: R

Question

Rooter.html Home Grades Modules Announcements Syllabus Download Rooter.html (1.68 KB) Write a complete Java program called "Rooter" (without the quotation marks) that Prompts the user to enter a singe posh ive integer or a single double value Checks whether the user did enter a single positive integer or double, and if the user didn't, prints an appropriate message and repeats the initial prompt until the user does enter a single positive integer or double. Uses a while loop (once the user enters valid data) and the Math.sqrt(0 to print on separate lines the square root of the number entered and the square roots of numbers that are smaller than the original number by a positive integral amount but greater than or equal to zero (but do not try to print out the square root of a negative number!) Uses the default ouput of Math.sqrt0 when printing the square roots. People For example: if the user enters 5.56, your program should output the following: 2.3579652245103193 2.1354156504062622 1.8867962264113207 1.5999999999999999 1.2489995996796794 0.748331477354788 But if the user enters 5.56 7 then you should repeat the prompt, because the entry is not a single positive integer or double value (There's a space between the 6 and the 7). Other examples of invalid input will be given in class Up to twenty-five points will be deducted if the graders or I find an example of input that your program doesn' handle correctly Questions on this assignment should be asked by the end of class on the Friday before the assignment is due

Explanation / Answer

Rooter.java

import java.io.*;

public class Rooter {

public static void main(String[] args) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String value; //We will take the input value as string

boolean isValueInt,isValueDouble;

Rooter rooter=new Rooter();

//This is a infinite loop which will keep prompting the user until he gives right value

while(true)

{

System.out.println("Please enter a single positive integer or a single double value");

value = br.readLine();

isValueInt = rooter.isInt(value);

if(isValueInt)

{

break; //It will break the loop if value is a single positive integer

}

isValueDouble = rooter.isDoublet(value);

if(isValueDouble)

{

break; //It will break the loop if value is a single positive double value

}

}

if(isValueInt)

{

int i = Integer.parseInt(value); //It converts the string value to Integer

//It will print the root values upto the value greater than 0

while(i >= 0)

{

System.out.println(Math.sqrt(i));

i--;

}

}

else

{

double d = Double.parseDouble(value);//It converts the string value to Double

//It will print the root values upto the value greater than 0

while(d >= 0)

{

System.out.println(Math.sqrt(d));

d--;

}

}

}

public boolean isInt(String value)

{

try

{

int i = Integer.parseInt(value);

/*If the value is not an integer then it will automatically throw an exception

* which will be handled by CATCH block. From the CATCH block we will return false*/

if(i >= 0)

{

return true; //If it's a positive value,then returns true,else false

}

else

return false;

}

catch(Exception e)

{

return false;

}

}

public boolean isDoublet(String value)

{

try

{

double d = Double.parseDouble(value);

/*If the value is not an double then it will automatically throw an exception

* which will be handled by CATCH block. From the CATCH block we will return false*/

if(d >= 0)

{

return true;//If it's a positive value,then returns true,else false

}

else

return false;

}

catch(Exception e)

{

return false;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote