EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
expand_more
expand_more
format_list_bulleted
Question
Chapter 3, Problem 3PE
Program Plan Intro
Object:
- The objects are instance of class.
- In java, the fields are the members of a class. The object can contain the methods and data items of a class.
- The object should be allocated a memory. To allocate memory to an object, the user must use “new()”.
- Syntax of declaring an object,
classname objectname = new classname();
- The object of one class can access the methods and variables of another class declare as public.
- The members of the class are accessed as,
objectname.member;
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a class named Employee that has the following fields:
name: The name field is a String object that holds the employee's name.
idNumber: The idNumber is an integer variable that holds the employee's ID
number.
department: The department field is a String object that holds the name of the
department where the employee works.
position: The position field is a String object that holds the employee's job title.
Write mutator methods that store values in these fields and accessor methods that return
the values in these fields. Once you have written the class, write a separate program that
creates three Employee objects to hold the following data.
Name
ID Number
Azah Samsudin
Mira Kamal
Che Yah Sam
Department
Computer Science
Biomedical
Language
Position
Programmer
Researcher
Lecturer
11011
35125
80001
The program should store the data in the three objects and then display the data for each
employee on the screen.
Question
Create a class called Quadratic for performing arithmetic on and solving quadratic equations. A quadratic equation is an equation of the form
ax2 + bx + c = 0
Where a !=0. Use double variables to represent the values of a, b, and c and provide a constructor that enables objects of this class to be initialized when they are created. Give default values of a = 1, b = 0, and c = 0. Create a char variable called variable to represent the variable used in the equation and give it a default value of x. The constructor should not allow the value of a to be 0. If 0 is given, assign 1 to a). Provide public member functions that perform the following tasks.
add—adds two Quadratic equations by adding the corresponding values of a, b, and c.
The function takes another object of type Quadratic as its parameter and adds it to the calling object.
subtract—subtracts two Quadratic equations by subtracting corresponding values of a, b, and…
Design a Ship class that has the following members:a. A field for the name of the ship (a string).b. A field for the year that the ship was built (a string).c. A constructor and appropriate accessors and mutators.d. A toString method that displays the ship's name and the year it was built.Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members:a. A field for the maximum number of passengers (an int).b. A constructor and appropriate accessors and mutators.c. A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should display only the ship's name and the maximumnumber of passengers.Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members:a. A field for the cargo capacity in tonnage (an int).b. A constructor and appropriate accessors and mutators.c. A toString method that overrides the toString method in the base class. The…
Chapter 3 Solutions
EBK JAVA PROGRAMMING
Ch. 3 - Prob. 1RQCh. 3 - Prob. 2RQCh. 3 - Prob. 3RQCh. 3 - Prob. 4RQCh. 3 - Prob. 5RQCh. 3 - Prob. 6RQCh. 3 - Prob. 7RQCh. 3 - Prob. 8RQCh. 3 - Prob. 9RQCh. 3 - Prob. 10RQ
Ch. 3 - Prob. 11RQCh. 3 - Prob. 12RQCh. 3 - Prob. 13RQCh. 3 - Prob. 14RQCh. 3 - Prob. 15RQCh. 3 - Prob. 16RQCh. 3 - Prob. 17RQCh. 3 - Prob. 18RQCh. 3 - Prob. 19RQCh. 3 - Prob. 20RQCh. 3 - Prob. 1PECh. 3 - Prob. 2PECh. 3 - Prob. 3PECh. 3 - Prob. 4PECh. 3 - Prob. 5PECh. 3 - Prob. 6PECh. 3 - Prob. 7PECh. 3 - Prob. 8PECh. 3 - Prob. 9PECh. 3 - Prob. 10PECh. 3 - Prob. 11PECh. 3 - Prob. 12PECh. 3 - Prob. 13PECh. 3 - Prob. 1GZCh. 3 - Prob. 2GZCh. 3 - Prob. 1CP
Knowledge Booster
Similar questions
- Write a program named SalespersonDemo that instantiates objects using classes named Real EstateSalesperson and GirlScout. Demonstrate that each object can use a SalesSpeech() method appropriately. Also, use a MakeSale() method two or three times with each object, and display the final contents of each objects data fields. First, create an abstract class named Salesperson. Fields include first and last names; the Salesperson constructor requires both these values. Include properties for the fields. Include a method that returns a string that holds the Salespersons full name—the first and last names separated by a space. Then perform the following Create two child classes of Salesperson: Real EstateSalesperson and Girl Scout. The Real EstateSalesperson class contains fields for total value sold in dollars and total commission earned (both of which are initialized to 0), and a commission rate field required by the class constructor. The Girl Scout class includes a field to hold the number of boxes of cookies sold, which is initialized to 0. Include properties for every field. Create an interface named ISell able that contains two methods: SalesSpeech() and MakeSale(). In each Real EstateSalesperson and Girl Scout class, implement SalesSpeech() to display an appropriate one- or two-sentence sales speech that the objects of the class could use. In the Real Estatesalesperson class, implement the MakeSale() method to accept an integer dollar value for a house, add the value to the Real EstateSalespersons total value sold, and compute the total commission earned. In the Girl Scout class, implement the MakeSale() method to accept an integer representing the number of boxes of cookies sold and add it to the total field.arrow_forwardWhich of the statements are true about the class Magazine? 01: class Magazine { 02: public: 03: Magazine (int maxSize = 100); 04: void add (Article a); 05: int numArticles() const; 06: Article getArticle (int i); 07: private: 08: Article* array; 09: int numberOfArticles; 10: }; The compiler will provide a destructor for this class. It has a default constructor It is const-correct. It has a copy constructor The programmer should provide an assignment operatorarrow_forwardIn C#, How would you compile the necessary code to define a class that will store a point in space. It will need the following data members: xcoord, ycoord. Include the following methods:- a default constructor that will set the coordinates to 0,0- a constructor that takes two values corresponding to the xcoord and ycoord values and sets them accordingly- accessor methods to get and set the values of the coordinates (you can use properties if you wish)- a display constructor that returns a string in the format of (xcoord, ycoord) Usage of the class: //create an instance of 2 points Point myPoint = new Point; //assign values using properties |If you do not declare properties for the data items, you might do the following myPoint.Xcoord = 42; |myPoint.SetX(42); myPoint.Ycoord = 19.63; |myPoint.SetY(19.63) // use the Display() method WriteLine(“The point is “+myPoint.Display()); //output The point…arrow_forward
- Using pythonarrow_forwardCreate a class called Rational to be able to perform arithmetic operations with fractions. testing your class and Write a program that presents a menu to the user to perform the desired action. User enters -1 value You can trade as much as you want. Use integer variables to represent the private data members (numerator and denominator) of the class. to this class Write a constructor function that initializes an object of the property as soon as it is declared. Constructive the function must contain default values for cases where no initial values are given and use the fractional number should store it in reduced form (i.e. the fraction 2/4 should be reduced to 1 for the numerator and 2 for the denominator). Provide public member functions for each of the following: ‐ The sum of two fractions - Subtraction of two fractions ‐ Multiplication of two fractions ‐ The division of two fractions In all of the above functions, results must be stored in a reduced form. Also, write the…arrow_forward1- Design a class named Rectangle to represent a Rectangle. The class contains: • three variables integer data field named height, integer data filed named width and integer data filed named length. The default values are 1 for height, length, and width are 1.0 for each of them respectively. • A no-arg constructor that creates a default Rectangle. • A constructor that creates a Rectangle with the specified height, length and width. • A method named getVolume() that returns the volume of this Rectangle. (Volume = height * width * length) %3! 2- Write a test program called Test Rectangle that: • Creates two Rectangle objects: one object with height 11, length 5, and width 7, the other object with height 10, length 2 and width 6 • Display the volume of each Rectangle object.arrow_forward
- In Javascript, Pet Class: Design a class named Pet, which should have the following fields and methods: name: The name of a pet type: The type of animal the pet is ("Dog", "Cat", "Lizard", etc.) age: The age of the pet. setName: The setter for the name field. getName: The getter for the name field. setType: The setter for the type field. getType: The getter for the type field. setAge: The setter for the age field. getAge: The getter for the age field. Once you have designed the class, create an instance of the class and use the setter and getter methods to store data in the object and display the data.arrow_forwardDesign a class called Date that has integer data members to store month , day, and year. The class should have two constructors; a default constructor that accepts no parameters, and a three-parameter constructor that allows the date to be set by the programmer. If the user creates a Date object without passing any arguments, or if any of the values passed are invalid, the default values of 1, 1, 2001 (i.e., January 1, 2001) should be used. The class should have three (3) member functions to print the date in the following formats: 3/15/16 March 15, 2016 15 March 2016 Demonstrate the class by writing a program that uses it. Be sure your program only accepts reasonable values for month and day. The month should be between 1 and 12. The day should be between 1 and the number of days in the selected month. I expect to see all uses of the constructors that you made, including the error checking. I also expect to see all uses of the 3 output functions in your program. You are responsible…arrow_forwardWrite a class named Car that has the following data attributes: _ _year_model (for the car’s year model) _ _make (for the make of the car) _ _speed (for the car’s current speed) The Car class should have an _ _init_ _ method that accepts the car’s year model and make as arguments. These values should be assigned to the object’s _ _year_model and _ _make data attributes. It should also assign 0 to the _ _speed data attribute. The class should also have the following methods: accelerate - The accelerate method should add 5 to the speed data attribute each time it is called but not higher than 100. brake - The brake method should subtract 5 from the speed data attribute each time it is called but not lower than 0. get_speed - The get_speed method should return the current speed. Next, design a program that creates a Car object then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the…arrow_forward
- Write a class named Car that has the following data attributes: __year_model: (for the car's year model)__make: (for the make of the car)__speed: (for the car's current speed) In addition, the class should have the following methods: __init__ method that accepts the car’s year model and makes arguments. These values should be assigned to the object’s __year_model and __make data attributes. It should also assign 0 to the __speed data attribute.accelerate: The accelerate method should add 5 to the speed data attribute when it is called.brake: The brake method should subtract 5 from the speed data attribute each time it is called.get_speed: The get_speed method should return the current speed Next, design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car…arrow_forwardThis is the question - 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 the code I have, the program does not like at all what I have - import java.util.*; public class DemoRock { public static…arrow_forwardThis is the question - 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. I am only missing two checks and they seem to be with the rock, here is my code then I will copy one check - import java.util.*;…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,