Java software solutions self review chapter 2
f. 100 / (2/2) /2/2
100 /1 /2 = 100 /2 = 50
a. 15 + 7 * 3
15 + 21 = 36
c. 3 * 6 + 10 / 5 + 50
18 + 2 + 5 = 25
d. 27 % 5 + 7 % 3
2 + 1 = 3
e. iResult = (int) val1/ num2;
2 casting as an int
c. fResult = val1 / num2;
2.4 floating point division
b (15 + 7) * 3
22 * 3 =66
a. iResult = num1 /num2;
3
b. fResult = num1 / num2;
3.0 fresult is double
d. fResult = (double)num1/ num2;
3.4 casting floating point division
27 What value is contained in the integer variable result after the following sequences of statements is executed? result = 27; result = result + 3; result = result / 7; result = result * 2;
30 /7 = 4 * 2 = 8 no remainder and int value
39 three corners of a rectangel drawn using the java coordinate system have coordinates (3,4), (3,7) and (8,7). what are the coordinates of the fourth corner?
4th corner (3,7) top right corner
a. how many variables are declared?
5 variables, count, value total, MAX_VALUE, and myValue
28 what value is contained in the integer variable result after the following sequence of statements is executed? int base: int result; base = 5; result = base + 3; base = 7;
5+ 3= 8, 7 does not get retroactively assigned to base
e. 100 / 2 /2 /2
50 /2 /2 = 25 /2 = 12
41 how many bits are needed to store a color picture that is 300 pixels wide and 200 pixels high? assume color is represented using the RGB technique described in the chapter no special compression technique is used?
8 bits per number then numbers per pixel 300 by 200 is 8 * 3 * 300 * 200 = 1440,000 bits.
45 what is a bounding rectangle
A bounding rectangle is an imaginary rectangle that surrounds a curved shape, such as an oval, in order to define the shape's width, height, and upper left corner.
9 what is a variable declaration?
A variable declaration establishes the name of a variable and the type of data that it can contain. A declartion may also have an optional initialization,which gives the variable an initial value.
26 For each of the follwing expressions state whether they are valid or invalid. if invalid explain why?
Answer a through d
36 identify which line of the GasMileage program does each of the following
Answer a through d
32 identify the following as either widening or narrowing conversions.
Answer a through e.
35. Given the following declarations what result is stored by each of the following assignment statements. int iResult, num1 = 17, num2 = 5; double fresult, val1 = 12.0 val2 = 2.34
Answer a through e.
48 what would be the result of making each of the following changes separately to the Snowman program? You may make the change, compile and run the program and observe and report the results. explain what you observe.
Answer questions a through e
25 what is the value of the following expressions:
Answer questions a through f.
23 if an interger variable diameter currently holds the value 5, what is its value after the following statement is executed? explain?
Diameter holds value 20, (5) * 4 result stored back into diameter.
34 Assuming result is a float variable that contains the value 27.32 and the value is an int variable that contains the value 15. what are the values of each of the variables after the following assignment statement is executed? explain? value = (int) result;
During the execution of the statement, the value stored in result is read and then transformed into an int as it is being copied into the memory location represented by value, but the result variable itself is not changed, so it remains equal to 27.32 whereas value becomes 27.
33 Assuming result is a float variable and value is an int variable, what type of variable will value be after the assignment statement is executed? explain? result = value;
During the execution of the statement, the value stored in the value is read and transformed no a float as it is being copied into the memory location represented by results, but the value variable itself is not changed so value will remain an int variable after the assignment statement.
13. Briefly describe three reasons for using a constant in a program instead of a literal value.
First by carefully choosing the name of the constant, more understandable than just using the literal value. Using a constant means the literal will not be changed somewhere in the program. if you rewrite using a different literal you will only need to change the value once.
6. what output is produced by the following statement?what is produced if the inner parenthesis are removed? System.out.println("It is good to be " + ( 5+ 5));
It is good to be 10 if parenthesis removed It is good to be 55 (concatenation)
42 what is the difference between a java application and a java applet?
Java applet executed using a Web browser. bytecode form of Java applet is pulled across the internet from another computer and executed locally. a Java application is a Java program that can standard on its own. it does not require a web browser to execute.
40 two corners of a square drawn using the Java coordinate system have the coordinates (5,10),and (8, 10). What are the coordinates of the other two corners?
Length is 3 other two corners are (5,13) and (8,13).
a. MID set to 120 instead of 150
MID is added to all horizontal components snowman shifts a little bit to the left.
24. what is operator precedence?
Operator precedence is the set of rules that dictates the order in which operators are evaluated in an expression.
20 How many characters are supported by the ASCII character set, the extended ASCII character set and the Unicode character set?
Original ASCII 128 characters extended ASCII 256 characters and UNICODE 65,536 characters.
21 what is the result of 19%5 when evaluated in a Java expression? explain?
Result is 4. remainder % returns the remainder after division 19 divided by 5 leaves a remainer of 4
22 what is the result of 13/4 when evaluated in a Java expression? explain?
Results 13/4 is 3 not 3.25 because both are integers / performs integer division remainder is truncated.
c. sets up the Scanner object scan to read from the standard input stream.
Scanner scan = new Scanner (System.in);
b. creates a Scanner object.
Scanner scan = new Scanner(System.in);
37 Assume you already have instantiated a Scanner object named myScanner an an int variable named vlaue as follows in your program: Scanner myScanner = new Scanner(Sytem.in); int value = 0; Write a program that wil ask the user to enter thier age, and store the response in value.
System.out.print("Enter your age in years : " ); value = myScanner.nextInt();
8. write a single println statement that will output the following exactly as shown (including line breaks and quotation marks). "I made this letter longer than usual because I lack the time to make it short" Blaise Pascal
System.out.println("\" I made this letter longer than" + "usual because I lack the time to \nmake it short.\"" + "\n\tBlaise Pascal");
44 what is wrong with the following HTML applet tag? Explain; <applet code = " DrawHouse.java" width = "400" height = " 300"> </applet>
The code tag should indicate a bytecode file, such as DrawHouse.class it is supposed to be "ready to run"
19 what is a character set?
a list of characters in a paarticular order. defines valid characters that a particular programming language will support. Java uses Unicode character set.
1. what is a string literal?
a string literal is a sequence of characters delimited by double quotes.
15 how many values can be stored in an integer value?
an integer variable can store only one value at a time. when a new value is assigned tyhe old one is overwritten.
14 what is primitive data? How are primitive data types different from objects?
basic values such as numbers or characters. Objects are more complex and usually contain primitive data that help define them.
d. in the statment that creates smile 190 is changed to 10?
by changing the start angle of the smile arc to 10 , the starting point is now on the right side of the snowman's face direction of arc is still counterclockwise arc curves downward. It is a frown instead of a smile.
16 what are the four integer data types in Java? how are they different?
byte, short, int and long, they vary by how much memory they take therefore how large a number they can hold.
c. which of the variables are given an inital value?
count, MAX_VALUE, and myValue are each gien an initial value.
3. what is a parameter?
data passied inot a method when it is invoked. method uses that data to perform the service it provides.
c.just before the last two statements we add page.setColor(Color.blue);
hat is now blue.
a. tells the program that we will be using the Scanner class.
import java.util Scanner
11. your program needs a variable of type int to hold the number of CDs in a music collection. the initial value should be zero. Write a declaraion statement for the variable?
int numCDs = 0;
c. result = ( 5 + 2(;
invalid no close on ()
b. result = ( 5 + 2 * ( 15 - 3);
invalid two open () and one closed
29 what is an assignment operator?
it combines an operation with assignment. += operator performs addition then stores the value back into the variable on the left hand sign.
18 what type does Java automatically assign to a floating point literal? How can you indicate that an integer literal should be considered a different type?
java assigns a floating point literal data type double if you append an F or f 12.34f, java will assing it type float.
17 what type does Java automatically assign to an integer literal? How can you indicate that an integer literal should be considered a different type?m
java assigns an integer literal as the data type int. if you append it with an l 1234L java will assign it the type long.
d. Based on the above declarations, is the following assignment statement legal? explain. myValue = 100;
legal myValue is a variable of type int and 100 in is literal
d. Reads an integer from the standard input stream.
miles = scan.nextInt();
b. int to byte
narrowing
e. based on the above dedclarations, is the following assignment statement legal : explain. MAX_VALUE = 50;
not legal MAX_VALUE declared final variable only can be assigned initial value.
4. what output is produced by the following code fragment? System.out.println("one "); System.out.print ("two "); System.out.println("three ");
one two, three
46 assuming you have a graphics object called page, write a statement that will draw a square with a side length of 50, such that its upper left corner is at point (16,12)?
page.drawRect(16, 12, 50, 50);
47 same thing page, write a sequence of statements that will draw a blue rectangle with a height of 20 and a width of 40, such that its upper-left corner is at point (30,35)?
page.setColor(Color.blue); page.fillRect(30,35, 40, 20);
43 when is an applet's paint method invoked?
paint method is invoked by the system whenever the applets graphic element needs to be displayed or (painted) on the screen. examples include when the applet first runs or when a window that was covering the applet's window is removed.
38 how can a black and white pitcure be represented using 1s and 0s?
pixels 0 are white and 1 are black using thousands of pixels realistic pictures can be created.
2. what is the difference between the print and println methods?
println performs a carraige return so what is printed appears on next line both are part of System.out print method appears on same line
5. what output is produced by the following code fragment? System.out.print("ready "); System.out.print(); System.out.println("set "); System.out.println(); System.out.println("go ");
ready set go
7. what is an escape sequence: give some examples?
sequence of characters that begins with \ implies follwing character should be treated in some special way. \n newline, \t tab, \" quotations as opposed to using it to terminate a string.
12. Your program needs a variable of type int to hold the number of feet in a mile (5,280), write a declaration statement for the variable?
since ft in miles will not change declare a constant final int FT_PER_MILE = 5280;
e. before the statement that creates the upper torso foreground color is set to cyan. set to black and white immediately after upper torso is created?
the upper torso "dissappears" since it merges with the background.
b what is the type of these declared variables?
they are all int
10. int count = 0, value, total; final int MAX_VALUE = 100; int myValue = 50;
use the preceding information to answer a through e
a. result = (5 + 2);
valid
b. TOP set to 25 instead of 50
value of TOP is added to all vertical components snowman shifts upwards a little bit.
30 If an integer variable weight currently holds the value 100, what is its value after the following statement is executed? explain? weight-=17;
weight holds the value 83, -= operator modifies the weight by subtracting 17 from (100) then storing result back into weight.
a. int to long
widening
c. byte to short
widening
d byte to char
widening
e short to double
widening
31 why are widening conversions safer than narrowing conversions?
widening goes from small data value to a large one in terms of space used to store it. Narrowing does the opposite. information is more likely to be lost in a narrowing conversion so they are less safe.