Task# 1 Write a program in java that takes a double value as input from user and
ID: 3567045 • Letter: T
Question
Task# 1
Write a program in java that takes a double value as input from user and prints the square of it. Add appropriate exception handling code block in case the user provides any string other than a double value.
Sample Output:
Enter a number: 3.2
The square of the nummber is: 10.24
Enter a number: 3N
The value you provided is invalid
Task# 2
Write a program in java that asks user to enter a whole number and keeps asking in case user enters wrong input. Input checking should be done within try/catch block.
Sample Output:
Enter a whole number: forty two
Incorrect input! Try again!
Enter a whole number: fortytwo
Incorrect input! Try again!
Enter a whole number: 42
You entered: 42
Explanation / Answer
1)
import java.util.Scanner;
public class DoubleCheck{
public Double value;
public static void main(String []args){
try{
System.out.println("Enter double value");
Scanner sc=new Scanner(System.in);
value=sc.nextDouble();
value=value*value;
System.out.println("the square of entered number is",+value);
}
catch(Exception e)
{
System.out.println("Enter only double type values);
}
}
}
2)import java.util.Scanner;
public class DoubleCheck{
public int value;
boolean check=true;
public static void main(String []args){
while(check)
{
try{
System.out.println("Enter whole number");
Scanner sc=new Scanner(System.in);
value=sc.nextInt();
System.out.println("you entered",+value);
check=false;
}
catch(Exception e)
{
System.out.println("Incorrect input try again");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.