

| 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 |


int n = 1000000; System.out.println(n * n); // Prints –727379968, which is clearly wrong

double f = 4.35; System.out.println(100 * f); // Prints 434.99999999999994
final double QUARTER_VALUE = 0.25;
final double DIME_VALUE = 0.1;
final double NICKEL_VALUE = 0.05;
final double PENNY_VALUE = 0.01;
payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
public class Math
{
. . .
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
}double circumference = Math.PI * diameter;

Change: 0.25 Expected: 0.25 Change: 2.0 Expected: 2.0
final double CM_PER_INCH = 2.54;and
public static final double CM_PER_INCH = 2.54;
double diameter = . . .; double circumference = 3.14 * diameter;
magic number3.14
(a + b) / 2
(a + b) / 2
a + b / 2
7 + 4.0 is the floating-point value 11.0

int dollars = pennies / 100; // Sets dollars to 17
int cents = pennies % 100; // Sets cents to 29
Integer division and the %
operator yield the dollar and
cent values of a piggybank
full of pennies.



double balance = total + tax; int dollars = balance; // Error: Cannot assign double to int
double balance = total + tax; int dollars = (int) balance;
long rounded = Math.round(balance);


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.
The volume of a sphere is given by

If the radius is given by a variable radius of type double, write a Java expression for the volume.
double root = 2.sqrt(); // Error
double root = Math.sqrt(2); // Correct
System.out.print("Please enter the number of bottles: "); // Display prompt
Scanner in = new Scanner(System.in);
System.out.print("Please enter the number of bottles: ");
int bottles = in.nextInt();
System.out.print("Enter price: ");
double price = in.nextDouble();import java.util.Scanner;
A supermarket scanner reads bar codes. The Java Scanner reads numbers and text.

System.out.printf("%.2f", price);System.out.printf("%10.2f", price); System.out.printf("Price per liter:%10.2f", price);
Price per liter: 1.22

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

System.out.printf("Quantity: %d Total: %10.2f", quantity, total);
Please enter the price for a six-pack: 2.95 Please enter the price for a two-liter bottle: 2.85 Pack price per liter: 1.38 Bottle price per liter: 1.43
System.out.print("How old are you? ");
int age = in.nextInt();System.out.print("Please enter the unit price: ");
double unitPrice = in.nextDouble();
int quantity = in.nextInt();
System.out.print("Please enter the unit price: ");
double unitPrice = in.nextInt();
System.out.print("Please enter the number of cans");
int cans = in.nextInt();
int volume = 10;
System.out.printf("The volume is %5d", volume);
There are four spaces between is and 10. One space originates from the format string (the space between s and %), and three spaces are added before 10 to achieve a field width of 5.
Bottles: 8 Cans: 24The numbers to the right should line up. (You may assume that the numbers have at most 8 digits.)
System.out.printf("Bottles: %8d\n", bottles);
System.out.printf("Cans: %8d\n", cans);
Note the spaces after Cans:. Alternatively,
you can use format specifiers for the strings.
You can even combine
all output into a single
statement:
System.out.printf("%-9s%8d\n%-9s%8d\n",
"Bottles: ", bottles, "Cans:", cans);

number of pairs = integer part of (total width - tile width) / (2 x tile width) number of tiles = 1 + 2 x number of pairs gap at each end = (total width - number of tiles x tile width) / 2
int pairs = (totalWidth - tileWidth) / (2 * tileWidth); int tiles = 1 + 2 * pairs; double gap = (totalWidth - tiles * tileWidth) / 2.0;
Again, the first and last tile should be black. How do you need to modify the algorithm?
number of groups = integer part of (total width - tile width) /(4 x tile width) number of tiles = 1 + 4 x number of groupsThe formula for the gap is not changed.
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.
| Rows%2 | Columns%2 | Color |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
color = ((row % 2) + (column % 2)) % 2
Repair cost in year n = 100 + n x 1400 / 9
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
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 name = "Harry";
"Harry"
String fName = "Harry";
String lName = "Morgan";
String name = fName + lName;
"HarryMorgan"
String name = fName + " " + lName;
"Harry Morgan"
String jobTitle = "Agent"; int employeeId = 7; String bond = jobTitle + employeeId;
"Agent7"
System.out.print("The total is ");
System.out.println(total);
versus
System.out.println("The total is " + total);
System.out.print("Please enter your name: ");
String name = in.next();"He said \"Hello\""
"C:\\Temp\\Secret.txt"
System.out.printf("Price: %10.2f\n", price);


String name = "Harry"; char start = name.charAt(0); char last = name.charAt(4);
String greeting = "Hello, World!"; String sub = greeting.substring(0, 5); // sub is "Hello"
String sub2 = greeting.substring(7, 12);

past the end- start
String tail = greeting.substring(7); // Copies all characters from position 7 on

Enter your first name: Rodolfo Enter your significant other's first name: Sally R&S

Consider this string variable
String str = "Java Program";
Give a call to the substring method that returns the substring "gram".
String str = "Harry"; int n = str.length(); String mystery = str.substring(0, 1) + str.substring(n - 1, n); System.out.println(mystery);;
String first = in.next(); String middle = in.next(); String last = in.next();


