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 10 Solutions
Starting Out with Java: From Control Structures through Objects (6th Edition)
Additional Engineering Textbook Solutions
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Concepts Of Programming Languages
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
- For the following four classes, choose the correct relationship between each pair. public class Room ( private String m type; private double m area; // "Bedroom", "Dining room", etc. // in square feet public Room (String type, double area) m type type; m area = area; public class Person { private String m name; private int m age; public Person (String name, int age) m name = name; m age = age; public class LivingSSpace ( private String m address; private Room[] m rooms; private Person[] m occupants; private int m countRooms; private int m countoccupants; public LivingSpace (String address, int numRooms, int numoccupants) m address = address; new int [numRooms]; = new int [numOceupants]; m rooms %3D D occupants m countRooms = m countOccupants = 0; public void addRoom (String type, double area)arrow_forwardQUESTION 15 public class numClass { private int a; private static int y = 10; public numClass(int newX) {a=newx; } public void set(int newx) { a=newx; y+=a; } public void setY(int newy) { y=newY; } public static int getY() { return y; } }// end of class public class output { public static void main(String[] args) { numClass one = new numClass(10); numClass two = new numClass (2); try{ one. sety(30); two.set(4); if(one.getY()==30) throw new Exception ("30"); one.setY(40); } catch (Exception e) { two.sety (50); } System.out.println(two.getY()); }// end of main } // end of classarrow_forward8. OOPS Theory #6 public class Car { public void horn() { System.out.println("Horn horn"); } } public class Ferrari extends Car { public void horn() { System.out.println("Ferarriiiiii"); } } public static void main(String[] args) { Ferrari ferrari = new Car(); ferrari.horn(); } Pick ONE option Horn horn Ferarriiii Cannot compile Runtime exception Clear Selection 9. OOPS Theory #7 In Java, we can achieve the abstraction by creating the blueprint of an object and only have abstract methods. What do we call this? Pick ONE option Interface Encapsulation Instance Protected Clear Selectionarrow_forward
- // Simulates a simple car with operations to drive and check the odometer.public class SimpleCar { // Number of miles driven private int miles; public SimpleCar(){ miles = 0; } public void drive(int dist){ miles = miles + dist; } public void reverse(int dist){ miles = miles - dist; } public int getOdometer(){ return miles; } public void honkHorn(){ System.out.println("beep beep"); } public void report(){ System.out.println("Car has driven: " + miles + " miles"); } }arrow_forwardAnalyze the code below and answer the questions that follows.class Shapes {public void identify () {System.out.println("Identifying Shapes...");}}class Square extends Shapes {public void identify () {System.out.println("Identifying Square...");}}class Circle extends Shapes {170public void identify () {System.out.println("Identifying Circle...");}}class Main {public static void main(String[] args) {Square s1 = new Square();s1.identify();Circle c1 = new Circle();c1.identify();}}a. What OOP concept (s) is illustrated in the code? Explain your answer.b. What is the output of the code?arrow_forward26. Assume the declaration of Exercise 24. A. Write the statements that derive the class dummyClass from class base as a private inheritance. (2 lines are { and }.) "A1 is { "A2 is "A3 is )) B. Determine which members of class base are private, protected, and public in class dummyClass. "B1 is dummyClass is a dearrow_forward
- Public classTestMain { public static void main(String [ ] args) { Car myCar1, myCar2; Electric Car myElec1, myElec2; myCar1 = new Car( ); myCar2 = new Car("Ford", 1200, "Green"); myElec1 = new ElectricCar( ); myElec2 = new ElectricCar(15); } }arrow_forwardPublic Class SavingsAccount { float interest; float FixedDeposit; SeniorAccount(float interest, float FixedDeposit) { this. interest = interest; this.fixedDeposit= FixedDeposit; } float calculateInterest(); { System.out.println(“Calculating Savings Account Interest”); return(FixedDeposit*interest/100); } } Public Class SeniorAccount extends SavingAccount { float seniorInterest; SeniorAccount(float interest, float FixedDeposit) { this.seniorInterest=interest; super(interest, FixedDeposit) } float calculateInterest() { System.out.println(“Calculating Savings Account Interest”); return(FixedDeposit*seniorinterest/10); } } Public static void main(String args[]) { SavingsAccount saving = new SavingsAccount(6,100000); System.out.println(saving.calculateinterest()); SeniorAccount senior=new seniorAccount(10,100000); System.out.println(Senior.calculateInterest()); } Correct the syntax and logical errorarrow_forwardPublic Class SavingsAccount { float interest; float FixedDeposit; SeniorAccount(float interest, float FixedDeposit) { this. interest = interest; this.fixedDeposit= FixedDeposit; } float calculateInterest(); { System.out.println(“Calculating Savings Account Interest”); return(FixedDeposit*interest/100); } } Public Class SeniorAccount extends SavingAccount { float seniorInterest; SeniorAccount(float interest, float FixedDeposit) { this.seniorInterest=interest; super(interest, FixedDeposit) } float calculateInterest() { System.out.println(“Calculating Savings Account Interest”); return(FixedDeposit*seniorinterest/10); } } Public static void main(String args[]) { SavingsAccount saving = new SavingsAccount(6,100000); System.out.println(saving.calculateinterest()); SeniorAccount senior=new seniorAccount(10,100000); System.out.println(Senior.calculateInterest()); } Rubric: Correct the syntax and logical errors Proper working codearrow_forward
- class Time { private int hour; private int minute; private int second; public Time( ) { } public Time(int hr , int min, int sec) { } public void setTime(int hr , int min, int sec) { } public void printTime( ) { } public void tick( ) { // tick method defined } } The class Time defines a time object. It represents time (military time) in 2400 hours. 10 AM = 10 hours, 0 minutes and 0 seconds 4 PM = 16 hours, 0 minutes and 0 seconds 7:26PM = 19 hours, 26 miunutes and 0 seconds Assume 0 represents 00 in this case. 1) Implement the two constructor: Time(): will set the instance variables to zero. Time(int hr , int min, int sec): will set the time with parameters passed in. 2) Implement the setTime method to set the instance variables according to the values passed in. 3) Implement the printTime method to print time in hours:minutes:seconds e.g.…arrow_forwardI have the following code: public class Length{ private int feet; private int inches; Length() { feet = inches = 0; } Length(int newFeet, int newInches) { feet = newFeet; inches = newInches; } public int getFeet() { return feet; } public void setFeet(int newFeet) { feet = newFeet; } public int getInches() { return inches; } public void setInches(int newInches) { inches = newInches; } public void add(Length otherLength) { int newFeet = this.feet + otherLength.feet; int newInches = this.inches + otherLength.inches; if(newInches >= 12) { newFeet++; newInches -= 12; } this.setFeet(newFeet); this.setInches(newInches); } public Length subtract(Length otherLength) { if(this.feet > otherLength.feet) { int newFeet = feet - otherLength.feet; int newInches = inches - otherLength.inches;…arrow_forwardThe following class declaration has errors. Locate as many as you can. class Circle {private:double diameter; int centerX; int centerY; public:Circle(double d, int x, int y){ diameter = d; centerX = x; centerY = y; } // Overloaded = operatorvoid Circle=(Circle &right) { diameter = right.diameter; centerX = right.centerX; centerY = right.centerY; }... Other member functions follow ...};arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning