Write a
- Use scanner to read a floating-point value x.
- Compute 1.0 I x and store the result in y.
- Display x, y, and the product of x and y.
- Subtract 1 from the product of x and y and display the result.
Try your program with values of x that range from 2e−11 to 2ell. What can you conclude?
Program Plan:
- • Include the required header files.
- • Define main function.
- ○ Create an object for scanner.
- ○ Get the floating-point value from the user.
- ○ Calculate the “y” value.
- ○ Display “x”, “y” and product of “x” and “y”.
- ○ Subtract “1” from product of “x” and “y” and then display the result.
The program is used to read a floating point value then compute “y” and display “x”, “y”, product of “x” and “y” and also subtract “1” from product of “x” and “y” then display the result as follows:
Explanation of Solution
Program:
//include required header files
import java.util.Scanner;
//definition of "Approximation" class
public class Approximation
{
//definition of main method
public static void main(String[] args)
{
//create an object for scanner
Scanner keyboard = new Scanner(System.in);
//declare the required variable
float x = 0, y;
//get the input from the user
System.out.println("Enter a floating point value.");
x = keyboard.nextFloat();
//calculate "y" value
y = (float) (1.0/x);
//display "x" and "y" value
System.out.println("x value is: "+ x);
System.out.println("y value is: "+ y);
//display product of "x" and "y"
System.out.println("Product of x and y is: "+ (x*y));
/*subtract 1 from product of "x" and "y" and display the result*/
System.out.println("The difference of x*y and 1 is " + ((x*y) - 1.0));
}
}
Conclusion:
The “x” depends on user but the product of “x” and “y” will always give “1” and difference of “x * y” and “1” is always “0” because, “1” is divided by “y” and then “y” is multiplied with the “x”. So there is no change in product of “x” and “y” and difference of “x * y” and “1”.
Output:
Enter a floating point value.
2.5
x value is: 2.5
y value is: 0.4
Product of x and y is: 1.0
The difference of x*y and 1 is 0.0
Want to see more full solutions like this?
Chapter 2 Solutions
Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Introduction To Programming Using Visual Basic (11th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (8th Edition)
Concepts Of Programming Languages
- Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + day_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forward
- Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forward
- Should software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forwardPlease show the code for the Tikz figurearrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,