front 1 Write a program that demonstrates the approximate nature of floating-point values by performing the following task:
-Use Scanner to read a floating-point value x
-Computer 1.0/x and store the result in y
-Dispay x,y, and the product of x and y
-Subtract 1 from the product of x and y and display the result
Try your program with values of x that range from 2e-11 to 2e11. What can you conclude? | back 1 Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a floating-point value");
float x = keyboard.nextFloat();
float y = 1.0F/x;
float product = x * y;
System.out.println("x is: " + x);
System.out.println("y is: " + y);
System.out.println("the product of x and y is: " + product);
product--;
System.out.println("product after subtracting 1 is: " + product);
You can conclude that the product of x and y is always 1. |
front 2 Write a program that demonstrates type casting of double values by performing the following tasks:
-Use Scanner to read a floating-point value x
-Type cast x to an int value and store the result in y
-Display x and y clearly labeled
-Type cast x to a byte value and store the result in z
-Display x and z clearly labeled
Try your program with positive and negative values of x that range in magnitude from 2e-11 to 2e11. What can you conclude? | back 2 Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a floating-point value");
float x = keyboard.nextFloat();
int y = (int)x;
System.out.println("x (floating-point) is: " + x);
System.out.println("y (int) is: " + y);
byte z = (byte)x;
System.out.println("z (byte) is: " + z);
You can conclude that y and z will give unexpected results if x is greater or less than its min and max value. |
front 3 Write a program that demonstrates the operator % by performing the following tasks:
-Use Scanner to read a floating-point value x
-computer x % 2.0 and store the result in y
-Display x and y clearly labeled
-Type cast x to an int value and store the result in z
-Display x, z, and z % 2 clearly labeled
Try your program with positive and negative values of x. What implications do your results have for deciding whether a negative integer is odd? | back 3 Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a floating-point value");
float x = keyboard.nextFloat();
float y = x % 2.0F;
System.out.println("x is: " + x);
System.out.println("y is: " + y);
int z = (int)x;
System.out.println("z is: " + z);
System.out.println("z % 2 is: " + (z % 2));
If z = -1 then the given integer is odd. |
front 4 If u = 2, v = 3, w = 5, x = 7 and y = 11, what is the value of each of the following expressions, assuming int variables?
(a)u + v * w + x
(b)u + y % v * w + x
(c)u++/v + u++ * w | |
front 5 What changes to the ChangeMaker program in Listing 2.3 are necessary if it also accepts coins for one dollar and half a dollar? | back 5 (1)
Variables for dollars and halfDollars need to be added.
(2)
The following lines of code need to be added directly after "originalAmount = amount":
dollars = amount / 100;
amount = amount % 100;
halfDollars = amount / 50;
amount = amount % 50;
(3)
Add code to print dollars and half dollars before printing quarters |
front 6 If the int variable x contains 10, what will the following Java statements display?
System.out.println("Test 1" + x * 3 * 2.0);
System.out.println("Test 2" + x * 3 + 2.0);
Given these results, explain why the following Java statement will not compile:
System.out.println("Test 3" + x * 3 - 2.0); | back 6 Test 1 60.0
Test 2 302.0
The statement does not compile because "-" is not a valid operator for string concatenation. |
front 7 Write some Java statements that use the String methods indexOf and substring to find the first word in a string. We define word to be a string of characters that does not include whitespace. For example, the first word of the string "Hello, my good friend!" is the string "Hello," and the second word is the string "my". | back 7 String sentence = "Hello, my good friend!";
int whitespace = sentence.indexOf(" ");
System.out.println("First word is: " + sentence.substring(0, whitespace)); |
front 8 Repeat the previous exercise, but find the second word in the string. | back 8 String sentence = "Hello, my good friend!";
int firstWhitespace = sentence.indexOf(" ");
int secondWhitespace = sentence.indexOf(" ", firstWhitespace + 1);
System.out.println("Second word is: " + sentence.substring(firstWhitespace + 1, secondWhitespace)); |
front 9 What does the following Java statement display?
System.out.println("\"\tTest\\\\\rIt\'");
Does replacing the r with an n make a difference in what is displayed? | back 9 It' Text\\
When "r" is replaced with "n" the text displayed is:
" Test\\
It' |
front 10 Write a single Java statement that will display the words one, two, and threee, each on its own line. | back 10 System.out.println("one\ntwo\nthree"); |
front 11 What does the Java code
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a string.");
int n = keyboard.nextInt();
String s = keyboard.next();
System.out.println("n is" + n);
System.out.println("s is" + s);
display when the keyboard input is 2istheinput? | back 11 You get an InputMismatchException because "2istheinput" cannot be converted to an int.
To make this work you need a space after the 2: "2 is the input" |
front 12 Wht does the Java code
Scanner keyboard = new Scanner(System.in);
keyboard.useDelimiter("y");
System.out.println("Enter a string.");
String a = keyboard.next();
String b = keyboard.next();
System.out.println("a is" + a);
System.out.println("b is" + b);
display when the keyboard input is: By theprickingof my thumbs | back 12 a is B
b is theprickingof |
front 13 Repeat the previous exercise, but change next to nextLine in the statement that assigns a value to b. | back 13 a is B
b is y theprickingof my thumbs |
front 14 Many sports have constants embedded in their rules. For example, baseball has 9 innings, 3 outs per inning, 3 strikes in an out, and 4 balls per walk. We might encode the constants for a program involving baseball as follows:
public static final int INNINGS = 9;
public static final int OUTS_PER_INNING = 3;
public static final int STRIKES_PER_OUT = 3;
public static final int BALLS_PER_WALK = 4;
For each of the following popular sports, give Java named constants that could be used in a program involving that sport:
-Basketball
-American football
-Soccer
-Cricket
-Bowling | back 14 Basketball:
public static final int TIME_PERIODS = 4;
public static final int PLAYER_FOULS_ALLOWED = 5;
public static final int POINTS_PER_FREE_THROW = 1;
American Football:
public static final int POINTS_PER_TOUCHDOWN = 6;
public static final int POINTS_PER_EXTRA_POINT = 1;
public static final int POINTS_PER_FIELD_GOAL = 3;
public static final int POINTS_PER_SAFETY = 2;
Soccer:
public static final int PLAYERS_PER_TEAM = 11;
public static final int TIME_PERIODS = 2;
public static final int GOALIES_PER_TEAM = 1;
Cricket:
public static final int PLAYERS_PER_TEAM = 11;
public static final int INNINGS_PER_GAME = 2;
Bowling:
public static final int FRAMES_PER_GAME = 10;
public static final int MAX_SCORE = 300;
public static final int PINS_PER_FRAME = 10; |
front 15 Repeat Exercise 18 in Chapter 1, but define and use named constants. | back 15 public static final int WIDTH = 120;
public static final int HEIGHT = 120;
public void paint(Graphics g)
{
g.drawOval(40, 40, width, height);
g.drawOval(140, 40, width, height);
g.drawOval(240, 40, width, height);
g.drawOval(90, 130, width, height);
g.drawOval(190, 130, width, height);
} |
front 16 Define named constants you could use in Programming Project 8 in Chapter 1. | back 16 public static final int ARC_RADIUS_1 = 100;
public static final int ARC_RADIUS_2 = 100;
public static final int ARC_ENDING_ANGLE = 180; |