Setup IDE Write simple arithmetic expression, input/output. Using the Scanner cl
ID: 3785745 • Letter: S
Question
Setup IDE Write simple arithmetic expression, input/output. Using the Scanner class to get input Be familiar with String class and some of its methods. Find the correct way of string comparisons. Be familiar with simple control statements (if else) Be familiar with some methods in Math Class Be able to generate random number by using Random class Know how to format the output by using DecimalFormat class Understanding order of operations Using the Scanner Class with input validation. Getting familiar with Control Statements (If else, Switch) Learn to use Scanner Class to get various inputs Learn to write simple arithmetic expression Learn to use escape sequence & DecimalFormat class outputExplanation / Answer
Note: based on the lab 3 and lab 4 requirements I developed some code.Please check it.If you need any modifications I will modify.Thank You.
___________________
Lab3.java
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
public class Lab3 {
public static void main(String[] args) {
//Declaring variables
double random_number;
int min,max;
//Creating the Random Class object
Random r = new Random();
//Scanner class object is sued to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Creating the DecimalFormat class object
DecimalFormat df=new DecimalFormat("#.##");
//Getting the inputs entered by the user
System.out.print("Enter the Minimum Number :");
min=sc.nextInt();
while(true)
{
System.out.print(" Enter the Maximum Number :");
max=sc.nextInt();
if(max<min)
{
//Displaying the error message
System.out.println("** Invalid Number.Max value must be greater than minimum value **");
continue;
}
else
break;
}
//Generating the double type random number
random_number = min + (max - min) * r.nextDouble();
//Displaying the random number
System.out.print("The Random Number Generated is :"+random_number);
//Using Math class floor() method
System.out.println(" Using Math class floor() method "+Math.floor(random_number));
//Using the format() method in DecimalFormat Class Object
System.out.println("Displaying the number limiting to single decimal point :"+df.format(random_number));
}
}
_________________
output:
Enter the Minimum Number :3
Enter the Maximum Number :2
** Invalid Number.Max value must be greater than minimum value **
Enter the Maximum Number :70
The Random Number Generated is :13.644608459095817
Using Math class floor() method 13.0
Displaying the number limiting to single decimal point :13.64
_____________________
Lab4.java
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
//Declaring the variables
int result,number;
//Scanner class object is sued to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//This while loop continues to execute until the user enteres a valid number
while(true)
{
//Getting the number entered by the user
System.out.print("Enter a number between (1-4) :");
number=sc.nextInt();
//Checking whether the number is in range or not
if(number<1 || number>4)
{
//Displaying the error message
System.out.println("Invalid.number must be between 1-4 ");
continue;
}
else
break;
}
//Based on the user entered number the corresponding case will get executed
switch(number)
{
case 1:{
int res=17 / 2 + 4 ;
//Displaying the result
System.out.println("The result for 17 / 2 + 4 is "+res);
break;
}
case 2:{
int res=3 - (5 + 10 / (2 * 2));
//Displaying the result
System.out.println("The result for 3 - (5 + 10 / (2 * 2)) is "+res);
break;
}
case 3:{
int res=15 / 2 % 3 - 1;
//Displaying the result
System.out.println("The result for 15 / 2 % 3 - 1 is "+res);
break;
}
case 4:{
int res=7 * 2 - 5 / 3;
//Displaying the result
System.out.println("The result for 7 * 2 - 5 / 3; is "+res);
break;
}
}
}
}
_________________________
output:
Enter a number between (1-4) :1
The result for 17 / 2 + 4 is 12
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.