Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Follow the steps below to complete the lab 0 9 . Open the project for Lab 0 9 Add a Java file ( create a

Follow the steps below to complete the lab09.
Open the project for Lab09
Add a Java file (create a class) name it Lab09
import java.lang.Math;
Add the main method:
public static main(String args[]){
}
Reference codes:
a. Equivalent Switch statements and if-else statement are shown below:
switch(operator){
case 1:
result =a+b;
break;
case 2:
result =a-b;
break;
case 3:
result =ab;
break;
case 4:
result =a**b;
break;
default:
System.out.print In ("Invalid operator");
break;
}
lelse this is equivalent to 'default' in the switch statement
System.out.printIn( "Invalid operator");
}
b. Conditional Operator:
int maxval, x1=5, x2=10;
maxval =(x1> x2)? x1 : x2;
The above implementation with Conditional Operator is equivalent to (this is also the algorithm to find the larger between two numbers):
if(x1> x2){
maxval = x1;
}else {
maxval = x2;
}
6. Add codes to the main method to implement the followings with switch statements, logical, relational, and arithmetic expressions.
a.//Write the name of the course in a single line comment
b./*
Write the course description in a multiline comment
*/
c. Print the values of the following variables:
int x =10; double y =5.0; float z =3.14f; String course =CSCI1010L;
i. Using println method
ii. Using printf method. Make sure to add new line escape character at the end of the format specifier.
d. Find and print the lower number between the following variables:
int var1=100; int var2=125;
e. Convert the following if-else statements into switch statements:
if(eqnType ==1){
y = m*x + c;
}
else if(eqnType ==2){
y = a*Math.pow(x,2)+ b*x + c;
}
else if (eqnType ==3){
y = a*Math.log(x);
}
else{
System.out.println("Unsupported Equation Type");
}
Make sure to declare eqnType as int type and all other needed variables as double (m, a, b, c, x, y). Also, you must initialize them (except y).
f. Use conditional operator to compare the values a and b and to find the larger value and print the larger number.
g. Compare values x and y with 3 digits of precision and implement the following logic with Condition Operator.
final double TOLERANCE =0.001;
String msg ="";
String msg1= "Absolute difference between x & y is less than the Tolerance";
String msg2= "Absolute difference between x & y is not less than the Tolerance";
if (Math.abs(x - y) TOLERANCE){
msg = msg1;
}else{
msg = msg2;
}
System.out.println(msg);
h.(Bonus)
Use Conditional Operator to Compare the values a, b, and c to find out the largest number. Print the largest number.
/* Following codes implement the problem with if block.
* Find the largest of a, b, c
*/
double largest = a;
if (b > largest){
x = b;
}
if (c > largest){
x = c;
}
7. Run the program
java
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Greed And Fear Understanding Behavioral Finance And The Psychology Of Investing

Authors: Hersh Shefrin

1st Edition

0195161211, 978-0195161212

Students also viewed these Databases questions