Concept explainers
Look at the following class declarations and answer the questions that follow them:
public class Shape
{
private double area;
public void setArea(double a)
{
area = a;
}
public double getArea( )
{
return area;
}
}
public class Circle extends Shape
{
private double radius;
public void setRadius(double r)
{
radius = r;
setArea(Math.PI * r * r);
}
public double getRadius()
{
return radius;
}
}
- a) Which class is the superclass? Which class is the subclass?
- b) Draw a UML diagram showing the relationship between these two classes.
- c) When a Circle object is created, what are its public members?
- d) What members of the Shape class are not accessible to the Circle class’s methods?
- e) Assume a program has the following declarations:
Shape s = new Shape();
Circle c = new Circle();
Indicate whether the following statements are legal or illegal:
c. setRadius(10.0);
s.setRadius(10.0);
System.out.println(c.getArea());
System.out.println(s.getArea());
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Starting Out with Java: Early Objects Plus MyLab Programming with Pearson eText -- Access Card Package (6th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Modern Database Management (12th Edition)
Programming in C
Concepts Of Programming Languages
- Java - Polymorphism 1.Create a class named “Animal” that has the method sound() which prints “Animals make sound.” 2.Create a class named “Cat”, “Dog”, “Pig”, “Rat” which all extends the “Animal” class. 3.Override the sound() method from Animal. These are the sound of the animals: Cat - prints "*meow meow*" Dog - prints "*barf barf*" Pig - prints "*oink oink*" Rat - prints "*squeak squeak*" 4.Call the sound() methods in the same order as stated in #3.arrow_forwardQuestion: (use java program) (Person, Student, Employee, Faculty and Staff classes) 1) Design a class named Person which has a name, address, phone_number and email_address with the toString() method. 2) Create 2 sub-classes from Person called Student and Employee 3) Student has class status (which is constant) - Fresh-man or junior or Senior. 4) Employee has an office_location and Salary 5) Create 2 sub-classes from Employee called Faculty and Staff. 6) Faculty has office_hours and rank 7) Staff has title Overirde the toString() method in each class to display the class name and Person's name. Hint:arrow_forwardThe following code is in Java: public class Vehicle // Superclass { private double cost; public Vehicle(double c) { cost = c; } (Other methods . . .) // assume these are given and they’re OK. } public class Car extends Vehicle // Subclass { private int passengers; public Car(int p) { passengers = c; } (Other methods . . .) // assume these are given and they’re OK. } 1) What problems are in the code above?2) What is needed to fix the issue of the code?arrow_forward
- CLASS AND OBJECT WRITE! 1.) Write the main class of the given class Animal below that set the number of legs to “4” and display (get) the number of legs. class Animal { private int numberOfLegs; public void setNumberOfLegs(int numberOfLegs) { this.numberOfLegs = numberOfLegs; } public int getNumberOfLegs() { return numberOfLegs; } }arrow_forward2. Problem (Cylinder.java) (SEE ATTACHED PHOTO FOR CORRECTION)CREATE A Cylinder.java programa. Implement a class called Cylinder. A cylinder has a circle and one additional data member for representing the height (type float).b. Create appropriate constructors for your Cylinder class.c. Encapsulate it.d. Include methods for finding the volume and area of your Cylinder. area = 2 * (area of the circle in this cylinder) + 2 * pi * radius + height volume = (area of the circle in this cylinder) * heighte. Override the toString method of the Object class. Try to use the toString of the Circle class and then just concatenate the remaining values.f. Override the equals method of the Object class. Two Cylinder objects are equal if they have the same center and the same radius and the same height. g. Write a simple main method that creates Cylinder objects and tests each of the methods that you have defined.arrow_forwardUse the class diagram below to create a Parking Charge class for an object-oriented parking system. Write your code using java. Classes should contain properties and method implementations. N.B. Try to make your code readable.arrow_forward
- A publishing house prints and distributes two types of publication: books and journals. Each book has a title, an author, an edition number, and an ISBN. Each journal has a title, a volume number, an issue number, and an ISBN. Each of these publication have a statement that prints their properties i. Design the classes needed for the problem specification above using UML class diagram. ii. Convert your UML class diagram in question (1i) into a java program. Create a test class and print out details about the object of the classes. The following object oriented concepts should be used in your program. Data encapsulation, Method Overriding, Method Overloading, Inheritance, Polymorphism, Super to call a constructor and Super to call a method. Please comment your code to show how each of these concepts have been applied. iii. Using your classes in question (1ii), draw a UML sequence diagram to show sequence of interaction between objects at runtime (that is, the flow of controls by method…arrow_forwardJava program Make a class Employee with a name and salary. Make a class Manager inherit from Employee. Add an instance variable, named department, of type String. Supply a method toString that prints the manager’s name, department, and salary. Make a class Executive inherit from Manager. Supply appropriate toString methods for all classes. Supply a test program that tests these classes and methods.arrow_forwardIn this exercise, you have to calculate the student's total marks using the concept of Classes Problem Statement# Write a Java class called Student with ● private fields: ○ name(String type) ○ mark1 and mark2 (double type) And methods: ● getMarks(int markNumber), a method which should return mark1 if markNumber equals 1 and mark2 otherwise. ● calcTotal() method should take the two marks entered and return their sum. Also define two constructors: ● A default constructor that takes no parameters and initializes the values to zeros and null. ● A constructor that takes the three variables and sets them as the values of the appropriate fields. Input# Name of the student and the marks obtained in the first and second tests Output# Sum of both marks Sample Input# Student student = new Student("Jack", 60, 70); Sample Output# getMarks(1) => 60 getMarks(2) => 70 calcTotal() => 130.0 Part of solution class Student { // Define private fields here public Student() { // Write…arrow_forward
- write all the code necessary for a class Toaster as described here. A toaster can be described as having a model (string), manufacture(string), and price. include the following methods in your class definition.(in java) 1. a default constructor2. an overloaded constructor takes the model and manufacture as input.3. an accessor method to acquire the price.4. a tostring method that returns a reasonable description of the toaster.arrow_forward(programming) A class may be made immutable in a number of ways. with the aid of an illustrationarrow_forwardWrite Java classes to solve the following problem: A company has two types of employees. These are called Exempt and NonExempt. Exempt means the employee is not eligible for overtime pay and is paid a fixed amount each pay period. NonExempt means the employee is paid by the hour and receives 1.5 times the hourly rate for hours worked over 40. Each Employee has an id number which is a string. They also have a name. Employee should be an abstract class with the member variables described in number 2. Employee should have an abstract method called findPay, a toString method, and it should implement the Comparable interface. Use alphabetical order of id number to determine less than, greater than, and equal to. The toString method should produce a string like the following: Emp: ID: 4590 Emp Name: Fred Jones Employee should have an equals method. Define two employees to be equal if they have the same name and the same id number. Provide accessor and mutators for Employee. NonExempt should…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,