Absolute Java (6th Edition)
6th Edition
ISBN: 9780134041674
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 4, Problem 10PP
Program Plan Intro
To modify the code that is mentioned in the Display 4.15 and 4.16 in chapter-4 by adding the instance variable to specify whether the type of pet is cat or dog. Also, modify the constructors of Display 4.15 and add some methods, such as acepromazine and carprofen.
Program Plan:
Modify a code in Java and add some methods, such as acepromazine and carprofen. Also, add one instance variable to specify the type of pet and modify the constructor as well.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Modify the student class presented in this chapter as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test score is assumed to be initially zero. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called getTestScore that accepts the test number and returns the appropriate score Provide a method called average that computes and returns the average test score for this student. Modify the toString method such that the test scores and average are included in the description of the student. Modify the driver class main method to exercise the new Student methods. (c++ language)
Modify the Car class example and add the following:
A default constructor that initializes strings to “N/A” and numbers to zero.
An overloaded constructor that takes in initial values to set your fields (6
parameters).
accel() method: a void method that takes a double variable as a parameter and
increases the speed of the car by that amount.
brake() method: a void method that sets the speed to zero.
To test your class, you will need to instantiate at least two different Car objects in the main method in the CarDemo class.
Each of your objects should use a different constructor. For the object that uses
the default constructor, you will need to invoke the mutator methods to assign
values to your fields.
Invoke the displayFeatures() method for both objects and display the variables.
Demonstrate the use of the accessor method getColor() by accessing the color of each car and printing it to the console.
Use the method accel to increase the speed of the first car by 65 mph.
Use…
Create one more method in the Starship class:
encounter(self, enemy_ship)
This method takes in self, along with another Starship object.
The method should simulate a battle between the two ships.
How the method is implemented is almost entirely up to you. You could take in user input for each crew member, or have them each act randomly from the available options, or specify exactly what a given crew member of each type should do. The only requirements are as follows:
Each crew member on each ship should get at least one “turn” before the battle is complete.
If the crew member is Injured on their turn, they should do nothing.
You may assume that each ship will have at least one crew member uninjured when the encounter begins.
You shouldn’t make any assumptions about how many crew members there will be on a given ship, where they will start, or about which subclasses will be present - it’s possible that you’ll have test cases including a ship with no captain, or no doctor.…
Chapter 4 Solutions
Absolute Java (6th Edition)
Ch. 4 - Write a method called makeItNewYears that could be...Ch. 4 - Write a method called yellIfNewYear that could be...Ch. 4 - Prob. 3STECh. 4 - Write a method called happyGreeting that could be...Ch. 4 - Prob. 5STECh. 4 - Write a method called advanceYear that could be...Ch. 4 - Prob. 7STECh. 4 - Prob. 8STECh. 4 - Consider the definition of the method monthString...Ch. 4 - The method writeOutput in Display 4.2 uses the...
Ch. 4 - Rewrite the definitions of the methods getDay and...Ch. 4 - Rewrite the method getMonth in Display 4.2 using...Ch. 4 - In the definition of precedes in Display 4.7, we...Ch. 4 - Prob. 14STECh. 4 - Following the style guidelines given in this book,...Ch. 4 - Prob. 16STECh. 4 - List all the accessor methods in the class...Ch. 4 - List all the mutator methods in the class...Ch. 4 - Write a better version of the method dateOK with...Ch. 4 - What is the signature of each of the following...Ch. 4 - Prob. 21STECh. 4 - Prob. 22STECh. 4 - Prob. 23STECh. 4 - If a class is named CoolClass, what names are...Ch. 4 - Suppose you have defined a class such as the...Ch. 4 - What is a no-argument constructor? Does every...Ch. 4 - What would be the last line in the dialogue in...Ch. 4 - What would be the last line in the dialogue in...Ch. 4 - Write a program that outputs a certain coded...Ch. 4 - Prob. 2PPCh. 4 - Define a class called Journal that could be used...Ch. 4 - Define a class called WordCount whose objects...Ch. 4 - Write a grading program for a class with the...Ch. 4 - Prob. 7PPCh. 4 - Redefine the class Date in Display 4.13 so that...Ch. 4 - Define a class whose objects are records on animal...Ch. 4 - Prob. 10PPCh. 4 - Prob. 11PPCh. 4 - This programming project extends Programming...Ch. 4 - Your Community Supported Agriculture (CSA) farm...Ch. 4 - A comma-separated values (CSV) file is a simple...Ch. 4 - Prob. 15PP
Knowledge Booster
Similar questions
- Finish 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_forwardCreate a deck of cards class. Internally, the deck of cards should use another class, a card class. Your requirements are: The Deck class should have a deal method to deal a single card from the deck After a card is dealt, it is removed from the deck. There should be a shuffle method which makes sure the deck of cards has all 52 cards and then rearranges them randomly. The Card class should have a suit (Hearts, Diamonds, Clubs, Spades) and a value (A,2,3,4,5,6,7,8,9,10,J,Q,K)arrow_forwardUsing classes and objects, create a program that will show method overloadingarrow_forward
- I would like you to create a program that keeps track of the inventory (by make - e.g., Toyota) of cars on a used car lot. you will have to create two classes. The first is a simple Car class that consists of an inventory, make, and model. The second class you need to create for this challenge is the Lot. A Lot can contain many cars. Part 1 Write the Car constructor, get methods, and toString method. Part 2 Write the Car's increase and decrease inventory methods. Part 3 Edit the Car so it implements Comparable and compares Car instances based on their inventory in descending order. Part 4 Write the getCars and getCarsSorted methods of the Lot class. Part 5 Write the getInventory method of the Lot class. Part 6 Use a try/catch block in the Car's constructor so that it sets the inventory to 0 if the inventory passed to the constructor is not a valid integer. this is the output that should be produced by main: Make: [Toyota Camry (10), Toyota Prius (4), Toyota Rav4 (16)] [Hyundai…arrow_forwardCreate a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variable Part number(type String) Part description(type String) Quantity of the item being purchased (type int) Price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.arrow_forwardThe class "Car" has the following attributes: plate (String), mark (String), model (String), year (int), km (int).Write a constructor method for the "Car" class that takes values as parameters for all these attributes.arrow_forward
- This is the question I am stuck on - Create a class named Rock that acts as a superclass for rock samples collected and catalogued by a natural history museum. The Rock class contains the following fields: sampleNumber - of type int description - A description of the type of rock (of type String) weight - The weight of the rock in grams (of type double) Include a constructor that accepts parameters for the sample number and weight. The Rock constructor sets the description value to "Unclassified". Include get methods for each field. Create three child classes named IgneousRock, SedimentaryRock, and MetamorphicRock. The constructors for these classes require parameters for the sample number and weight. Search the Internet for a brief description of each rock type and assign it to the description field using a method named setDescription inside of the constructor. This is what I am given - import java.util.*; public class DemoRocks { public static void main(String[] args) {…arrow_forwardCreate a class called Point which will have 2 co-ordinate value x & y and will be able to calculate the distance between two points. Write appropriate constructor and method of the class. Create another class called Shape, which takes different number of points to create a triangle, rectangle and circle. And also able to calculate the area and perimeter for the different shapes. Write the appropriate overloading method for this shape class. Write appropriate main function to test the shape class. (Use C++ )arrow_forwardYou will create a couple of classes that could be used to create a Text Adventure game. The class will be called "Item" and will represent an item in the game that can be carried and used by a player (such as a weapon or a tool). The Item Class The Item class will have only one property: A string description. The Item class will have exactly 3 methods: There will be a default constructor that takes no parameters. This constructor will give a generic, default description of the Item. There will be a constructor that takes a string parameter, and it will initialize the Item's description based on the parameter. There will be a method called "print" that will print the Item's description to standard output. PLEASE MAKE SURE ITS POSSIBLE TO COMPILE WITH THIS CODE. THANKS. #include "Item.h" #include <iostream> #include <string> using namespace std; int main() { Item sword("Sword of Destiny"); Item potion("Healing Potion"); Item key("Key of Wisdom"); string name; cout…arrow_forward
- Part I Create a Warrior class that has properties given below. • String name, • int health, • int strength. Write the necessary constructor, get & set, and toString methods. Part II Write an defense method Warrior class that takes an integer parameter, hitPoint. The method decreases the Warrior's health depending on hitPoint. Part III Write a battle method for another class (Test) that takes two parameters: (1) Warrior object, (2) an integer monsterStrength parameter. The Warrior object defenses the attacks of the monster depending on monsterStrength until the Warrior health is below 30. Note: Do not compress your files, submit only java files. Note: Do ot forget to justify your answers with comments.arrow_forwardPart 1: Setting the turtle size when it is instantiated Currently, a turtle is created and given a default size of 50. To change the size of the turtle its increaseTurtleSizeBy10 and decreaseTurtleSizeBy10 methods can be called. Add functionality so that a turtle can be created with a specified size. In the DoodleTurtle class do the following: Overload the DoodleTurtle constructor by creating a one-parameter constructor that takes the size of the turtle as an integer. Add an empty method body. Complete the method specification for the new constructor. The default constructor method specification can be used as a model. The only difference will be that the getTurtleSize() will equal the name of the size parameter instead of INITIAL_SIZE. In the method body of the one-parameter constructor do the following in this order: Call the initializeWithDefaultValues helper method. Call the setSize method passing it the size parameter. Compile the program and fix any…arrow_forwardCreate a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four information as instance variables: a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a tester class named InvoiceTest that demonstrates class Invoice’s capabilities by creating two invoices then changing and printing their characteristics.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