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
java programming
Big Java, Enhanced Early Objects 7th Edition Cay S Horstmann - Solutions
Give three examples of objects that belong to the String class. Give an example of an object that belongs to the PrintStream class. Name two methods that belong to the String class but not the PrintStream class. Name a method of the PrintStream class that does not belong to the String class.
Declare and initialize variables for holding the price and the description of an article that is available for sale.
Explain the difference between the = symbol in Java and in mathematics.
Give an example of a method that has an argument of type int. Give an example of a method that has a return value of type int. Repeat for the type String.
Write Java statements that initialize a string message with "Hello" and then change it to"HELLO". Use the toUpperCase method.
Write Java statements that initialize a string message with "Hello" and then change it to"hello". Use the replace method.
Write Java statements that initialize a string message with a message such as "Hello, World" and then remove punctuation characters from the message, using repeated calls to the replace method.
Explain the difference between an object and an object variable.
Give the Java code for constructing an object of class Rectangle, and for declaring an object variable of class Rectangle.
Give Java code for objects with the following descriptions:a. A rectangle with center (100, 100) and all side lengths equal to 50b. A string with the contents “Hello, Dave”Create objects, not object variables.
Repeat Exercise •• R2.16, but now declare object variables that are initialized with the required objects.Data from Exercise R2.16 Give Java code for objects with the following descriptions:a. A rectangle with center (100, 100) and all side lengths equal to 50b. A string with the
Write a Java statement to initialize a variable square with a rectangle object whose top-left corner is (10, 20) and whose sides all have length 40. Then write a statement that replaces square with a rectangle of the same size and top-left corner (20, 20).
Write Java statements that initialize two variables square1 and square2 to refer to the same square with center (20, 20) and side length 40.
Find the errors in the following statements:a. b. c. d. Rectangle r= (5, 10, 15, 20);
Name two accessor methods and two mutator methods of the Rectangle class.
Consult the API documentation to find methods for• Concatenating two strings, that is, making a string consisting of the first string, followed by the second string.• Removing leading and trailing white space of a string.• Converting a rectangle to a string.• Computing the smallest
Explain the difference between an object and an object reference.
What is the difference between a console application and a graphical application?
Why does the argument of the paintComponent method have type Graphics and not Graphics2D?
What is the purpose of a graphics context?
Write a program that initializes a string with "Mississippi". Then replace all "i" with"ii" and print the length of the resulting string. In that string, replace all "ss" with "s"and print the length of the resulting string.
Look into the API documentation of the String class and locate the trim method.Write a program demonstrating what it does. Then show how you can use the replace method to remove all spaces from a string.
Write an AreaTester program that constructs a Rectangle object and then computes and prints its area. Use the getWidth and getHeight methods. Also print the expected answer.
Write a PerimeterTester program that constructs a Rectangle object and then computes and prints its perimeter. Use the getWidth and getHeight methods. Also print the expected answer.
Write a program that constructs a rectangle with area 42 and a rectangle with perimeter 42. Print the widths and heights of both rectangles.
Look into the API documentation of the Rectangle class and locate the method void add (int newx, int newy) Read through the method documentation. Then determine the result of the following statements: Rectangle box = new Rectangle (5, 10, 20, 30); box.add(0, 0); Write a program Add Tester that
Write a program ReplaceTester that encodes a string by replacing all letters "i" with"!" and all letters "s" with "$". Use the replace method. Demonstrate that you can correctly encode the string "Mississippi". Print both the actual and expected result.
Write a program HollePrinter that switches the letters "e" and "o" in a string. Use the replace method repeatedly. Demonstrate that the string "Hello, World!" turns into"Holle, Werld!"
The StringBuilder class has a method for reversing a string.In a ReverseTester class, construct a StringBuilder from a given string (such as "desserts"), call the reverse method followed by the toString method, and print the result. Also print the expected value. BONAJUSMA
In the Java library, a color is specified by its red, green, and blue components between 0 and 255 (see Table 4 on page 57). Write a program BrighterDemo that constructs a Color object with red, green, and blue values of 50, 100, and 150. Then apply the brighter method of the Color class and print
Repeat Exercise •• E2.10, but place your code into the following class. Then the color will be displayed.Data from exercise E2.10In the Java library, a color is specified by its red, green, and blue components between 0 and 255 (see Table 4 on page 57). Write a program BrighterDemo that
Repeat Exercise •• E2.10 but apply the darker method of the Color class twice to the object Color.RED. Call your class DarkerDemo.Data from exercise E2.10In the Java library, a color is specified by its red, green, and blue components between 0 and 255 (see Table 4 on page 57). Write a program
The Random class implements a random number generator, which produces sequences of numbers that appear to be random. To generate random integers, you construct an object of the Random class, and then apply the nextInt method. For example, the call generator.nextInt(6) gives you a random number
Write a program RandomPrice that prints a random price between $10.00 and $19.95 every time the program is run.
Look at the API of the Point class and find out how to construct a Point object. In a PointTester program, construct two points with coordinates (3, 4) and (–3, –4). Find the distance between them, using the distance method. Print the distance, as well as the expected value. (Draw a sketch on
Using the Day class of Worked Example 2.1, write a Day Tester program that constructs a Day object representing today, adds ten days to it, and then computes the difference between that day and today. Print the difference and the expected value.Data from worked example 2.1. WORKED EXAMPLE 2.1 How
Using the Picture class of Worked Example 2.2, write a Half Size Picture program that loads a picture and shows it at half the original size, centered in the window.Data from worked example 2.2: Problem Statement Edit and display image files using the Picture class found in the worked_example_2
Using the Picture class of Worked Example 2.2, write a Double Size Picture program that loads a picture, doubles its size, and shows the center of the picture in the window.Data from worked example 2.2. WORKED EXAMPLE 2.2 Working with Pictures Problem Statement Edit and display image files using
Write a graphics program that draws two squares, both with the same center. Provide a class TwoSquareViewer and a class TwoSquareComponent.
Write a program that draws two solid squares: one in pink and one in purple. Use a standard color for one of them and a custom color for the other. Provide a class TwoSquareViewer and a class TwoSquareComponent.
Write a program called FourRectanglePrinter that constructs a Rectangle object, prints its location by calling System.out.println(box), and then translates and prints it three more times, so that, if the rectangles were drawn, they would form one large rectangle, as shown at right.Your program will
Write a GrowSquarePrinter program that constructs a Rectangle object square repre senting a square with top-left corner (100, 100)and side length 50, prints its location by calling System.out.println(square), applies the translate and grow methods, and calls System.out.println(square) again. The
Write a CenteredSquaresPrinter program that constructs a Rectangle object square representing a square with top-left corner (100, 100)and side length 200, prints its location by calling System.out.println(square), applies the grow and translate methods, and calls System.out.println(square) again.
The intersection method computes the intersection of two rectangles—that is, the rectangle that would be formed by two overlapping rectangles if they were drawn, as shown at right.You call this method as follows:Write a program IntersectionPrinter that constructs two rectangle objects, prints
In this exercise, you will explore a simple way of visualizing a Rectangle object. The setBounds method of the JFrame class moves a frame window to a given rectangle.Complete the following program to visually show the translate method of the Rectangle class: import java.awt. Rectangle; import
The BigInteger class represents integer numbers with an arbitrary number of digits.(As you will see in Chapter 4, the int type cannot express very large integers.) You can construct a BigInteger object by providing a string of its digits, such asWrite a program that prints the square, fourth power,
Write a program Lottery Printer that picks a combination in a lottery. In this lottery, players can choose 6 numbers (possibly repeated) between 1 and 49. Construct an object of the Random class (see Exercise •• E2.13) and invoke an appropriate method to generate each number.(In a real lot
Using the Day class of Worked Example 2.1, write a program that generates a Day object representing February 28 of this year, and three more such objects that represent February 28 of the next three years. Advance each object by one day and print each object. Also print the expected values:Data
The GregorianCalendar class describes a point in time, as measured by the Gregorian calendar, the standard calendar that is commonly used throughout the world today.You construct a GregorianCalendar object from a year, month, and day of the month, like this:Your task is to write a program that
The LocalDate class describes a calendar date that does not depend on a location or time zone. You construct a date like this:Your task is to write a program that prints • The weekday of “Pi day”, that is, March 14, of the current year.• The date and weekday of “Programmer’s day” in
Write a program LineDistanceTester that constructs a line joining the points (100, 100) and (200, 200), then constructs points (100, 200), (150, 150), and (250, 50). Print the distance from the line to each of the three points, using the ptSegDist method of the Line2D class. Also print the expected
Repeat Exercise ••• Testing P2.11, but now write a graphical application that shows the line and the points. Draw each point as a tiny circle. Use the drawString method to draw each distance next to the point, using calls:Data from testing P2.11Write a program LineDistanceTester that
Write a graphical program that draws a traffic light.
Run the following program:Modify the program as follows:• Double the frame size.• Change the greeting to “Hello, your name!”.• Change the background color to pale green (see Exercise •• Graphics E2.11).• For extra credit, add an image of yourself. (Construct an Image Icon.)Data from
It is possible to replace an old-fashioned thermostat with a device that can be controlled through a smart phone, without making any other changes to the heating system. Explain this by using the terms “interface”, “encapsulation”, and “implementation”.
Instance variables are a part of the hidden implementation of a class, but they aren’t actually hidden from programmers who have the source code of the class. Explain to what extent the private reserved word provides information hiding.
Consider a class Grade that represents a letter grade, such as A+ or B. Give two choices of instance variables that can be used for implementing the Grade class.
Consider a class Time that represents a point in time, such as 9 a.m. or 3:30 p.m. Give two different sets of instance variables that can be used for implementing the Time class.
Suppose the implementor of the Time class of Exercise •• R3.6 changes from one imple mentation strategy to another, keeping the public interface unchanged. What do the programmers who use the Time class need to do?Data from Exercise R3.6Consider a class Time that represents a point in time,
You can read the value instance variable of the Counter class with the getValue acces sor method. Should there be a setValue mutator method to change it? Explain why or why not.
Show that the BankAccount(double initialBalance) constructor is not strictly nec essary.That is, if we removed that constructor from the public interface, how could a programmer still obtain BankAccount objects with an arbitrary balance?Conversely, could we keep only the BankAccount(double
Why does the BankAccount class not have a reset method?
What happens in our implementation of the BankAccount class when more money is withdrawn from the account than the current balance?
Which of the methods in the Cash Register class of How To 3.1 are accessor methods? Which are mutator methods?
What does the following method do? Give an example of how you can call the method. public class BankAccount { public void mystery (BankAccount that, double amount) { this.balance this. balance amount; that. balance that. balance + amount;
Suppose you want to implement a class TimeDepositAccount. A time deposit account has a fixed interest rate that should be set in the constructor, together with the initial balance. Provide a method to get the current balance. Provide a method to add the earned interest to the account. This method
Consider the following implementation of a class Square: public class Square { private int sideLength; private int area; // Not a good idea public Square(int length) { side Length = length; } public int getArea() { } area side Length side Length; return area; } Why is it not a good idea to
Consider the following implementation of a class Square: public class Square { private int sideLength; private int area; public Square(int initialLength) { } side Length = initialLength; area sideLength * side Length; public int getArea() { return area; } public void grow() { side Length = 2 *
Provide a unit test class for the Counter class in Section 3.1.Data from section 3.1In Chapter 1, you learned how to use objects from existing classes. In this chapter, you will start implementing your own classes. We begin with a very simple example that shows you how objects store their data, and
Read Exercise •• E3.13, but do not implement the Car class yet. Write a tester class that tests a scenario in which gas is added to the car, the car is driven, more gas is added, and the car is driven again. Print the actual and expected amount of gas in the tank.Data from exercise
Design a modification of the BankAccount class in which the first five transactions per month are free and a $1 fee is charged for every additional transaction. Provide a method that deducts the fee at the end of a month. What additional instance variables do you need? Using the object tracing
Suppose you want to extend the car viewer program in Section 3.8 to show a suburban scene, with several cars and houses. Which classes do you need?
Explain why the calls to the getWidth and getHeight methods in the CarComponent class have no explicit parameter.
How would you modify the Car class in order to show cars of varying sizes?
Simulate a tally counter that can be used to admit a limited number of people. First, the limit is set with a call public void setLimit(int maximum) If the click button is clicked more often than the limit, it has no effect. ( The call Math.min(n, limit) returns n if n is less than limit, and limit
Write a class Range Input that allows users to enter a value within a range of values that is provided in the constructor. An example would be a temperature control switch in a car that allows inputs between 60- and 80-degrees Fahrenheit. The input control has “up” and “down” buttons.
Write a Circuit Tester class that tests all switch combinations in Exercise •• E3.4, printing out actual and expected states for the switches and lamps.Data from Exercise E3.4Simulate a circuit for controlling a hallway light that has switches at both ends of the hallway. Each switch can be up
Change the public interface of the circuit class of Exercise •• E3.4 so that it has the following methods:Provide an implementation using only language features that have been introduced.The challenge is to find a data representation from which to recover the switch states. public int
Write a Bank Account Tester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
Add a method:the balance in moms Savings is $1,100. Also supply a Bank Account Tester class that prints the actual and expected balance. public void add Interest (double rate) to the BankAccount class that adds interest at the given rate. For example, after the statements BankAccount momsSavings =
Write a class Savings Account that is similar to the Bank Account class, except that it has an added instance variable interest. Supply a constructor that sets both the initial balance and the interest rate. Supply a method add Interest (with no explicit parameter) that adds interest to the
Add a method printReceipt to the CashRegister class. The method should print the prices of all purchased items and the total amount due. You will need to form a string of all prices. Use the concat method of the String class to add additional items to that string. To turn a price into a string,
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 getSalesCount to get the total amount of all sales and the number of sales. Supply a method reset
Implement a class Employee. An employee has a name (a string) and a salary (a dou ble).Provide a constructor with two arguments: public Employee (String employeeName, double currentSalary) and methods public String getName() public double getSalary() public void raiseSalary(double byPercent) These
Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a method drive that
Implement a class Product. A product has a name and a price, for example new Product("Toaster", 29.95 ). Supply methods getName, getPrice, and reducePrice. Supply a program ProductPrinter that makes two products, prints each name and price, reduces their prices by $5.00, and then prints the prices
Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient:Construct an object of the Letter class and call addLine twice.(1) Use the concat method to form a longer string from two shorter strings.(2) The special string "" represents a new
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 move, its position changes by one unit in the current direction. Provide a constructor: public Bug
Implement a class Moth that models a moth flying along a straight line. The moth has a position, which is the distance from a fixed origin. When the moth moves toward a point of light, its new position is halfway between its old position and the position of the light source. Supply a constructor:
Write a program that fills the window with a large ellipse, with a black outline and filled with your favorite color. The ellipse should touch the window boundaries, even if the window is resized. Call the getWidth and getHeight methods of the JComponent class in the paintComponent method.
Draw a shooting target—a set of concentric rings in alternating black and white colors. Hint: Fill a black circle, then fill a smaller white circle on top, and so on. Your program should be composed of classes Target, TargetComponent, and TargetViewer.
Write a program that draws a picture of a house. It could be as simple as the accompanying figure, or if you like, more elaborate (3-D, skyscraper, marble col umns in the entryway, whatever). Implement a class House and supply a method draw(Graphics2D g2) that draws the house.
Extend Exercise •• Graphics E3.20 by supplying a House constructor for specifying the position and size. Then populate your screen with a few houses of different sizes.Data from Graphics E3.20Write a program that draws a picture of a house. It could be as simple as the accompanying
Change the car viewer program in Section 3.8 to make the cars appear in different colors. Each Car object should store its own color. Supply modified Car and Car -Component classes.
Change the Car class so that the size of a car can be specified in the constructor. Change the CarComponent class to make one of the cars appear twice the size of the original example.
Write a program that displays the Olympic rings. Color the rings in the Olympic colors. Provide classes OlympicRing, OlympicRingViewer, and OlympicRingComponent. 80
Make a bar chart to plot the following data set. Label each bar. Make the bars horizontal for easier labeling. Provide a class BarChartViewer and a class BarChartComponent. Bridge Name Golden Gate Brooklyn Delaware Memorial Mackinac Longest Span (ft) 4,200 1,595 2,150 3,800
Enhance the CashRegister class so that it counts the purchased items. Provide a get ItemCount method that returns the count.
Support computing sales tax in the CashRegister class. The tax rate should be supplied when constructing a CashRegister object. Add recordTaxablePurchase and getTotal- Tax methods. (Amounts added with recordPurchase are not taxable.) The giveChange method should correctly reflect the sales tax that
Implement a class Balloon. A balloon starts out with radius 0. Supply a method:that returns the current volume of the balloon. Use Math.PI for the value of π. To compute the cube of a value r, just use r * r * r. public void inflate (double amount) that increases the radius by the given amount.
Showing 600 - 700
of 791
1
2
3
4
5
6
7
8
Step by Step Answers