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 programming
Questions and Answers of
Java Programming
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
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
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
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
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
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
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
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
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
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
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,
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
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
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
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
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
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:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 ••
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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).
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
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
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
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
Showing 600 - 700
of 791
1
2
3
4
5
6
7
8