2.99 See Answer

Question: Modify the code you wrote in Question


Modify the code you wrote in Question 18 so it adds all of the numbers read from the file and displays their total.


> What is the purpose of the MIN_VALUE and MAX_VALUE variables that are members of the numeric wrapper classes?

> What wrapper class methods convert a number from decimal to another numbering system? What wrapper classes are these methods a member of?

> What is the output of the following statement? System.out.println(Character.toUpperCase(Character.toLowerCase('A')));

> Write a statement that converts the following integer to a string and stores it in the String object referenced by str: int i = 99;

> Write code that does the following: opens a file named NumberList.txt, uses a loop to write the numbers 1 through 100 to the file, and then closes the file.

> Look at the following string: "dog$cat@bird%squirrel" Write code using the String class’s split method that can be used to extract the following tokens from the string: dog, cat, bird, and squirrel.

> Look at the following string: "/home/rjones/mydata.txt" Write code using the String class’s split method that can be used to extract the following tokens from the string: home , rjones , mydata , and txt.

> Look at the following code: String str = "one two three four"; String[] tokens = str.split(" "); int x = tokens.length; String first = tokens[0]; What value will be stored in x? What value will the first variable reference?

> The following string contains three tokens. What are they? What character is the delimiter? "apples pears bananas"

> How does the StringBuilder class’s replace method differ from the String class’s replace method?

> You wish to change a specific character in a StringBuilder object. What method do you use?

> You wish to delete a specific character from the existing contents of a StringBuilder object. What method do you use?

> You wish to insert a string into the existing contents of a StringBuilder object. What method do you use?

> You wish to add a string to the end of the existing contents of a StringBuilder object. What method do you use?

> Write an if statement that displays the word “digit†if the char variable ch contains a numeric digit. Otherwise, it should display “Not a digit.â€.

> Complete the following program so it displays a random integer in the range of 1 through 10. // Write the necessary import statement(s) here. public class ReviewQuestion15 { public static void main(String[ ] args) { // Write the necessary code here. } }

> Look at the following statement: String city = "Asheville"; Rewrite this statement so that city references a StringBuilder object instead of a String object.

> In a program that makes lots of changes to strings, would it be more efficient to use String objects or StringBuilder objects? Why?

> The String class is immutable. What does this mean?

> Assume that a program has the following declarations: double number = 9.47; String str; Write a statement that assigns a string representation of the number variable to str.

> What will the following code display? String str1 = "William "; String str2 = " the "; String str3 = " Conqueror"; System.out.println(str1.trim() + str2.trim() + str3.trim());

> Look at the following code: String str1 = "To be, or not to be"; String str2 = str1.replace('o', 'u'); System.out.println(str1); System.out.println(str2); You hear a fellow student claim that the code will display the following Tu be ur nut tu be Tu be u

> What is the difference between the getChars and toCharArray methods?

> The + operator, when used with strings, performs the same operation as what String method?

> What is the difference between the getChars and substring methods?

> What is the difference between the indexOf and lastIndexOf methods?

> Write nested loops to draw this pattern:

> Write a statement that converts the contents of the char variable big to lowercase. The converted value should be assigned to the variable little.

> Assume that the following enumerated data type has been declared: enum Letters { Z, Y, X } What will the following code display? if (Letters.Z.compareTo(Letters.X) > 0) System.out.println("Z is greater than X."); else System.out.println("Z is not greater

> Assume that the following enumerated data type has been declared: enum Creatures{ HOBBIT, ELF, DRAGON } What will the following code display? System.out.println(Creatures.HOBBIT + " " + Creatures.ELF + " " + Creatures.DRAGON);

> Look at the following statement, which declares an enumerated data type: enum Flower { ROSE, DAISY, PETUNIA } 1. What is the name of the data type? 2. What is the ordinal value for the enum constant ROSE? For DAISY? For PETUNIA? 3. What is the fully qual

> Look at the following code. Stock stock1 = new Stock("XYZ", 9.65); Stock stock2 = new Stock("SUNW", 7.92); While the equals method is executing as a result of the following statement, what object does this reference? if (stock2.equals(stock1)) System.out

> Describe the limitation of static methods.

> What action is possible with a static method that isn’t possible with an instance method?

> What is the difference between an instance field and a static field?

> Look at the following statements: int[] numbers1 = { 1, 3, 6, 9 }; int[] numbers2 = { 2, 4, 6, 8 }; int result; Write a statement that multiplies element 0 of the numbers1 array by element 3 of the numbers2 array and assigns the result to the result vari

> Write a statement that creates and initializes a double array with the following ­values: 1.7, 6.4, 8.9, 3.1, and 9.2. How many elements are in the array?

> Write an input validation loop that asks the user to enter a number in the range of 1 through 4.

> Show how the double variables temp, weight, and age can be declared in one statement.

> What is the output of the following code? int[] values = new int[5]; for (int count = 0; count < 5; count++) values[count] = count + 1; for (int count = 0; count < 5; count++) System.out.println(values[count]);

> What happens in Java when a program tries to use a subscript that is out-of-bounds?

> What does it mean for a subscript to be out-of-bounds?

> What is the difference between an array’s size declarator and a subscript?

> What is the difference between an ArrayList object’s size and its capacity?

> How do you determine an ArrayList object’s size?

> What would the valid subscript values be in a four-element array of doubles?

> How do you insert an item at a specific location in an ArrayList object?

> How do you retrieve a specific item from an ArrayList object?

> How do you remove an item from an ArrayList object?

> How do you add items to an ArrayList object?

> Write a statement that creates an ArrayList object and assigns its address to a variable named lizards. The ArrayList should be able to store String objects only.

> Write a statement that creates an ArrayList object and assigns its address to a variable named frogs.

> What import statement must you include in your code in order to use the ArrayList class?

> If a sequential search is performed on an array, and it is known that some items are searched for more frequently than others, how can the contents of the array be reordered to improve the average performance of the search?

> On average, with an array of 20,000 elements, how many comparisons will the sequential search perform?

> Describe the difference between the sequential search and the binary search.

> What’s wrong with the following array declarations? int[ ] readings = new int[−1]; double[ ] measurements = new double[4.5];

> How many times will the selection sort swap the smallest value in an array with another value?

> What value in an array does the selection sort algorithm look for first? When the selection sort finds this value, what does it do with it?

> Write a while loop that lets the user enter a number. The number should be multiplied by 10, and the result stored in the variable product. The loop should iterate as long as product contains a value less than 100.

> A video rental store keeps videos on 50 racks with 10 shelves each. Each shelf holds 25 videos. Declare a three-dimensional array large enough to represent the store’s storage system.

> Recall that we discussed a Rectangle class in Chapter6 . Write code that declares a Rectangle array with five elements. Instantiate each element with a Rectangle object. Use the Rectangle constructor to initialize each object with values for the length a

> 1. Write a statement that declares a String array initialized with the following strings: “Mercuryâ€, “Venusâ€, “Earthâ€, and “Marsâ€. 2. Write a loop that displays the contents of each element in the array you declared in A. 3. Write a loop that

> Write a method named zero, which accepts an int array as an argument and stores the value 0 in each element.

> Look at the following method header: public static void myMethod(double[] array) Here is an array declaration: double[] numbers = new double[100]; Write a statement that passes the numbers array to the myMethod method.

> Look at the following statements: int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = new int[7]; Write code that copies the a array to the b array.

> A program has the following declaration: double[] values; Write code that asks the user for the size of the array and then creates an array of the specified size, referenced by the values variable.

> A program uses a variable named array that references an array of integers. You do not know the number of elements in the array. Write a for loop that stores −1 in each element of the array.

> Write statements that create the following arrays: 1. A 100-element int array referenced by the variable employeeNumbers. 2. A 25-element double array referenced by the variable payRates. 3. A 14-element float array referenced by the variable miles. 4. A

> In this chapter we used the metaphor of a kite attached to a spool of string to describe the relationship between an object and a reference variable. In this metaphor, does the kite represent an object, or a reference variable?

> Write an if-else statement that displays the String objects title1 and title2 in alphabetical order.

> You hear someone make the following comment: “A blueprint is a design for a house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint.†Think of this as a

> How is the relationship between an object and a reference variable similar to a kite and a spool of string?

> What values do reference variables hold?

> What does the new operator do?

> You have programs that create Scanner, Random, and PrintWriter objects. Where are the Scanner, Random, and PrintWriter classes?

> Will all of a class’s actions always be directly mentioned in the problem domain description?

> How is a class like a blueprint?

> What two questions should you ask to determine a class’s responsibilities?

> What are a class’s responsibilities?

> How do you identify the potential classes in a problem domain description?

> Write an if statement that prints the message “The number is valid†if the variable temperature is within the range −50 through 150.

> When designing an object-oriented application, who should write a description of the problem domain?

> What is a problem domain?

> How many default constructors may a class have?

> Look at the following class: public class CheckPoint { public void message(int x) { System.out.print("This is the first version "); System.out.println("of the method."); } public void message(String x) { System.out.print("This is the second version "); S

> What is a method’s signature?

> Is it required that overloaded methods have different return values, different parameter lists, or both?

> Assume that the following is a constructor, which appears in a class: ClassAct(int number) { item = number; } 1. What is the name of the class that this constructor appears in? 2. Write a statement that creates an object from the class and passes the val

> What are an object’s methods?

> What is a constructor’s return type?

> How is a constructor named?

> A program has a float variable named total and a double variable named number. Write a statement that assigns number to total without causing an error when compiled. Answer: total = (float)number;

> Assume that r1 and r2 are variables that reference Rectangle objects, and the following statements are executed: r1.setLength(5.0); r2.setLength(10.0); r1.setWidth(20.0); r2.setWidth(15.0); Fill in the boxes in Figure6-21 that represent each object&Atild

> What is a stale data item?

> What is an accessor? What is a mutator?

> What does the key word new do?

> Assume that limo is a variable that references an instance of the class shown in ­Figure6-17 . Write a statement that calls setMake and passes the argument "Cadillac".

> Look at the UML diagram in Figure6-17 and answer the following questions: 1. What is the name of the class? 2. What are the fields? 3. What are the methods? 4. What are the private members? 5. What are the public members?

> A string literal, such as "Joe", causes what type of object to be created?

2.99

See Answer