Chapter 3 Self Review Q's + A's:

Ace your homework & exams now with Quizwiz!

(Packages) SR 3.10 What is a Java package?

A Java package is a collection of related classes. The Java standard class library is a group of packages that support common programming tasks.

SR 3.37 What is a stage in the JavaFX theatre metaphor?

A JavaFX stage is a window in which a scene is displayed. The primary stage of a JavaFX application is automatically created & passed into the start method.

(The Random Class) SR 3.16 Given a Random object called rand, what does the call rand.nextInt ( ) return?

A call to the nextInt method of a Random object returns a random integer in the range of all possible int values, both positive and negative.

(The Math Class) SR 3.20 What is a class method (also called a static method)?

A class or static method can be invoked through the name of the class that contains it, such as Math.abs. If a method is not static, it can be executed only through an instance (an object) of the class.

SR 3.1 What is a null reference?

A null reference is a reference that does not refer to any object. The reserved word null can be used to check for null references before following them.

(Wrapper Classes) SR 3.32 How can we present a primitive value as an object?

A wrapper class is defined in the Java standard class library for each primitive type. In situations where objects are called for, an object created from a wrapper class may suffice.

(Representing Colors) SR 3.45 What is an RGB value?

An RGB value is a set of three integer values that represent a color by specifying the relative contributions of the colors red, green and blue.

SR 3.14 What does an import statement accomplish?

An import statement establishes the fact that a program uses a particular class, specifying what package that class is a part of. This allows the programmer to use the class name (such as Random) without having to fully qualify the reference (such as java.util.Random) every time.

SR 3.44 What is the advantage of grouping particular elements in a scene?

By grouping a set of nodes in a scene, you can apply transformations such as rotations and position shifts (translation) to the entire group at once.

SR 3.31 Why use an enumerated type such as CardSuit defined in the previous question? Why not just use String variables and assign them values such as "hearts"?

By using an enumerated type, you guarantee that variables of that type will only take on the enumerated values.

SR 3.11 What does the java.net package contain? The javafx.scene.shape package?

Each package contains a set of classes that support particular programming activities. The classes in the java.net package support network communication and the classes in the javafx.scene.shape package represent shapes such as circles and rectangles.

SR 3.24 Using the online JavaAPI documentation, list three methods of the Math class that are not included in Figure 3.5.

Examples of methods that are not listed in Figure 3.5 include: static int min (int a, int b) static float max (long a, long b) static long round (double a)

(Introduction to JavaFX) SR 3.36 In what situation might you omit the main method in a JavaFX application?

If the IDE you're using will automatically launch a JavaFX application, no main method is needed. Otherwise, a one-line main method calling the launch method is required.

SR 3.43 What is the effect of calling setFill on a Circle object, passing it a parameter of null?

If the value null is passed to the setFill method of a Circle object, its fill color will be fully transparent. Any shapes behind the circle will be visible through the circle.

SR 3.34 Suppose that an int variable named number has been declared and initialized and an Integer variable named holdNumber has been declared. Show two approaches in Java for having holdNumber represent the value stored in number.

One approach is to use the constructor of Integer, as follows: holdNumber = new Integer(number); Another approach is to take advantage of autoboxing, as follows: holdNumber = number;

SR 3.17 Given a Random object called rand, what does the call rand.nextInt (20) return?

Passing a positive integer parameter x to the nextInt method of a Random object returns a random number in the range of 0 to x-1. So a call to nextInt (20) will return a random number in the range of 0 to 19, inclusive.

(Basic Shapes) SR 3.40 How do you make one shape appear in front of another?

Shapes are drawn in the order they are added to a container such as a group or pane. So, to make one shape appear in front of another, it should be added after it.

SR (The String Class) 3.6 Assume s1, s2, and s3 are String variables initialized to "Amanda", "Bobby", and "Chris", respectively. Which String variable or variables are changed by each of the following statements? a. System.out.println(sl); b. sl = s3.toLowerCase( ); c. System.out.println (s2.replace ('B', 'M') ); d. s3 = s2.concat(s1);

Strings are immutable. The only way to change the value of a String variable is to reassign it a new object. Therefore, the variables changed by the statements are: a. none b. s1 c. none d. s3

SR 3.13 Using the online Java API documentation, describe the Point class.

The Point class, according to the online JavaAPI documentation, represents a location with coordinates (x, y) in two-dimensional space.

SR 3.12 What package contains the Scanner class? The String class? The Random class? The Math class?

The Scanner class and the Random class are part of the java.util package. The String and Math classes are part of the java.lang package.

SR 3.15 Why does the String class have to be specifically imported into our programs?

The String class is part of the java.lang package, which is automatically imported into any Java program. Therefore, no separate import declaration is needed.

SR 3.33 What wrapper classes correspond to each of the following primitive types: byte, int, double, char, and boolean?

The corresponding wrapper classes are Byte, Integer, Double, Character, and Boolean.

SR 3.28 Write code statements that prompt for and read in a double value from the user, and then print the result of taking the square root of the absolute value of the input value. Output the result to two decimal places.

The following code will prompt for and read in a double value from the user and then print the result of taking the square root of the absolute value of the input value to two decimal places: Scanner scan = new scanner (System.in); DecimalFormat fmt = new DecimalFormat ("0.00"); double value, result; System.out.print ("Enter a double value: "); value = scan.nextDouble ( ); result = Math.sqrt (Math.abs (value) ); System.out.println (fmt.format (result) );

SR 3.9 Write a declaration for a String variable called front, and initialize it to the first ten characters of another String object called description.

The following declaration creates a String object and sets it equal to the first 10 characters of the String description: String front = description.substring (0, 10);

SR 3.3 Write a declaration for a String variable called author, and initialize it to the string "Fred Brooks". Draw a graphic representation of the variable and its value.

The following declaration creates a String variable called author and initializes it: String author = new String ("Fred Brooks"); For strings, this declaration could have been abbreviated as follows: author---> "Fred Brooks"

SR 3.23 Write a declaration for a double variable called result and initialize it to 5 raised to the power 2.5.

The following declaration creates a double variable and initializes it to 5 raised to the power 2.5: double result = Math.pow (5, 2.5);

SR 3.41 Write a declaration for a Rectangle that is 100 pixels wide, 200 pixels high, with is upper-left corner positioned at point (30, 20).

The following declarations create a Rectangle that is 100 pixels wide, 200 pixels high, with its upper-left corner positioned at point (30, 20): Rectangle myRect = new Rectangle (30, 20, 100, 200);

(Enumerated Types) SR 3.29 Write the declaration of an enumerated type that represents movie ratings.

The following is a declaration of an enumerated type for movie ratings: enum Ratings {G, PG, PG13, R, NC17}

SR 3.46 Write a statement to create a Color object equivalent to Color.PINK using the rgb method.

The following statement creates a Color object equivalent to Color.PINK: Color myPink= Color.rgb(255, 165, 0);

SR 3.47 Write a statement to create a Color object equivalent to Color.YELLOW using the color method.

The following statement creates a Color object equivalent to Color.YELLOW, which is defined by a full contribution of red and green, and no contribution of blue: Color myYellow= Color.color(1.0, 1.0, 0.0);

SR 3.22 Write a statement that prints the sine of an angle measuring 1.23 radians.

The following statement prints the sine of an angle measuring 1.23 radians: System.out.println (Math.sin (1.23) );

SR 3.8 Write a statement that prints the value of a String object called title in all uppercase letters.

The following statement prints the value of a String object in all uppercase letters: System.out.println (title.toUppercase ( ) );

SR 3.35 Write a statement that prints out the largest possible int value.

The following statement uses the MAX_VALUE constant of the Integer class to print the largest possible int value: System.out.println(Integer.MAX_VALUE);

SR 3.42 Is the ellipse defined in the following statement wider than it is tall or taller than it is wide? Ellipse ellipse = new Ellipse (100, 150, 70, 90);

The last two parameters to the Ellipse constructor specify the horizontal radius and the vertical radius, respectively. So that ellipse is taller than it is wide.

SR 3.2 What does the new operator accomplish?

The new operator creates a new instance (an object)of the specified class. The constructor of the class is then invoked to help set up the newly created object.

SR 3.7 What output is produced by the following code fragment? String s1 = "Foundations"; String s2; System.out.println (s1.charAt (1) ); s2 = s1.substring (0, 5); System.out.println(s2); System.out.println(s1.length ( ) ); System.out.println(s2.length ( ) );

The output produced is: o Found 11 5

SR 3.39 Describe where the point (20, 50) is in the Java coordinate system.

The point (20, 50) is 20 pixels from the left edge of the scene and 50 pixels down from the top edge of the scene. All visible pixels in the Java coordinate system have positive coordinate values.

SR 3.18 Assuming that a Random object had been created called generator, what is the range of the result of each of the following expressions? a. generator.nextInt (50) b. generator.nextInt (5) + 10 c. generator.nextInt (10) + 5 d. generator.nextInt (50) - 25

The ranges of the expressions are: a. From 0 to 49 b. From 10 to 14 c. From 5 to 14 d. From -25 to 24

SR 3.38 What does the root node of a scene contain?

The root node of a scene contains all nodes displayed in the scene.

SR 3.21 What is the value of each of the following expressions? a. a. Math.abs (10) + Math.abs(-10) b. b. Math.pow (2, 4) c. c. Math.pow (4, 2) d. d. Math.pow (3, 5) e. e. Math.pow (5, 3) f. f. Math.sqrt (16)

The values of the expression are: a. 20 b. 16.0 c. 16.0 d. 243.0 e. 125.0 f. 4.0

(Formatting Output) SR 3.25 Describe how you request a NumberFormat object for use within a program.

To obtain a NumberFormat object for use within a program, you request an object using one of the static methods provided by the NumberFormat class. The method you invoke depends upon your intended use of the object. For example, if you intend to use it for formatting percentages, you might code: NumberFormat fmt = NumberFormat.getPercentInstance( );

SR 3.27 What are the steps to output a floating point value as a percentage using Java's formatting classes?

To output a floating point value as a percentage, you first obtain a NumberFormat object using a call to the static method getPercentageInstance of the NumberFormat class. Then you pass the value to be formatted to the format method of the formatter object, which returns a properly formatted string. For example: NumberFormat fmt = NumberFormat.getPercentageInstance ( ); System.out.println (fmt.format (value) );

SR 3.4 Write a code statement that sets the value of an integer variable called size to the length of a String object called name.

To set an integer variable size to the length of a String object called name, you code: size = name.length( );

SR 3.5 What is an alias? How does it relate to garbage collection?

Two references are aliases of each other if they refer to the same object. Changing the state of the object through one reference changes it for the other because there is actually only one object. An object is marked for garbage collection only when there are no valid references to it.

SR 3.30 Suppose that an enumerated type called CardSuit has been defined as follows: CardSuit card1, card2; card1 = CardSuit.clubs; card2 = CardSuit.hearts; System.out.println (card1); System.out.println (card2.name ( ) ); System.out.println (card1.ordinal ( ) ); System.out.println (card2.ordinal ( ) );

Under the listed assumptions, the output is: club hearts 0 2

SR 3.26 Suppose that in your program you have a double variable named cost. You want to output the value stored in cost formatted as the currency of the current locale. a. write a code statement that declares and requests a NumberFormat object named moneyFormat that can be used to represent currency in the format of the current locale. b. write a code statement that uses the moneyFormat object and prints the value of cost, formatted as the currency of the current locale. c. What would be the output from the statement you wrote in part (b) if the value in cost is 54.89 and your computer's locale is set to the United States? What if your computer's locale is set to the United Kingdom?

a. The statement is: NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( ); Do not forget, you also must import java.text.NumberFormat into your program. b. The statement is: System.out.println (moneyFormat.format (cost) ); c. If the locale is the United States, the output would be $54.89. If the locale is the United Kingdom, the output would be ₤54.89.

SR 3.19 Assuming that a Random object had been created called generator, write expressions that generate each of the following ranges of integers, including the endpoints. Use the version of the nextInt method that accepts a single integer parameter. a. 0 to 30 b. 10 to 19 c. -5 to 5

a. generator.nextInt (31); // range is 0 to 30 b. generator.nextInt (10) + 10; // range is 10 to 19 c. generator.nextInt (11) - 5; // range is -5 to 5


Related study sets

Chapter 13 - Biopsychology (Pinel-8), Biopsych chapter 11, Biopsychology chapter 16 (16.2-16.3, 16.5, 16.6, 16.7), Biopsychology Chapter 13, Chapter 11: Learning, Memory, and Amnesia

View Set

NCLEX Questions Test 4 MS, Parkinsons, Myasthenia Gravis, Osteoarthritis

View Set

Forensic Science Chapter 2- the crime scene

View Set