R8.5 Consider the data representation of a cash register that keeps track of sal
ID: 3871675 • Letter: R
Question
R8.5 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 change.
R8.6 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 to the CashRegister class to support this modification?
R8.7 What is an instance method, and how does it differ from a static method?
R8.8 What is a mutator method? What is an accessor method?
R8.9 What is an implicit parameter? How does it differ from an explicit parameter?
R8.10 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?
R8.11 What is a constructor?
R8.12 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?
R8.13 Using the object tracing technique described in Section 8.8, trace the program at the end of Section 8.7.
R8.14 Using the object tracing technique described in Section 8.8, trace the program in How To 8.1.
R8.15 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 deducts the fee at the end of a month. What additional instance variables do you need? Using the object tracing technique described in Section 8.8, trace a scenario that shows how the fees are computed over two months.
R8.16 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 hides the private implementation of a class.
R8.17 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.
R8.18 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 instance method?
R8.19 What is the this reference? Why would you use it?
R8.20 What is the difference between the number zero, the null reference, the value false, and the empty string?
R8.22 What is the default package? Have you used it before this chapter in your programming?
On pages 427-428 of Big Java: Late Objects, complete the practice exercises:
E8.1 We want to add a button to the tally counter in Section 8.2 that allows an operator to undo an accidental button click. Provide a method
public void undo()
that simulates such a button. As an added precaution, make sure that the operator cannot click the undo button more often than the count button.
E8.2 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 count button was clicked more often than the limit, simulate an alarm by printing out a message “Limit exceeded”.
E8.3 Simulate a circuit for controlling a hallway light that has switches at both ends of the hallway. Each switch can be up or down, and the light can be on or off. Toggling either switch turns the lamp on or off. Provide methods
public int getFirstSwitchState() // 0 for down, 1 for up
public int getSecondSwitchState()
public int getLampState() // 0 for off, 1 for on
public void toggleFirstSwitch()
public void toggleSecondSwitch()
E8.4 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 switchNum)
E8.5 Reimplement the Menu class of Worked Example 8.1 so that it stores all menu items in one long string. Hint: Keep a separate counter for the number of options. When a new option is added, append the option count, the option, and a newline character.
E8.6 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 without. Supply a print method that prints the address with the street on one line and the city, state, and zip code on the next line. Supply a method public boolean comesBefore(Address other) that tests whether this address comes before another when the addresses are compared by postal code.
E8.7 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(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took.
E8.8 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 that constructs a grade from a string, such as "B+". You will also need a method that translates grades into their numeric values (for example, "B+" becomes 3.3).
E8.9 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(int initialPosition)
and methods
public void turn()
public void move()
public int getPosition()
Sample usage:
Bug bugsy = new Bug(10);
bugsy.move(); // Now the position is 11
bugsy.turn();
bugsy.move(); // Now the position is 10
Your main method should construct a bug, make it move and turn a few times, and print the actual and expected positions.
E8.10 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 halfway between its old position and the position of the light source. Supply a constructor
public Moth(double initialPosition)
and methods
public void moveToLight(double lightPosition)
public void getPosition()
Your main method should construct a moth, move it toward a couple of light sources, and check that the moth’s position is as expected.
E8.11 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 cylinderSurface(double r, double h)
public static double coneVolume(double r, double h)
public static double coneSurface(double r, double h)
that compute the volume and surface area of a sphere with a radius r, a cylinder with a circular base with radius r and height h, and a cone with a circular base with radius r and height h. Place them into a class Geometry. Then write a program that prompts the user for the values of r and h, calls the six methods, and prints the results.
E8.12 Solve Exercise E8.11 by implementing classes Sphere, Cylinder, and Cone. Which approach is more object-oriented?
E8.13 Implement a class LoginForm that simulates a login form that you find on many web pages. Supply methods
public void input(String text)
public void click(String button)
public boolean loggedIn()
The first input is the user name, the second input is the password. The click method can be called with arguments "Submit" and "Reset". Once a user has been successfully logged in, by supplying the user name, password, and clicking on the submit button, the loggedIn method returns true and further input has no effect. When a user tries to log in with an invalid user name and password, the form is reset.
Supply a constructor with the expected user name and password.
E8.14 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 methods
public void turnLeft()
public void turnRight()
public void move()
public Point getLocation()
public String getDirection()
The turnLeft and turnRight methods change the direction but not the location. The move method moves the robot by one unit in the direction it is facing. The getDirection method returns a string "N", "E", "S", or "W".
E8.15 Implement a CashRegister class and place the class into a package called money. Keep the CashRegisterTester class in the default package.
E8.16 Place a BankAccount class in a package whose name is derived from your e-mail address, as described in Section 8.12.3. Keep the BankAccountTester class in the default package.
On page 484 of Big Java: Late Objects, complete the review exercises:
R9.1 Identify the parent class and child class in each of the following pairs of classes.
a. Employee, Manager
b. GraduateStudent, Student
c. Person, Student
d. Employee, Professor
e. BankAccount, CheckingAccount
f. Vehicle, Car
g. Vehicle, Minivan
h. Car, Minivan
i. Truck, Vehicle
R9.2 Consider a program for managing inventory in a small appliance store. Why isn’t it useful to have a parent class SmallAppliance and child classes Toaster, CarVacuum, TravelIron, and so on?
R9.3 Which methods does the ChoiceQuestion class inherit from its parent class? Which methods does it override? Which methods does it add?
R9.4 Which methods does the SavingsAccount class in How To 9.1 inherit from its parent class? Which methods does it override? Which methods does it add?
R9.5 List the instance variables of a CheckingAccount object from How To 9.1.
R9.6 Suppose the class Sub extends the class Sandwich. Which of the following assignments are legal?
Sandwich x = new Sandwich();
Sub y = new Sub();
a. x = y;
b. y = x;
c. y = new Sandwich();
d. x = new Sub();
R9.7 Draw an inheritance diagram that shows the inheritance relationships between these classes.
Person
Employee
Student
Instructor
Classroom
Object
R9.8 In an object-oriented traffic simulation system, we have the classes listed below. Draw an inheritance diagram that shows the relationships between these classes.
Vehicle
Car
Truck
Sedan
Coupe
PickupTruck
SportUtilityVehicle
Minivan
Bicycle
Motorcycle
R9.9 What inheritance relationships would you establish among the following classes?
Student
Professor
TeachingAssistant
Employee
Secretary
DepartmentChair
Janitor
SeminarSpeaker
Person
Course
Seminar
Lecture
ComputerLab
R9.10 How does a cast such as (BankAccount)x differ from a cast of number values such as (int)x?
R9.11 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 PrintStream
b. System.out instanceof OutputStream
c. System.out instanceof LogStream
d. System.out instanceof Object
e. System.out instanceof Closeable
f. System.out instanceof Writer
Explanation / Answer
R8.5 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 change.
=> Not sure of the design, as you have not provided what is in section 8.8
R8.6 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 to the CashRegister class to support this modification?
=> We need to insert a stack in which we will store the last operation performed.. If cashier wants to undo the changes, we will pop the stack and provide the operation.. a successful operation from the cashier should be pushed to the stack.
R8.7 What is an instance method, and how does it differ from a static method?
An instance method is created only along with an object instance.. while for calling the static method, there is no need to have an object instance.
R8.8 What is a mutator method? What is an accessor method?
=> An mutator method is used to set the value of the variable through a defined procedure.. It may performs error checking also. The accessor method is used to provide the value of the variable to the calling methods.
R8.9 What is an implicit parameter? How does it differ from an explicit parameter?
Explicit parameter is passed by specifying the parameter in the parenthesis of a method call.
Implicit parameter is passed by specifying an object variable (object reference) before the name of a method.
R8.10 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?
there can be just one implicit parameter an instance method can have, While static method will have 0 implicit parameters. There is no limit on number of explicit parameters.
R8.11 What is a constructor?
An constructor i a method which doesn't return anything but is used to instantiate an object new instance. It can set default values and perform error checking.
R8.12 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?
A class can have any number of constructors but with different signatures. A class can be defined without providing any explicit constructor, in taht case complier defines one default for you. If a class has more than one constructor, the exact parameter matching constructor will be called.
R8.13 Using the object tracing technique described in Section 8.8, trace the program at the end of Section 8.7.
No Section 8.8 provided.
R8.14 Using the object tracing technique described in Section 8.8, trace the program in How To 8.1.
No Section 8.8 provided.
R8.15 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 deducts the fee at the end of a month. What additional instance variables do you need? Using the object tracing technique described in Section 8.8, trace a scenario that shows how the fees are computed over two months.
No Section 8.8 provided.
============================
Answering your first few questions, as it is not possible to answer all the questions in the given time limit.. Please post other questions in a group of 4 questions in a thread.. That way it will help us in explaining the answers clearly.
Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.