All the input from the user should be from the command line (use the Scanner obj
ID: 3905619 • Letter: A
Question
All the input from the user should be from the command line (use the Scanner object). You only need to create one Scanner object to handle all the input.
Do not use JOptionPane.
Put all the code for the following exercises in one program following the naming guidelines outlined above. Declare and initialize all needed variables. Unless indicated, it does not matter what the variables are initialized to as long as the code compiles. Some variables may have already been declared from a previous activity (such as boolean variable overtime).
*Declare all variables outside if statements.
1. Write an if statement that sets the int variable x equal to 100 when the int variable y is equal to 0.
2. Write an if statement that sets the boolean variable overtime equal to false when the double variable hours is less or equal to 40.
3. Write an if statement that sets the double variable overtimeRate equal to 1.5 when the boolean variable overtime equals true.
4. Write an if statement that sets the variable pi equal to 3.14 and the variable e equal to 2.718 if the boolean variable mathConstants is set to true.
5. Use a nested if statement to test the following conditions:
If a boolean variable called sale is set to true, do the following:
If the int variable price is less than 50, the double variable
discount is set to 0.02.
If the int variable price is 50 or greater, the double variable
discount is set to 0.04.
6. Use a logical operator to test the following two conditions. Use one if statement:
If a boolean variable called sale is set to true and if the int variable
price is greater than 50, the double variable discount is set to 0.01.
For the next two questions, the user must be prompted for values.
7. Prompt the user for two characters and store them in two char variables. Compare the variables and print “<firstChar> is less than <secondChar>” if the first char is less than the second char. Otherwise, print “<firstChar> is greater than <secondChar>” if the first char is greater than the first char. Otherwise, print “<firstChar> is equal to <secondChar>” if they are equal.
If user enters ‘A’ then ‘B’ print:
A is less than B
If user enters ‘B’ then ‘A’ print:
B is greater than A
If user enters ‘A’ then ‘A’ print:
A is equal to A
Note: The output above would be printed if the user entered the characters A and B. If the user enters other letters, those letters would be printed. This is the power of variables!
8. Declare 2 integer variables. Prompt the user for two numbers.
Store the values into variables using the Scanner object.
If the second number is a multiple of the first number, display “<second number> is a multiple of <first number>”. Otherwise display “<second number> is not a multiple of <first number>”
Hint: Use the Modulus arithmetic operation.
Explanation / Answer
Given below is the code for the question. Most of the tasks don't do any output except the last 2 tasks 8 and 9. Tasks 1-7 only set values of some variables based on some conditions.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int x = 0, y = 0;
boolean overtime = true;
double hours = 40;
double overtimeRate = 0;
double pi = 0, e = 0;
boolean mathConstants = true;
boolean sale = true;
int price = 0;
double discount = 0;
//1. set x = 100 when y = 0
if(y == 0)
x = 100;
//2. set overtime = false wehn hours <= 40
if(hours <= 40)
overtime = false;
else
overtime = true;
//3. set overtimerate
if(overtime)
overtimeRate = 1.5;
//4. set math constants
if(mathConstants)
{
pi = 3.14;
e = 2.718;
}
//5. set discount
if(sale)
{
if(price < 50)
discount = 0.02;
else
discount = 0.04;
}
//6. usng one if statment to set discount to 0.01
if(sale && price > 50)
discount = 0.01;
//7. compare characters
char firstChar, secondChar;
System.out.print("Enter 2 characters separated by space: ");
firstChar = keyboard.next().charAt(0);
secondChar = keyboard.next().charAt(0);
if(firstChar < secondChar)
System.out.println(firstChar + " is less than " + secondChar);
else if(firstChar > secondChar)
System.out.println(firstChar + " is greater than " + secondChar);
else
System.out.println(firstChar + " is equal to " + secondChar);
//8.check for multiple
int num1, num2;
System.out.print("Enter first number: ");
num1 = keyboard.nextInt();
System.out.print("Enter second number: ");
num2 = keyboard.nextInt();
if(num2 % num1 == 0)
System.out.println(num2 + " is a multiple of " + num1);
else
System.out.println(num2 + " is not a multiple of " + num1);
}
}
output
====
Enter 2 characters separated by space: A B
A is less than B
Enter first number: 3
Enter second number: 9
9 is a multiple of 3