New Semester
Started
Get
50% OFF
Study Help!
--h --m --s
Claim Now
Question Answers
Textbooks
Find textbooks, questions and answers
Oops, something went wrong!
Change your search query and then try again
S
Books
FREE
Study Help
Expert Questions
Accounting
General Management
Mathematics
Finance
Organizational Behaviour
Law
Physics
Operating System
Management Leadership
Sociology
Programming
Marketing
Database
Computer Network
Economics
Textbooks Solutions
Accounting
Managerial Accounting
Management Leadership
Cost Accounting
Statistics
Business Law
Corporate Finance
Finance
Economics
Auditing
Tutors
Online Tutors
Find a Tutor
Hire a Tutor
Become a Tutor
AI Tutor
AI Study Planner
NEW
Sell Books
Search
Search
Sign In
Register
study help
computer science
introduction to programming with java a problem solving approach
Introduction To Programming With Java A Problem Solving Approach 3rd International Edition John Dean - Solutions
Assume an ordinary queue has been declared with the Queue interface and instantiated with the ArrayDeque class. Also, assume a priority queue has been declared with the Queue interface and instantiated with the PriorityQueue class. Figure 10.15 shows that the PriorityQueue class also implements the
A collection like ArrayList students could contain two or more references to the same object (same student enrolled in more than one course). Therefore, students.size() would not necessarily return the total number of distinct Student objects in a school’s student body. However, all Java
In the ListExecutionTimes program, the getIndices method fills an ArrayList with randomly ordered indices and then copies the ArrayList’s values to an array. Write code for an alternative getIndices method that returns a similar result. It calls the zero-parameter add method to populate the
Enhance the HungerGames program. Create a Tribute class with three instance variables, name (a String), district (an int), and gender (a char). Provide this new Tribute class with a constructor to initialize its instance variables and with get methods to access them. Modify the HungerGames class by
Modify the Community program by enabling the user to explore alternate community relationships. Specifically, modify the Community constructor so it asks the user to supply an integer seed number.
Fill in the blank: “A recursive method needs to have at least one parameter whose value ________ with successive methods calls.”
When a stopping condition is satisfied, a recursive method calls itself. (T / F)
Write a sufficient stopping condition for the recursive calculation of a factorial.
In Figure 11.2’s factorial calculation, during the calling sequence (before reaching the stopping condition), what is the value of nF when n equals 2?Figure 11.2 1234 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import java.util.Scanner; public class
Fill in the blank: “If the method’s body includes two recursive calls to itself and the method executes both calls, the method is said to exhibit________.”
If it’s just as easy to solve a problem with a loop as with recursion, which solution is preferable and why?
In the printReverseMessage method of Figure 11.7, what variable changes as the recursion progresses, and what is the stopping condition?Figure 11.7 private static void print Reverse Message(String msg) { int index; // position of last character in msg if (!msg.isEmpty()) ( index= msg.length() - 1;
Write the recurrence relation for a factorial using the functional notation, f(x).
What’s wrong with the stopping condition in this specification? f(x) [ f(x X-2 - 2) + X x > 0 0 X =
How many recursive method calls does it take to binary search an array with a length of 1,000?
Because a recursive binary search uses tail recursion, it’s easy to convert it to a loop solution. (T / F)
The recursive merge sort algorithm uses binary recursion. (T / F)
In a recursive merge sort, what is the recursive stopping condition?
In a merge sort, the next element to go into the merged array is the smaller of the smallest elements in the two subordinate arrays. What happens when one of the two subordinate arrays becomes empty?
What is the algorithm used to move a stack of disks whose bottom disk is an arbitrary disk n?
Describe the arguments used for the first recursive method call in the algorithm in question 15 in terms of the current method’s parameters.Question 15:What is the algorithm used to move a stack of disks whose bottom disk is an arbitrary disk n?
What is a fractal?
In our tree-drawing program, what does the model do?
In our tree-drawing program, what does the view do?
In our tree-drawing program, what does the controller do?
The text says: “The else body should contain one or more calls to the same method with argument values that are closer to the stopping condition’s value.” But in the previous section’s line-of-ships example, the flag signals are not likely to contain any values “that are closer to the
What is wrong with this recursive method? long sum(long n) { return n + sum(n+1);
Many times, you have used Scanner methods to read and parse keyboard input. You can also use those same Scanner methods to read and parse an ordinary string. The following program uses the Scanner class’s now-familiar next method, but this time it reads from a string instead of the keyboard. When
Modify the previous exercise’s Recurse program so that it uses tail recursion. Minimize code changes. You should be able to do this with exactly the same number and sequence of statements and with only internal alterations to one line in the main method and three lines in the recurse
Modify the previous exercise’s program so that it uses iteration instead of recursion. Do not use an array.Exercise 11.5:Modify the previous exercise’s Recurse program so that it uses tail recursion. Minimize code changes. You should be able to do this with exactly the same number and sequence
Change Figure 11.2’s recursive Factorial program so that it uses tail recursion.Figure 11.2 1234 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import java.util.Scanner; public class Factorial { public static void main(String[] args) ( Scanner stdIn = new
A palindrome is a string that reads the same in both directions. For example, “mom,” “kayak,” and “avid diva” are all palindromes. Write a recursive method named isPalindrome that determines whether a string is a palindrome. More specifically, your method should return true if the given
What’s wrong with this method? public double sum (double x) { if (x >= 0.5) { return x; } else { return sum(x)+ Math.random(); } } // end sum
Given this recurrence relation and stopping condition:By hand, evaluate y(3, 0.1), using the format presented. y(k, A) = (1 + A) - A * y(k-1, A) k > k
Provide a three-parameter tail-recursive static method named fibonacci that generates the numbers in a Fibonacci sequence. It should be an overloaded variation of the one-parameter fibonacci method shown below. import java.util.Scanner; public class Fibonacci public static void main(String[] args)
The following recurrence relation is called the Logistic Equation:y(k) = y(k-1) + gain * y(k-1) * (1.0 - y(k-1))This is the equation used to simulate growth in the optional problem-solving. This exercise asks you to use recursion to evaluate successive values generated by the Logistic equation. To
Specify all changes required to make this chapter’s binarySearch method search for a particular string in an array of String values.binarySearch Method:
Modify this chapter’s MergeSort class to make it sort the strings in an array of String values.MergeSort Class:
Modify the Towers of Hanoi program to help identify its algorithm efficiency. Declare a static variable named moveCount that keeps track of the total number of moves. Remove (or comment out) all the print statements that document a move, and replace them with statements that increment the moveCount
For each integer data type, how many bits of storage are used?
How would you write the decimal constant, 1.602 × 10−19, as a double?
What is the approximate precision (number of accurate decimal digits) of each of the floating-point types?
How many distinct characters are identified by the basic ASCII character set?
What number can you add to an uppercase-letter char variable to convert it to lowercase?
Assume the declaration:public final double C = 3.0E10; // speed of light in cm/sec Write a Java print statement that uses a cast operator to display the value of C in this format:30000000000.
Will this statement be OK, or will it generate a compile-time error? (OK / error)float price = 66;
Will this statement be OK, or will it generate a compile-time error? (OK / error)boolean done = (boolean) 0;
Will this statement be OK, or will it generate a compile-time error? (OK / error)float price = 98.1;
What is the value of z after these statements execute? int z, x = 3;z = --x;z += x--;
Write one Java statement that makes w, x, and y all equal to the current value of z.
Suppose x equals 0.43. Given the following switch construct heading, what does the controlling expression evaluate to?switch (x>0.67 ? 'H' : (x>0.33 ? 'M' : 'L')).
Assume this:int a = 2; int b = 6;float x = 8.0f;Evaluate each of the following expressions, using these guidelines:• Put each evaluation step on a separate line and use the ⇒ symbol between steps.• Evaluate each expression independently of the other expressions; in other words, use the above
Assume expr1 and expr2 are expressions that evaluate to boolean values. Assume that expr1 evaluates to true. When the computer evaluates each of the following expressions, will it evaluate expr2? If yes, just say “yes.” If no, explain why, and use the term “short-circuit evaluation” in your
Assume this:int a = 2;boolean flag = true;Evaluate the following expression: a < 3 || flag && !flag.
Assume that the following code fragment is inside of a program that compiles successfully. What does the code fragment print? This is a trick question. Study the code carefully. int x = 1; while (x < 4); { } System.out.println(x); x++;
Usually, you should avoid using break statements in loops because using break statements forces readers to look for termination conditions inside loop bodies. (T / F)
Assume that the following code fragment is inside of a program that compiles successfully. What does the code fragment print? for (int i=0, j=0; ; i++, j++) { } System.out.print(i + j + " ") ;
Declare a set of private constants called “Kingdom” that identify the five kingdoms of life: Monera, Protoctista, Fungi, Plantae, and Animalia.
What is the hexadecimal symbol for the decimal number 13?
The Unicode values for characters are the same as the ASCII values in the range 0x00 to 0xFF. (T / F)
If a double value overflows, what happens? What happens if a program divides by the result? Is an error produced, and if so what type of error?
The following program illustrates using BigInteger and BigDecimal. Replace the lines with two assignment statements. As indicated by the output, the first assignment statement divides the original number by 3.0 and uses 30 significant digits in displaying the result. The second assignment statement
Provide a robust method named getOddDigit which prompts the user to enter an odd digit and returns the odd digit as an int. Read the input as a string and verify that the input consists of a single odd digit. If the user enters invalid input, repeat the prompt and input.
Provide a program that reads a line of text, retrieves the first character from the input line, and displays the subsequent character in the ASCII table.
Assume c and d are boolean variables initialized as false. What are their values after this statement executes?c = !((c=!(7=6));
Assume this:What is the value of the result after this statement executes? int result = flag ? x : (int) (y - 0.7); boolean flag int x = -3; double y = 5.0; false;
Assume this: int a = 4;int b = 13;double x = -9.1;Evaluate each of the following expressions. Follow these guidelines:• Put each evaluation step on a separate line and use the ⇒ symbol between steps.• Evaluate each expression independently of the other expressions; in other words, use the
Assume this:Evaluate each of the following expressions. Follow these guidelines:Put each evaluation step on a separate line and• Use the ⇒ symbol between steps.• Evaluate each expression independently of the other expressions; in other words, use the above assumed values for each expression
Consider the following code fragment. Line numbers are at the left.Trace the code using this trace setup:Does short-circuit evaluation occur in line 4? Why or why not? 2 3 4 5 6 7 int x = 5; boolean y = true; boolean z; z = y && ++X == 6; y = x++ 3; y ! Z; = == System.out.println(x + 11 11 + y + "
Assume:Determine the output of this code fragment: boolean x; boolean y false; double z 9.5;
What happens if you try to compile the following code, and why does it happen? public static void main(String[] args) { } int number = 4; int factorial = } 1; for (int i=1; i
The Fibonacci sequence is a sequence of numbers such that the first two numbers are 0 and 1, and each successive number is the sum of the two previous numbers. Thus, here are the first nine numbers in the Fibonacci sequence:0 1 1 2 3 5 8 13 21 The third number is 1 because the previous two
The following program works, but it’s ugly. Modify it to perform the same function with a more conventional while loop. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdIn = new Scanner (System.in); String all Entries=""; for (String entry=""; ;) {
As you learned earlier, an enumerated type is a class (albeit a special kind of class). As such, you can add a main method to it and have that main method run the enumerated type as a program. Enhance this chapter’s City enumerated type by adding a main method that uses a for- each loop with a
Improve the CityTravel program by prompting the user to enter origin and destination cities and using the user-entered values in the getDistance method call. If the user does not spell one of the enumerated cities correctly, print an error message and loop until one of the enumerated cities is
Modify the StockAverage2 program in Figure 12.13 by inserting into the stream’s method-call chain a .peek method call that prints the previously entered values. Use a lambda expression for peek’s argument.Figure 12.13 /***** * StockAverage2.java *Dean & Dean * * This program stores user-entered
What is the Unicode hexadecimal value for Σ (uppercase Greek sigma)? Show or explain how you got your answer.
Run GridWorld’s BugRunner program. Perform the following operations and answer the questions:a) Move the rock to the location of the bug. Then move the rock to location (6, 0).What happens when you move a rock to the location of a bug?b) Insert a second rock at location (3, 9). Insert a red bug
In a UML diagram, what does an asterisk (*) indicate?UML diagram: Commission +COMMISSION_RATE: double = 0.10 +addSales(sales: double): void Commissioned -sales: double = 0.0 +Commissioned(name: String) +addSales(sales: double): void +getPay(): double Salaried And Commissioned -sales: double
In a UML diagram, what does a solid diamond indicate?UML diagram Commission +COMMISSION_RATE: double = 0.10 +addSales(sales: double): void Commissioned -sales: double = 0.0 +Commissioned(name: String) +addSales(sales: double): void +getPay(): double Salaried And Commissioned -sales: double
Explain how using an inheritance hierarchy can lead to code reusability.
What are two synonyms for a superclass?
What are two synonyms for a subclass?
How do you tell the compiler that a particular class is derived from another class?
Based on the UML diagram in Figure 13.9, an instance of the PartTime class includes the following instance variables: name and id. (T / F)Figure 13.9 Employee Person -name : String +Person() +Person(name: String) +getName(): String Full Time -id: int +Employee() +Employee(name: String, id: int)
In a subclass’s constructor, what do you have to do if you want to begin the constructor with a call to the superclass’s zero-parameter constructor?
If a superclass and a subclass define methods having the same name and the same sequence of parameter types, and an object of the subclass calls the method without specifying which version, Java generates a runtime error. (T / F).
If a subclass method overrides a method in the superclass, is it still possible to call the method in the superclass from the subclass?
If a superclass declares a variable to be private, can you access it directly from a subclass?
If you wish to call a superclass method, you must always prefix the method name with super. (T / F)
A final method is called “final” because it’s allowed to contain only named constants, not regular variables. (T / F)
Composition and inheritance are alternative programming techniques for representing what is essentially the same kind of real-world relationship. (T / F).
A Deck is a group of cards and a Deck has a group of cards. In our example, it’s better to choose the is-a relationship and implement inheritance. In this case, why is inheritance a better choice than composition?
It’s possible to support an association with references, variables, and methods in existing classes. What’s the advantage of using an association class instead?
This exercise should be used in combination with Exercises 2 and 3. Implement a Point class with the following features. Declare a position instance variable—an array of doubles that holds the point’s x, y, and z coordinate values. Provide a constructor that receives x, y, and z values and
This exercise should be used in conjunction with Exercises 1 and 3. Implement a Sphere class with the following features. Declare two instance variables—center (whose type is Point) and radius (whose type is double). Provide a constructor that receives center and radius values and uses them to
This exercise should be used in conjunction with Exercises 1 and 2. Implement a SphereDriver class with a main method that performs the following tasks. Instantiate a Point at x=10, y=15, and z=20. Instantiate a Sphere centered at that point and having a radius of 8. Call that sphere’s describe
Suppose that you have three classes—HomeInsurance (insurance for a home), InsurancePolicy (insurance for anything), and FloodDamageInsurance (insurance for flood damage to a home). Assume that the three classes form an inheritance hierarchy. Describe the inheritance relationships that exist
Suppose you want to design a program that handles various renewable energy sources. Implement your design as a UML diagram with 4 classes (RenewableEnergy, Hydro, Wind, and Solar) and 10 variables (initialCapitalInvestment, peakPowerOutput, averagePowerOutput, heightDifference, peakFlow,
CircleProperties program:Java’s API classes make extensive use of inheritance. For example, Oracle’s Java API documentation shows that the javafx.scene.shape package has a class named Circle that is a child of a class named Shape, which is itself a child of a class named Node. Circle inherits
Showing 200 - 300
of 717
1
2
3
4
5
6
7
8
Step by Step Answers