Question

If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that...

If and else statements:

1a.)

Given the following declaration:

int temperature;

Write an if statement that prints the message "The number is valid" if the variable temperature is within the range of -50 trough 150 (both values excluded).

1b.)

Given the following declarations

int x;

int y;

Write an if statement that assigns 100 to x when y is equal to 0.

1c.)

Given the following declaration:

int grade;

Write an if statement that prints the message "The number is valid" if the variable grade is within the range of 0 trough 100 (both inclusive).

1d.)

Given the following declaration:

int year;

Write an if statement that prints the message "The year is not valid" if the variable year is outside the 20th century (1901 - 2000).

1e.)

Given the following declaration:

int hours;

Write an if statement that prints the message "The number is not valid" if the variable hours is outside the range of 0 trough 80 (0 and 80 are valid values).

1f.)

Given the following declarations:

int x;

int y;

Write an if-else statement that assigns 0 to x when y is equal to 10. Otherwise, it should assign 1 to x.

1g.)

Given the following declarations:

int hours;

boolean minimum;

Write an if statement that sets the variable hours to 10 when the boolean flag variable minimun is true.

Homework Answers

Answer #1

Answers:

1a)

//&& - And - Logical operator
if(temperature>-50 && temperature<150) //-50 trough 150 (both values excluded).
{
System.out.print("The number is valid"); //using print() as a function to print
}

1b.)

//== relational operator ,equal equal to - to compare two values

if(y==0)
{
x=100;
}


1.c)

//&& - And - Logical operator
if(grade>=0 && grade<=100) //0 & 100 (both inclusive).
{
print("The number is valid");
}


1.d)

// ! - Not - Logical operator
if(!(year>=1901 && year<=2000))
{
print("The year is not valid");
}

1.e)

// ! - Not - Logical operator

if(!(hours>=0 && hours<=80))
{
print("The number is not valid");
}


1.f)


//== - Realational operator

if(y==10)
{
x=0;
}
else
{
x=1;
}


1. g)

//To compare two boolean values, we need to use == relational operator
if(minimum== true)
{
hours=10;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a method with the following header: public static int getValidInput(int low, int high, String message)...
Write a method with the following header: public static int getValidInput(int low, int high, String message) This method will return a user entered number between high and low inclusive. If a number is entered that is not between high and low, string message will be printed to the screen. If high is less than low, a -1 is returned. For example, given this call: int input = getValidInput(0, 100, “Not a valid grade, Please re- enter”); The following could occur:...
The String Class: 1a.) Given the following delcaration: String modification1; Write a statement that stores the...
The String Class: 1a.) Given the following delcaration: String modification1; Write a statement that stores the uppercase equivalent of the string object called "city" in modification1. 1b.) Write a statement that declares a String variable named city. The variable should be initialized with the string literal "Los Angeles". 1c.) Given the following declaration: int stringSize; Write a statement that stores the length of the string object called "city" in stringSize. 1d.) Given the following declaration: char oneChar; Write a statement...
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main...
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main function { int limit, // a user entered limit for a loop step, // a user entered step value x; // control variable for the loop cout << "Counting up from 1 to 10\n"; // The control variable x takes on values from 1 to 10 for (x = 1; x <= 10; x++) // for loop to count from 1 to 10 values...
What does the following variable declaration in C mean, assuming it occurs within a function called...
What does the following variable declaration in C mean, assuming it occurs within a function called my_function() ? static int x = 0 ; x retains its value between calls to my_function() x is a constant and cannot be changed within that function x is a global variable and can be accessed from other functions directly Syntax error, this is not a valid C statement.
ON PYTHON Exercise 1. For each of the relational/logical expressions listed in exercise1.py determine whether the...
ON PYTHON Exercise 1. For each of the relational/logical expressions listed in exercise1.py determine whether the expression evaluates to True or False. Place a comment after each expression with the truth value and submit the updated exercise1.py file to the dropbox. For example, x = 10 y = 11 x < y # True  insert a comment after the expression x = 10 y = 11 a = 12.5 b = -5.2 x < y # True x <...
Programming in C: 1. Given the following variable declarations: const size_t n = 50; 2. Write...
Programming in C: 1. Given the following variable declarations: const size_t n = 50; 2. Write the declaration of an an array of pointers to n memory blocks containing 16-bit signed integer values. Use 'x' for the name of the variable. 3. What is the type of the variable x? uint8_t *x[256]; 4. What type does the variable x decay to when used in an expression? uint8_t *x[256] 5. What is the data type of an array of five strings?...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
Given the following declaration char x = -12; char y = 63; if the computer uses...
Given the following declaration char x = -12; char y = 63; if the computer uses 2’s complement to represent the numbers then which statement is correct: a. The number of bits in x is equal to the number of bits in y b. The number of bits in x is less than the number of bits in y c. The number of bits in x is greater than the number of bits in y d. None of the above
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Second question Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same...
Q1: Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); Q2: Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same...