CS 1401 Homework #5

Date Assigned: Tuesday, February 15, 2005

Due Date: Thursday, February 17, 2005, before the beginning of your lecture section.

Goal: the goal of this assignment is to learn how to deal with Boolean variables and if-then statements in Java.

Assignments:

Let us assume that we have a variable salary of type int, and a variable name of type String.

1. In each subproblem, write Java statements that assign, to a Boolean variable bool, the value "true" or "false" depending on whether the following conditions hold:

(a) 6 < salary

Answer: 

bool = (6 < salary);

(b) 6 < salary < 10

Answer:

bool = (6 < salary) && (salary < 10);

(c) the variable name contains the string "Gang"

Answer:

bool = name.equals("Gang");

(d) the value of the string name follows "Gang" in alphabetic order

Answer:

bool = (name.compareTo("Gang") > 0); 
(e) the salary (i.e., the value of the variable salary) is larger than 6, and the name (i.e., the value of the variable name) is different from "Gang"

Answer:

bool = (salary > 6) && !name.equals("Gang);

2. Write a piece of code that does the following:
Answer:

if (salary > 6) 
  System.out.println("The salary is larger than 6."); 
else if (salary < 6) 
  System.out.println("The salary is smaller than 6."); 
else 
  System.out.println("The salary is exactly 6."); 

if (name.equals("Gang")) 
  System.out.println("It is Gang");
else
  System.out.println("It is not Gang");