Section 1: Define Variable limitations- control statements- If ( condition ) sta
ID: 3666894 • Letter: S
Question
Section 1: Define
Variable limitations-
control statements-
If (condition) statement-
What is the if / else ladder / Describe how if else works-
Boolean Expressions-
Relational Operators-
(Define Each Symbol)
<
<=
>
>=
==
!=
How do relational operators relate to if statements?
What does java.util.Scanner and new Scanner (System.in); allow the programmer to do?
Import statement-
Constructor-
Programming Assignments
Task 1 –
Page 39. in Programming A Comprehensive Introduction
Update your program from Assignment 2, Task #2
Allow the user to input their weight for the earth weight to moon weight conversion problem. Add an if statement that prompts the user if she inputs 0 or a negative number for her earth weight.
#13. Mars’ gravity is about 17 percent less that of the earth’s. (Meaning you weigh less on the moon). Write a program that computes your affective weight on Mars.
P9. 29 in book
2nd Task: Write a simple program displaying knowledge of if conditional statements and using the Scanner input method.
Create 2 input integer (int) variables. The input numbers should not be set by the programmer but entered by the user.
Have 2 simple equations that solve for a the variable ( c ). c = a – b and c = b – a
Create statements for if (c) is a negative or non-negative number for each equation.
Display the possible outputs for in each equation.
Please enter an integer: //Prompt the user
7 // Integer entered by the user not by the programmer.
Please enter an integer: //Prompt the user
9 // Integer entered by the user not by the programmer.
a = 7 // display the value of a
b = 9 // display the value of b
a is less than b // display the difference the relationship between and b
// Create space between each output
c = a – b
c = -2
c is negative //create a statement stating whether c is negative or positive.
c = b - a
c = 2
c is non-negative
Explanation / Answer
If Else Ladder Example and Scanner Class Demo.
Scanner is a class which is used to read the input from the keyboard. Here we read the value of n from the keyboard and display the result using if else ladder.
import java.io.*;
import java.util.*;
public class IfElseIfLadder
{
public static void main(String args[])
{
int n;
System.out.println( " Enter the value of n- n=1 Hello, n=2 Hi, n=3 Good else No Match Found");
Scanner s =new Scanner(System.in); // read the value of n using the keyboard
n=s.nextInt();
if(n==1)
{
//This block will be executed only if "n" is equal to 1
System.out.println("Hello");
}
else if(n==2)
{
//This block will be executed only if "n" is equal to 2
System.out.println("Hi");
}
else if(n==3)
{
//This block will be executed only if "n" is equal to 3
System.out.println("Good");
}
else
{
System.out.println("No Match Found");
}
}
}
Output:
Enter the value of n- n=1 Hello, n=2 Hi, n=3 Good else No Match Found
3
Good.
program displaying knowledge of if conditional statements and using the Scanner input method.
import java.io.*;
import java.util.*;
public class Scanner_Demo
{
public static void main(String args[])
{
int a,b,c;
System.out.println(" Enter the values a and b");
Scanner s = new Scanner(System.in);
a=s.nextInt();
b=s.nextInt();
System.out.println(" Value of a is : " +a);
System.out.println(" Value of b is : " +b);
if (a<b)
System.out.println(" a is less than b");
else
System.out.println(" a is greater than b");
c=a-b;
System.out.println(" Value of c=a-b is : "+ c);
if(c>0)
System.out.println(" Value of c is positive");
else
System.out.println(" Value of c is negative");
}
}
Output:
Enter the values a and b 3 1
Value of a is 3
Value of b is 1
a is greater than b
Value of c=a-b is 2
C is positive
If Else Ladder Example and Scanner Class Demo.
Scanner is a class which is used to read the input from the keyboard. Here we read the value of n from the keyboard and display the result using if else ladder.
import java.io.*;
import java.util.*;
public class IfElseIfLadder
{
public static void main(String args[])
{
int n;
System.out.println( " Enter the value of n- n=1 Hello, n=2 Hi, n=3 Good else No Match Found");
Scanner s =new Scanner(System.in); // read the value of n using the keyboard
n=s.nextInt();
if(n==1)
{
//This block will be executed only if "n" is equal to 1
System.out.println("Hello");
}
else if(n==2)
{
//This block will be executed only if "n" is equal to 2
System.out.println("Hi");
}
else if(n==3)
{
//This block will be executed only if "n" is equal to 3
System.out.println("Good");
}
else
{
System.out.println("No Match Found");
}
}
}
Output:
Enter the value of n- n=1 Hello, n=2 Hi, n=3 Good else No Match Found
3
Good.
program displaying knowledge of if conditional statements and using the Scanner input method.
import java.io.*;
import java.util.*;
public class Scanner_Demo
{
public static void main(String args[])
{
int a,b,c;
System.out.println(" Enter the values a and b");
Scanner s = new Scanner(System.in);
a=s.nextInt();
b=s.nextInt();
System.out.println(" Value of a is : " +a);
System.out.println(" Value of b is : " +b);
if (a<b)
System.out.println(" a is less than b");
else
System.out.println(" a is greater than b");
c=a-b;
System.out.println(" Value of c=a-b is : "+ c);
if(c>0)
System.out.println(" Value of c is positive");
else
System.out.println(" Value of c is negative");
}
}
Output:
Enter the values a and b 3 1
Value of a is 3
Value of b is 1
a is greater than b
Value of c=a-b is 2
C is positive
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.