Big Java 4

Chapter 4 – Fundamental Data Types

Chapter Goals

flight board

Number Types

Primitive Types

Type Description Size
int The integer type, with range -2,147,483,648 (Integer.MIN_VALUE) . . . 2,147,483,647 (Integer.MAX_VALUE) 4 bytes
byte The type describing a single byte, with range -128 . . . 127 1 byte
short The short integer type, with range -32768 . . . 32767 2 bytes
long The long integer type, with range -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 8 bytes
double The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits 8 bytes
float The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits 4 bytes
char The character type, representing code units in the Unicode encoding scheme 2 bytes
boolean The type with the two truth values false and true 1 bit

Number Literals

Number Literals

table of number literals

Overflow

Rounding Errors

example of imprecision

Constants: final

Constants: static final

Syntax 4.1 Constant Declaration

Syntax 4.1 Constant Declaration

section_1/CashRegister.java

Your browser does not support this feature

section_1/CashRegisterTester.java

Your browser does not support this feature Program Run:

Self Check 4.1

Which are the most commonly used number types in Java?
.

Self Check 4.2

Suppose you want to write a program that works with population data from various countries. Which Java data type should you use?

Self Check 4.3

Which of the following initializations are incorrect, and why?
  1. int dollars = 100.0;
  2. double balance = 100;

Self Check 4.4

What is the difference between the following two statements?
final double CM_PER_INCH = 2.54;
and
public static final double CM_PER_INCH = 2.54;

Self Check 4.5

What is wrong with the following statement sequence?
double diameter = . . .;
double circumference = 3.14 * diameter;

Arithmetic Operators

Increment and Decrement

Integer Division and Remainder

Integer Division and Remainder

Integer Division and Remainder

integer division and remainer

Powers and Roots

Analyzing an Expression

Mathematical Methods

math methods

Converting Floating-Point Numbers to Integers - Cast

Converting Floating-Point Numbers to Integers - Rounding

Syntax 4.2 Cast

cast syntax

Arithmetic Expressions

arithmetic expressions

Self Check 4.6

A bank account earns interest once per year. In Java, how do you compute the interest earned in the first year? Assume variables percent and balance of type double have already been declared.

Self Check 4.7

In Java, how do you compute the side length of a square whose area is stored in the variable area?

Self Check 4.8

The volume of a sphere is given by

volume formula

If the radius is given by a variable radius of type double, write a Java expression for the volume.

Self Check 4.9

What are the values of 1729 / 100 and 1729 % 100?

Self Check 4.10

If n is a positive number, what is (n / 10) % 10?

Calling Static Methods

Reading Input

Reading Input - Scanner

Reading Input

a supermarket uses a scanner

A supermarket scanner reads bar codes. The Java Scanner reads numbers and text.

Syntax 4.3 Input Statement

input syntax

Formatted Output

Formatted Output

Formatted Output

ledger

 

You use the printf method to line up your output in neat columns.

Formatted Output

format specifiers

Formatted Output

section_3/Volume.java

Your browser does not support this feature Program Run:

Self Check 4.11

Write statements to prompt for and read the user’s age using a Scanner variable named in.

Self Check 4.12

What is wrong with the following statement sequence?
System.out.print("Please enter the unit price: ");
double unitPrice = in.nextDouble();
int quantity = in.nextInt();

Self Check 4.13

What is problematic about the following statement sequence?
System.out.print("Please enter the unit price: ");
double unitPrice = in.nextInt();

Self Check 4.14

What is problematic about the following statement sequence?
System.out.print("Please enter the number of cans");
int cans = in.nextInt();

Self Check 4.15

What is the output of the following statement sequence?
int volume = 10;
System.out.printf("The volume is %5d", volume);

Self Check 4.16

Using the printf method, print the values of the integer variables bottles and cans so that the output looks like this:
Bottles:         8
Cans:           24
The numbers to the right should line up. (You may assume that the numbers have at most 8 digits.)

Problem Solving: First Do It By Hand

Problem Solving: First Do It By Hand

Problem Solving: First Do It By Hand

Self Check 4.17

Translate the pseudocode for computing the number of tiles and the gap width into Java.

Self Check 4.18

Suppose the architect specifies a pattern with black, gray, and white tiles, like this:
self check tiles

Again, the first and last tile should be black. How do you need to modify the algorithm?

Self Check 4.19

A robot needs to tile a floor with alternating black and white tiles. Develop an algorithm that yields the color (0 for black, 1 for white), given the row and column number. Start with specific values for the row and column, and then generalize.

tile floor

Self Check 4.20

For a particular car, repair and maintenance costs in year 1 are estimated at $100; in year 10, at $1,500. Assuming that the repair cost increases by the same amount every year, develop pseudocode to compute the repair cost in year 3 and then generalize to year n.

Self Check 4.21

The shape of a bottle is approximated by two cylinders of radius r1 and r2 and heights h1 and h2, joined by a cone section of height h3.

Using the formulas for the volume of a cylinder, V = π r² h, and a cone section

bottle

develop pseudocode to compute the volume of the bottle. Using an actual bottle with known volume as a sample, make a hand calculation of your pseudocode.

String Type

Concatenation

Concatenation

Concatenation in Print Statements

String Input

Escape Sequences

Strings and Characters

Strings and Characters

Substrings

Substrings

section_5/Initials.java

Your browser does not support this feature Program Run:

String Operations

string operations

Self Check 4.22

What is the length of the string "Java Program"?

Self Check 4.23

Consider this string variable

String str = "Java Program";

Give a call to the substring method that returns the substring "gram".

Self Check 4.24

Use string concatenation to turn the string variable str from Self Check 23 into "Java Programming".

Self Check 4.25

What does the following statement sequence print?
String str = "Harry";
int n = str.length();
String mystery = str.substring(0, 1) + str.substring(n - 1, n);
System.out.println(mystery);;

Self Check 4.26

Give an input statement to read a name of the form “John Q. Public”.

International Alphabets and Unicode: German Keyboard

german keyboard

Hebrew, Arabic, and English

different_scripts

Chinese Script

chinese script