All Matches
Solution Library
Expert Answer
Textbooks
Search Textbook questions, tutors and Books
Oops, something went wrong!
Change your search query and then try again
Toggle navigation
FREE Trial
S
Books
FREE
Tutors
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
Hire a Tutor
AI Study Help
New
Search
Search
Sign In
Register
study help
computer science
java concepts late objects
Questions and Answers of
Java Concepts Late Objects
Write a recursive method for computing a string with the binary digits of a number. If n is even, then the last digit is 0. If n is odd, then the last digit is 1. Recursively obtain the remaining
Write a recursive method String reverse(String text) that reverses a string. For example, reverse("Hello!") returns the string "!olleH". Implement a recursive solution by removing the first
The following class generates all permutations of the numbers 0, 1, 2, . . ., n – 1, without using recursion.public class NumberPermutationIterator{private int[] a;public
Write a recursive definition of xn, where n ≥ 0, similar to the recursive definition of the Fibonacci numbers. Hint: How do you compute xn from xn – 1? How does the recursion terminate?
Find out how often the recursive version of fib calls itself. Keep a static variable fibCount and increment it once in every call to fib. What is the relationship between fib(n) and fibCount?
Find out how often the recursive version of fib calls itself. Keep a static variable fibCount and increment it once in every call to fib. What is the relationship between fib(n) and fibCount?
Write a graphics program that draws 12 strings, one each for the 12 standard colors, besides Color.WHITE, each in its own color. Provide a class ColorNameViewer and a class ColorNameComponent.
Given a class Rectangle with instance variables width and height, provide a recursive getArea method. Construct a rectangle whose width is one less than the original and call its getArea method.
Phone numbers and PIN codes can be easier to remember when you find words that spell out the number on a standard phone pad. For example, instead of remembering the combination 5282, you can just
Outline, but do not implement, a recursive solution for finding the smallest value in an array.
Given a class Square with an instance variable width, provide a recursive getArea method. Construct a square whose width is one less than the original and call its getArea method.
Continue Exercise P13.1, checking the words against the /usr/share/dict/words file on your computer, or the words.txt file in the companion code for this book. For a given number, return only actual
Outline, but do not implement, a recursive solution for finding the kth smallest element in an array. Look at the elements that are less than the initial element. Suppose there are m of them. How
Write a recursive method for factoring an integer n. First, find a factor f, then recursively factor n / f.
With a longer number, you may need more than one word to remember it on a phone pad. For example, 263-346-5282 is CODE IN JAVA. Using your work from Exercise P13.2, write a program that, given any
Who calls the paintComponent method of a component? When does the call to the paintComponent method occur?
Write a program that draws a clock face with a time that the user enters in two text fields (one for the hours, one for the minutes). You need to determine the angles of the hour hand and the minute
Define the termsa. Recursionb. Iterationc. Infinite recursiond. Recursive helper method
Redo Exercise E13.5 with a recursive helper method that reverses a substring of the message text.Data from Exercise E13.5Write a recursive method String reverse(String text) that reverses a string.
Improve upon Exercise R13.6 by computing xn as (xn/2)2 if n is even. Why is this approach significantly faster? Hint: Compute x1023 and x1024 both ways.Data from Exercise R13.6Write a recursive
Implement the reverse method of Exercise E13.5 as an iteration.Data from Exercise E13.5Write a recursive method String reverse(String text) that reverses a string. For example, reverse("Hello!")
Escaping a Maze. You are currently located inside a maze. The walls of the maze are indicated by asterisks (*). Use the following recursive approach to check whether you can escape from the maze: If
Write a recursive definition of n! = 1 × 2 × . . . × n, similar to the recursive definition of the Fibonacci numbers.
Use recursion to implement a method public static boolean find(String text, String str)that tests whether a given text contains a string. For example, find("Mississippi", "sip") returns
Using the PartialSolution interface and solve method from Exercise E13.22, provide a class MazePartialSolution for solving the maze escape problem of Exercise P13.7.Data from Exercise E13.22,The
Repeat Exercise E9.13, but in the BetterRectangle constructor, invoke the superclass constructor.Data from Exercise E9.13,The Rectangle class of the standard Java library does not supply a method to
Suppose the class Sandwich implements the Edible interface, and you are given the variable declarationsSandwich sub = new Sandwich();Rectangle cerealBox = new Rectangle(5, 10, 20, 30);Edible e =
Write a class PrimeSequence that implements the Sequence interface of Worked Example 9.2, and produces the sequence of prime numbers.Data from Example 9.2,n this Worked Example, we investigate
The Rectangle class of the standard Java library does not supply a method to compute the area or the perimeter of a rectangle. Provide a subclass BetterRectangle of the Rectangle class that has
Suppose C is a class that implements the interfaces I and J, and i is declared as I i = new C();Which of the following statements will throw an exception?a. C c = (C) i;b. J j = (J) i;c. i = (I) null;
Modify the display method of the LastDigitDistribution class of Worked Example 9.2 so that it produces a histogram, like this:0: *************1: ******************2: *************Scale the bars so
Make a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance variable, named department, of type String. Supply a method toString that prints the
Suppose C is a class that implements the interfaces I and J. Which of the following assignments require a cast?C c = . . .;I i = . . .;J j = . . .;a. c = i;b. j = c;c. i = j;
Resonant circuits are used to select a signal (e.g., a radio station or TV channel) from among other competing signals. Resonant circuits are characterized by the frequency response shown in the
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary.
Which of these conditions returns true? Check the Java documentation for the inheritance patterns. Recall that System.out is an object of the PrintStream class.a. System.out instanceof PrintStreamb.
Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “see the dentist”) and a date. Write a method occursOn(int year, int
Consider a program for managing inventory in a small appliance store. Why isn’t it useful to have a superclass SmallAppliance and subclasses Toaster, CarVacuum, TravelIron, and so on?
Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new
Identify the superclass and subclass in each of the following pairs of classes.a. Employee, Managerb. Graduate Student, Studentc. Person, Studentd. Employee, Professore. Bank Account, Checking
The figure below shows a frequently used electric circuit called a “voltage divider”. The input to the circuit is the voltage vi. The output is the voltage vo. The output of a voltage divider is
What is the default package? Have you used it before this chapter in your programming?
In the Resistor class from Exercise P8.20, supply a method that returns a description of the “color bands” for the resistance and tolerance. A resistor has four color bands: • The first
For some values (for example, 20), the intName method returns a string with a leading space (" twenty"). Repair that blemish and ensure that spaces are inserted only when necessary. There are two
Implement the following modification of the quicksort algorithm, due to Bentley and McIlroy. Instead of using the first element as the pivot, use an approximation of the median.If n ≤ 7, use the
Trace the expression evaluator program from Section 13.5 with inputs 3 – 4 + 5, 3 – (4 + 5), (3 – 4) * 5, and 3 * 4 + 5 * 6.
Write a program that reads an expression involving integers and the variable x into an Expression object, and then computes the derivative. Add a method Expression derivative() to the Expression
Using recursion, compute the area of a polygon. Cut off a triangle and use the fact that a triangle with corners (x1, y1),(x2, y2), (x3, y3) has area 2+x23+x,-バューソュョーツ3 2
Exercise P13.5 shows an iterative way of generating all permutations of the sequence (0, 1, . . . , n – 1). Explain why the algorithm produces the correct result.Data from Exercise P13.5The
Add a toString method to the Expression class (as described in Exercise P13.9 and Exercise P13.10) that returns a string representation of the expression. It is ok to use more parentheses than
Using recursion, compute the sum of all values in an array.
Outline, but do not implement, a recursive solution for generating all subsets of the set {1, 2, . . . , n}.
Refine the expression evaluator of Exercise P13.9 so that expressions can contain the variable x. For example, 3*x*x+4*x+5 is a valid expression. Change the Expression interface so that its value
Using recursion, find the largest element in an array. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last
Let moves(n) be the number of moves required to solve the Towers of Hanoi problem. Find a formula that expresses moves(n) in terms of moves(n – 1). Then show that moves(n) = 2n – 1.
The expression evaluator in Section 13.5 returns the value of an expression. Modify the evaluator so that it returns an instance of the Expression interface with five implementing classes, Constant,
Use recursion to implement a methodpublic static int indexOf(String text, String str)that returns the starting position of the first substring of the text that matches str. Return –1 if str is not
The colored bands on the top-most resistor shown in the photo below indicate a resistance of 6.2 kΩ ±5 percent. The resistor tolerance of ±5 percent indicates the acceptable variation in the
What is the difference between the number zero, the null reference, the value false, and the empty string?
Design a class Cannonball to model a cannonball that is fired into the air. A ball has• An x- and a y-position.• An x- and a y-velocity.Supply the following methods:• A constructor with an
What is the this reference? Why would you use it?
The Downtown Marketing Association wants to promote downtown shopping with a loyalty program similar to the one in Exercise P8.17. Shops are identified by a number between 1 and 20. Add a new
In a static method, it is easy to differentiate between calls to instance methods and calls to static methods. How do you tell them apart? Why is it not as easy for methods that are called from an
Design a Customer class to handle a customer loyalty marketing campaign. After accumulating $100 in purchases, the customer receives a $10 discount on the next purchase. Provide methods• void
You can read the itemCount instance variable of the CashRegister class with the getCount accessor method. Should there be a setCount mutator method to change it? Explain why or why not.
Design a class Mailbox that stores e-mail messages, using the Message class of Exercise P8.15. Implement the following methods:• public void addMessage(Message m)• public Message getMessage(int
Instance variables are “hidden” by declaring them as private, but they aren’t hidden very well at all. Anyone can read the class declaration. Explain to what extent the private reserved word
Design a class Message that models an e-mail message. A message has a recipient, a sender, and a message text. Support the following methods:• A constructor that takes the sender and recipient• A
Implement a CashRegister class and place the class into a package called money. Keep the CashRegisterTester class in the default package.
Design a modification of the BankAccount class in How To 8.1 in which the first five transactions per month are free and a $1 fee is charged for every additional transaction. Provide a method that
Design and implement a class Country that stores the name of the country, its population, and its area. Then write a program that reads in a set of countries and prints• The country with the
Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west.Supply methodspublic void
Using the object tracing technique described in Section 8.8, trace the program at the end of Section 8.7.
Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount that was developed in How To 8.1 (ch08/how_to_1/BankAccount. java in the companion code for this
Implement a class LoginForm that simulates a login form that you find on many web pages. Supply methodspublic void input(String text)public void click(String button)public boolean loggedIn()The first
After closing time, the store manager would like to know how much business was transacted during the day. Modify the CashRegister class to enable this functionality. Supply methods getSalesTotal and
Solve Exercise E8.11 by implementing classes Sphere, Cylinder, and Cone. Which approach is more object-oriented?Data from Exercise E8.11Write static methods• public static double
How many constructors can a class have? Can you have a class with no constructors? If a class has more than one constructor, which of them gets called?
Reimplement the CashRegister class so that it keeps track of the total price as an integer:The total cents of the price. For example, instead of storing 17.29, store the integer 1729. Such an
Write static methods• public static double sphereVolume(double r)• public static double sphereSurface(double r)• public static double cylinderVolume(double r, double h)• public static double
Reimplement the CashRegister class so that it keeps track of the price of each added item in an ArrayList. Remove the itemCount and totalPrice instance variables. Reimplement the clear, addItem,
Implement a class Moth that models a moth flying in a straight line. The moth has a position, the distance from a fixed origin. When the moth moves toward a point of light, its new position is
How many implicit parameters can an instance method have? How many implicit parameters can a static method have? How many explicit parameters can an instance method have?
Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each
What is an implicit parameter? How does it differ from an explicit parameter?
Implement a VotingMachine class that can be used for a simple election. Have methods to clear the machine state, to vote for a Democrat, to vote for a Republican, and to get the tallies for both
Modify the Student class of Exercise E8.7 to compute grade point averages. Methods are needed to add a grade and get the current GPA. Specify grades as elements of a class Grade. Supply a constructor
What is a mutator method? What is an accessor method?
Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods getName(), addQuiz(int score), getTotalScore(),
What is an instance method, and how does it differ from a static method?
Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the
Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code. Supply two constructors: one with an apartment number and one
Suppose the CashRegister needs to support a method void undo() that undoes the addition of the preceding item. This enables a cashier to quickly undo a mistake. What instance variables should you add
Implement a class SodaCan with methods getSurfaceArea() and getVolume(). In the constructor, supply the height and radius of the can.
Reimplement the Menu class of Worked Example 8.1 so that it stores all menu items in one long string. Keep a separate counter for the number of options. When a new option is added, append the option
Consider the data representation of a cash register that keeps track of sales tax in Section 8.8. Instead of tracking the taxable total, track the total sales tax. Redo the walkthrough with this
Write a class Battery that models a rechargeable battery. A battery has a constructorpublic Battery(double capacity)where capacity is a value measured in milliampere hours. A typical AA battery has a
Change the public interface of the circuit class of Exercise E8.3 so that it has the following methods:public int getSwitchState(int switchNum)public int getLampState()public void toggleSwitch(int
What is the public interface of a class? How does it differ from the implementation of a class?
Add a method public int getFriendCount()to the Person class of Exercise P8.2.Data from Exercise P8.2.A Person has a name (just a first name for simplicity) and friends. Store the names of the friends
Showing 1 - 100
of 835
1
2
3
4
5
6
7
8
9