Question

In: Computer Science

java In one program, write 3 separate functions, and use them. 1) Write the function body...

java

In one program, write 3 separate functions, and use them.
1) Write the function body for each:
int squareInteger s( int x)
double squareDouble( double d)
float squareFloat ( float f)
They basically do the same thing, square a number, but use different argument datatypes.
2) Test Each function with: Using int 4, double 4.9, float 9.4
What happened when you use the correct numerical data type as input ?
What happened when you use the incorrect numerical data types as inputs ?
What is the lesson this teaches you about functions/methods ?

Solutions

Expert Solution

import java.util.*;

class DataTasks
{
int squareInteger(int x)
{
return(x*x);
}
double squareDouble(double d)
{
return(d*d);
}
float squareFloat(float f)
{
return(f*f);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
DataTasks dt=new DataTasks();
System.out.print("Enter an integer : ");
int numInt=sc.nextInt();
System.out.println("Square Integer : "+dt.squareInteger(numInt));
System.out.print("Enter a double : ");
double numDouble=sc.nextDouble();
System.out.println("Square Double: "+dt.squareDouble(numDouble));
System.out.print("Enter a float : ");
double numFloat=sc.nextFloat();
System.out.println("Square Float : "+dt.squareFloat(numFloat));
}
}

/*
Output :

Using int 4, double 4.9, float 9.4

Square Integer: 16
Square Double: 24.010000000000005
Square Float : error: incompatible types: possible lossy conversion from double to float (Oracle JDK 1.8 or above)


What happened when you use the correct numerical data type as input?
Then the answer will be the same as mentioned above.

What happened when you use the incorrect numerical data types as inputs?
Then it will raise an exception for integer , suppose you entered incorrect inputs like 4.5 for integer then it will raise an exception
"Exception in thread "main" java.util.InputMismatchException"
For double it will execute properly if your enter integer then also it will work properly even for float.


What is the lesson this teaches you about functions/methods?
Functions do return value while methods do not return any value.
functions or methods values should be correct means actual and formal variables should have the same data type, for integer, it has to be same but for double it does not affect that much

*/


Related Solutions

write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
How to use a Java program that calls for two functions. The first function is asking...
How to use a Java program that calls for two functions. The first function is asking the user for a two integer numbers, then display the sum of the numbers. The second method is asking the user for two floating point numbers, then display the product of the numbers. Call these methods addNumbers and multiplyNumbers respectively. Call the program AddAndMultiply
Please use Java language to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Write a Python program to implement one studied entropy coding method, including two separate functions for...
Write a Python program to implement one studied entropy coding method, including two separate functions for encoding and decoding. Use the lyrics of your favorite song as the message to be processed, and test the program on the message. Include the source code, and detail the program design and execution procedure in this section.
Write Java Program. (Plot the sine and cosine functions) Write a program that plots the sine...
Write Java Program. (Plot the sine and cosine functions) Write a program that plots the sine function in red and cosine in blue, as shown in Figure 14.48c. Hint: The Unicode for P is u03c0. To display -2p, use Text(x, y, "-2\u03c0"). For a trigonometric function like sin(x), x is in radians. Use the following loop to add the points to a polyline: Polyline polyline = new polyline (); ObservableList<Double> list = polyline.getPoints (); double scaleFactor = 50; for (int...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Write a program that utilizes a function called paymentCalc. In the main body of the program,...
Write a program that utilizes a function called paymentCalc. In the main body of the program, ask the user what is their principal and how many months they want the loan. If it is easier, ask the user for number of years, but don’t forget to change it to months for the calculation. Use a function to calculate their monthly mortgage payment. In the main body of the program, print out what their monthly mortgage payment will be. For simplicity,...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT