Discover a world of knowledge at Westonci.ca, where experts and enthusiasts come together to answer your questions. Discover the answers you need from a community of experts ready to help you with their knowledge and experience in various fields. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.

Write a complete Java program called Rooter that gets a positive integer called "start" from the user at the command line and then finds the squareroot of every number from "start" down to 0. Use a while loop to count down. Print each square root on a separate line. Include data validation to ensure the user provides a positive integer. Assume that the user enters an integer, and for validation, just check to be sure it is positive. If the validation is not passed, provide the user with suitable feedback and stay in the program to let the user try again until valid input is received. Use the Math.sqrt(double a) method to find each square root.

Sagot :

Answer:

The program in Java is as follows:

import java.util.*;

import java.lang.Math;

public class Rooter{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

 int start;

 System.out.print("Start: ");

 start = input.nextInt();

 while(start<=0){

     System.out.print("Number must be positive\nStart: ");

     start = input.nextInt();  }

 while(start>=0){

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

     start--;  }

}

}

Explanation:

This declares start as integer

 int start;

This prompts the user for input

 System.out.print("Start: ");

This gets input for start

 start = input.nextInt();

The following is repeated until the user input is valid i.e. positive

 while(start<=0){

     System.out.print("Number must be positive\nStart: ");

     start = input.nextInt();  }

The following while loop prints the square root of each number till 0

 while(start>=0){

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

     start--;  }