The following code is supposed to compute the floor of the square root of its input value x. (Note: The floor of a number n is the largest integer less than or equal to n.) // Computes and writes floor(sqrt(x)) for // an input value x >= 0. public static void main(String args[]) { int x; // input value Scanner input = new Scanner(System.in); // initialize int result = 0; // will equal floor of sqrt(x) int temp1 = 1; int temp2 = 1; // read input x = input.nextInt(); // compute floor while (temp1 < x) { ++result; temp2 += 2; temp1 += temp2; } // end while System.out.println("The floor of the square root of "+ x +" is " + result); } // end main This program contains an error. a. What output does the program produce when x = 64? b. Run the program and remove the error. Describe the steps that you took to find the error. c. How can you make the program more user friendly and fail-safe?
Added by Cameron V.
Close
Step 1
When x = 64, the program will output "The floor of the square root of 64 is 7". b. To remove the error, we need to modify the condition in the while loop. Currently, the loop continues as long as temp1 is less than x. However, this condition will not work Show more…
Show all steps
Your feedback will help us improve your experience
Patina Herring and 64 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Problem 1: Create a class called Elevator that can be moved between floors in an N-storey building. The Elevator uses a constructor to initialize the number of floors (N) in the building when the object is instantiated. The Elevator also has a default constructor that creates a five-storey building. The Elevator class has a termination condition that requires the elevator to be moved to the main (i.e., first) floor when the object is cleaned up. Write a finalize() method that satisfies this termination condition and verifies the condition by printing a message to the output: "Elevator ending: elevator returned to the first floor." In main(), test at least five (5) possible scenarios that can occur when the Elevator is used in a building with many floors (e.g., create, move from one floor to another, etc.). Tip: Termination occurs when the instance is set to null. You may wish to investigate "garbage collection" for this exercise.
Akash M.
A variable like userNum can store a value like an integer. Extend the given program to print userNum values as indicated. (Submit for 2 points). (1) Output the user's input. Enter an integer: 4 You entered: 4 (2) Extend to output the input squared and cubed. Hint: Compute squared as userNum * userNum. (Submit for 2 points, so 4 points total). Enter an integer: 4 You entered: 4 4 squared is 16 And 4 cubed is 64!! (3) Extend to get a second user input into userNum2. Output the sum and product. (Submit for 1 point, so 5 points total). Enter an integer: 4 You entered: 4 4 squared is 16 And 4 cubed is 64!! Enter another integer: 5 4 + 5 is 9 4 * 5 is 20 import java.util.Scanner; public class OutputWithVars { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userNum = 0; System.out.println("Enter an integer: "); userNum = scnr.nextInt(); return; } }
Shelayah R.
import java.util.Scanner; public class U4_L3_Activity_Four { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a positive integer:"); int x = scan.nextInt(); if (x > 0) { while (x % 5 != 0) { x--; } for (int i = x; i >= 0; i -= 5) { if (x % 5 == 0) { System.out.print(i + " "); } } } if (x < 0) { System.out.print("error"); } } }
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD