2.18 Java example: Salary calculation with
Using variables in expressions, rather than numbers like 40, makes a program more
general and makes expressions more meaningful when read too.
2.18.1 : Calculate salary:
2.20 Ch 2 Warm up: Variables, input, and
This content is controlled by your instructor, and is not zyBook content. Direct questions or concerns
about this content to your instructor. If you have any technical issues with the zyLab
2 . 7 Math methods
2.8 Type conversions
A calculation sometimes must mix integer and floating-point numbers. For example,
given that about 50.4% of human births are males, then 0.504 * numBirths
calculates the number of expected males in numBirths births.
2.14 Random numbers
Some programs need to use a random number. For example, a program might serve as
an electronic dice roller, generating random numbers between 1 and 6. The following
example demonstrates how to generate four random numbers between 1 and
2 . 10 Characters
2.11 String basics
Some variables should store a sequence of characters like the name Julia. A sequence
of characters is called a string. A string literal uses double quotes as in "Julia". Various
characters may be included, such as lett
3 . 9 String modify operations
3.10 Character operations
The Character class provides several methods for working with characters.
Table 3.10.1: Character methods return values. Each method must prepend
Character., as in Character.isLetter.
3.8 String access operations
A string is a sequence of characters in memory. Each string character has a position
number called an index. The numbering starts with 0, not 1.
charAt(): The notation someString.charAt(0) determines the character at a particu
3 . 8 String access operations
3.9 String modify operations
The String class has several methods for modifying strings.
Table 3.9.1: String modify methods, invoked as
someString.concat(moreString). Each returns a new String of the appropriate
2.19 Java example: Married-couple names
2.19.1 : Married-couple names with variables.
Pat Smith and Kelly Jones are engaged. What are possible last name combinations for the
married couple (listing Pat first)
3 . 2 Relational / equality ops
3.3 Multiple if-else branches
Commonly, a programmer requires more than two branches, in which case a multibranch if-else arrangement can be used.
Construct 3.3.1: Multi-branch if-else arrangement. Only 1 branch will execut
.11 Conditional expressions
If-else statements with the form shown below are so common that the language
supports the shorthand notation shown.
3.11.1 : Conditional expression.
if (condition) cfw_
myVar = expr1;
3.5 Switch statements
A switch statement can more clearly represent multi-branch behavior involving a
variable being compared to constant values. The program executes the first case whose
constant expression matches the value of the switch expression, exe
3.7 String comparisons
Two strings are commonly compared for equality. Equal strings have the same number
of characters, and each corresponding character is identical.
3.7.1 : Equal strings.
Which strings are equal?
3 . 13 Short circuit evaluation
3.14 Java example: Salary calculation with
3.14.1 : Calculate salary: Calculate overtime using branches.
The following program calculates yearly and monthly salary given an hourly wa
3 . 11 Conditional expressions
3.12 Floating-point comparison
Floating-point numbers should not be compared using =. Ex: Avoid float1 = float2. Reason: Some
floating-point numbers cannot be exactly represented in the limited available memory bits like 64
3 . 12 Floating-point comparison
3.13 Short circuit evaluation
A logical operator evaluates operands from left to right. Short circuit evaluation skips
evaluating later operands if the result of the logical operator can already be determined.
3.4 Logical operators
More operators are available for use in expressions. A logical operator treats operands
as being true or false, and evaluates to true or false.
Table 3.4.1: Logical operators.
a & b
a | b
public class InventoryItem implements Comparable
private String name;
private int uniqueItemID;
public InventoryItem(String name, int id)
this.name = name;
uniqueItemID = id;
public void setName(String newName)
name = newName;
Normally, a programmer can think in terms of base ten numbers. However, a compiler
must allocate some finite quantity of bits (e.g., 32 bits) for a variable, and that quantity of
bits limits the range of numbers that the variable can represent.
2.13 Numeric data types
int and double are the most common numeric data types. However, several other
numeric types exist. The following table summarizes available integer numeric data
Table 2.13.1: Integer numeric data types.
A variable of char type can store a single character, like the letter m or the symbol %. A
character literal is surrounded with single quotes, as in 'm' or '%'.
Figure 2.10.1: Simple char example: Arrow.
public class CharArrow cfw_
2 Constant variables
2.7 Using math methods
Some programs require math operations beyond basic operations like + and *, such as computing a
square root or raising a number to a power. Thus, Java comes with a standard Math class that has about
30 math oper
A recipe consists of instructions that a chef executes, like adding eggs or stirring
ingredients. Likewise, a computer program consists of instructions that a computer
executes (or runs), like multiplying numbers or printing a number to a
2 . 11 String basics
2.12 Integer overflow
An integer variable cannot store a number larger than the maximum supported by the
variable's data type. An overflow occurs when the value being assigned to a variable is
greater than the maximum value the variab