Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 5, Problem 9PP
Program Plan Intro
Modified “Dog” class
Program Plan:
Modified “Dog.java”:
- • Define “Dog” class.
- ○ Declare instance variables for “Dog” class.
- ○ Create accessor and mutator methods to set dog’s name, breed and age.
- ○ Define “equals” method.
- ■ This method returns “true”, if name, breed and age of one dog is equal to other dog. Otherwise returns “false”.
- ○ Define “writeOutput” method for displays all details of dog.
- ○ Define “getAgeInHumanYears” method as “Listing 5.1” of chapter 5.
“Main.java”:
- • Define “Main” class.
- ○ Define main function.
- ■ Create objects “dog1”, “dog2”, “dog3” and “dog4” from “Dog” class.
- ■ Set name for each object by calling the method “setDogName()”.
- ■ Set breed for each object by calling the method “setDogBreed()”.
- ■ Set age for each object by calling the method “setDogAge()”.
- ■ Call "writeOutput" method for object “dog1” and “dog2”.
- ■ Check the object of “dog1” and “dog2” are equal or not by calling method “equals”.
- ■ Check the object of “dog3” and “dog1” are equal or not by calling method “equals”.
- ■ Check the object of “dog4” and “dog1” are equal or not by calling method “equals”.
- ○ Define main function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
THE BELOW IS IMPORTANT
Rewrite the above code so that
It has all of the correct access modifiers for good encapsulation.
Your code obeys the principle of encapsulation that obliges you to put each method in the proper class.
Your code uses NO getters and NO additional classes or methods. You are welcome to modify any existing methods (including signatures and bodies) and/or move them between the existing classes.
Your code DOES NOT modify any of the constructors.
Important notes:
You DO NOT have to write a main method.
You DO NOT have to write tests.
You DO NOT have to write Javadocs.
You DO NOT have to write interfaces to encapsulate data structures.
There should be no incorrect syntax or other compile-time errors.
The owner of a venue for events needs software to keep track of reservations. The venue can handle
two events in a day, one scheduled for the afternoon and another scheduled for the evening. Each
event consists of the type (wedding, bar mitzvah, etc.), the date of the event, and whether or not the
event will be held in the evening. A programmer writes the code given on the handout.
import java.util.LinkedList;
class Event {
// wedding, etc.
String type;
Date date;
boolean isEvening;
// true if the event is to be held in the evening
public Event(string type, Date date, boolean isEvening) {
this.type - type;
this.date = date;
this.isEvening = isEvening;
}
class Date {
string month;
int day;
int year;
public Date(string month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
class Reservations {
LinkedList events;
public Reservations() {
this.events = new LinkedList();
// adds the given event to the events list, as long as there is no
// date/time conflict with…
Challenge exercise The following object creation will result in the constructor of the Date class being called. Can you write the constructor’s header? new Date("March", 23, 1861) Try to give meaningful names to the parameters
Chapter 5 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 5.1 - Consider the program in Listing 5.4 . Suppose you...Ch. 5.1 - Prob. 2STQCh. 5.1 - Prob. 3STQCh. 5.1 - Suppose every species in the world has a peculiar...Ch. 5.1 - Revise the definition of the method writeOutput in...Ch. 5.1 - Revise the definition of the method readInput in...Ch. 5.1 - Revise the definition of the method...Ch. 5.1 - What is the meaning of (int) that appears in the...Ch. 5.1 - Prob. 9STQCh. 5.1 - Define a method called getDensity that could be...
Ch. 5.1 - Define a method called changePopulation that could...Ch. 5.1 - Define a method called changePopulation that could...Ch. 5.2 - In Listing 5.12, we set the data for the object...Ch. 5.2 - Give preconditions and postconditions for the...Ch. 5.2 - What is an accessor method? What is a mutator...Ch. 5.2 - Give the complete definition of a class called...Ch. 5.2 - Prob. 17STQCh. 5.2 - In the definition of the method in Listing 5.15,...Ch. 5.2 - What is a well-encapsulated class definition?Ch. 5.2 - When should an instance variable in a class...Ch. 5.2 - Prob. 21STQCh. 5.2 - In a class definition, is anything private ever...Ch. 5.2 - In a class definition, is the body of any method...Ch. 5.3 - What is a reference type? Are class types...Ch. 5.3 - When comparing two quantities of a class type to...Ch. 5.3 - Prob. 26STQCh. 5.3 - Write a method definition for a method called...Ch. 5.3 - Given the class Species as defined in Listing...Ch. 5.3 - After correcting the program in the previous...Ch. 5.3 - What is the biggest difference between a parameter...Ch. 5.3 - Prob. 31STQCh. 5.3 - Write an equals method for the class Person...Ch. 5.4 - Rewrite the method drawFaceSansMouth in Listing...Ch. 5 - Design a class to represent a credit card. Think...Ch. 5 - Repeat Exercise 1 for a credit card account...Ch. 5 - Repeat Exercise 1 for a coin instead of a credit...Ch. 5 - Repeat Exercise 1 for a collection of coins...Ch. 5 - Consider a Java class that you could use to get an...Ch. 5 - Consider a class that keeps track of the sales of...Ch. 5 - Consider a class MotorBoat that represents...Ch. 5 - Prob. 8ECh. 5 - Prob. 9ECh. 5 - Prob. 10ECh. 5 - Write a program to answer questions like the...Ch. 5 - Define a class called Counter. An object of this...Ch. 5 - Prob. 3PCh. 5 - Define a Trivia class that contains information...Ch. 5 - Define a Beer class that contains the following...Ch. 5 - Write a grading program for an instructor whose...Ch. 5 - Add methods to the Person class from Self-Test...Ch. 5 - Create a class that represents a grade...Ch. 5 - Write a program that uses the Purchase class in...Ch. 5 - Write a program to answer questions like the...Ch. 5 - Consider a class that could be used to play a game...Ch. 5 - Consider a class BasketballGame that represents...Ch. 5 - Consider a class ConcertPromoter that records the...Ch. 5 - Prob. 9PPCh. 5 - Consider a class Movie that contains information...Ch. 5 - Repeat Programming Project 18 from Chapter 4, but...Ch. 5 - Prob. 12PP
Knowledge Booster
Similar questions
- B elow for each class you find a UML and description of the public interface. Implementing the public interface as described is madatory. There's freedom on how to implement these classes.The private properties and private methods are under your control.. There are multiple ways of implementing all these classes. Feel free to add private properties and methods. For each object, it's mandatory to create a header file (.h), implementation file (.cpp) and a driver. Blank files are included. The header should have the class definition in it. The implementation file should contain the implementations of the methods laid out in the header fine. And finally the Driver should test/demonstrate all the features of the class. It's best to develop the driver as the class is being written. Check each section to see if there are added additional requirements for the driver. Two test suites are included so that work can be checked. It's important to implement the drivers to test and demonstrate…arrow_forwardDevelop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. Develop the ‘Square’ and ‘Circle’ class where ‘Square’ inherits from ‘Rectangle’ and‘Circle’ inherits from ‘Ellipse’. ‘Triangle’ has no derived class. For each class, implement the overridden methods ‘draw’, ‘move’, and ‘erase’. Eachmethod should only have an output statement such as “Rectangle – draw method”that will be displayed when the method is invoked. Implement the default constructors for each class with a corresponding message tobe displayed when invoked. No initializations are required; that is, the outputmessage will be the only executable statement in the constructors. Do not implement any other methods for these classes ( i.e., ‘toString’, ‘equals’,getters and setters ). Implement a ‘ShapeTest’ class which will instantiate an object of each class. Exercise each of the ‘draw’, ‘move’, and ‘erase’ methods of each class Remember to make sure that…arrow_forwardDevelop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. Develop the ‘Square’ and ‘Circle’ class where ‘Square’ inherits from ‘Rectangle’ and‘Circle’ inherits from ‘Ellipse’. ‘Triangle’ has no derived class. For each class, implement the overridden methods ‘draw’, ‘move’, and ‘erase’. Eachmethod should only have an output statement such as “Rectangle – draw method”that will be displayed when the method is invoked. Implement the default constructors for each class with a corresponding message tobe displayed when invoked. No initializations are required; that is, the outputmessage will be the only executable statement in the constructors. Do not implement any other methods for these classes ( i.e., ‘toString’, ‘equals’,getters and setters ). Implement a ‘ShapeTest’ class which will instantiate an object of each class. Exercise each of the ‘draw’, ‘move’, and ‘erase’ methods of each class Remember to make sure that…arrow_forward
- Change the classes "Line" and "Matrix" as follows: a. all static properties of classes should be changed only inside class methods; b. select one or more sub methods (if it has not been done before) and arrange them as static methods.arrow_forwardWhy is it important for a class to have a destructor implemented? With your comment, fill in the spaces.arrow_forwardSELECT ALL the members of the class that are accessible via a static method in the supplied class below. Take note: In your response, leave off the constructor(s).arrow_forward
- Create a Right Triangle class that has two sides. Name your class rightTraingle. Code getter and setters for the base and the height. (Remember class variables are private.) The class should include a two-argument constructor that allows the program to set the base and height. The constructor should verify that all the dimensions are greater than 0. before assigning the values to the private data members. If a side is not greater than zero, set the value to -1. The class also should include two value-returning methods. One value-returning method should calculate the area of a triangle, and the other should calculate the perimeter of a triangle. If either side is -1, these functions return a -1. The formula for calculating the area of a triangle is 1/2 * b*h, where b is the base and h is the height. The formula for calculating the perimeter of a triangle is b+h+sqrt (b*b+h*h). Be sure to include a default constructor that initializes the variables of the base, height to -1. To test…arrow_forwardPrompt We have learned how to use accessor and mutator methods to access private class member data. It is possible to instead make the class members public, which would allow other programs to directly retrieve and modify the class member data without needing to write the accessor and mutator methods. This is usually considered bad practice. Why do you think that is? What do you think the benefits are to writing accessor and mutator methods instead of just leaving the variables public? Below is the definition for a class called Counter. Define a new method for this class called "findDifference". This method should take another Counter object as an argument and return the difference in the counts between the counter being called and the one passed as an argument. The difference should be given as an absolute value (not returned as a negative). See below the class definition for examples of this method being used. public class Counter {private int count;public Counter() {count =…arrow_forwardFinish the TestPlane class that contains a main method that instantiates at least two Planes. Add instructions to instantiate your favorite plane and invoke each of the methods with a variety of parameter values to test each option within each method. To be able to test the functionality of each phase, you will add instructions to the main method in each phase.arrow_forward
- Design and implement a Restaurant class with at least three meaningful attributes and two methods. Implement a test program which creates at least two instances of the restaurant class and calls all the methods. Some possible attributes are name, numberOfTables, cuisine, numberOfReservations, capacity, etc. Some possible methods are make Reservation, checkAvailability, etc. Note the following: -All properties must be declared as private and you need accessor and mutator methods for each property. - You need to define at least one constructor. Documentation o Include header comments that include description of the program. o Include body comments o Consistent indentations o Consistent white line spaces Properties are declared as private o Accessor and mutator methods are created o At least one default constructor o At least two instances are createdarrow_forwardB - RobotFriend Create a RobotFriend class. RobotFriend is a new toy just on the market. Copy the starter class and the tester from Codecheck. A RobotFriend has a name and replies to various commands Provide a constructor that takes the name of the RobotFriend as a parameter. Remember that a constructor has the same name as the class. What does the class need to remember? That is the instance variable. Provide the following methods: public String getName () gets the name of this RobotFriend • public void setName (String newName) sets a new name for this RobotFriend • public String whatDoYouNeed () returns a string consisting of name + needs a battery charge" where name is the name of this RobotFriend (which was supplied in the constructor) public String doCommand (String whatToDo) returns a string consisting of "Your friend " + name + does not + whatToDo where name is the name of this RobotFriend and whatToDo is the parameterarrow_forwardWrite the Movie class. The UML diagram of the class is represented below: 1. Implement the class strictly according to its UML one-to-one (do not include anythingextra, and do not miss any data fields or methods). 2. Implement a MovieTest class to test the class Movie you just created.• Create two Movie objects: one using the no-args constructor and one from theconstructor with all fields.• Print the contents of both objects. Please submit a screenshot. Hint: toString()method.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education