Question

What is the code for this in Java?

Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible Create an array of superclass type and use a foreach loop to iterate through it Style Comments On this and all upcoming assignments pay attention to style. Follow Java naming convention, use proper indentation group related code statements with single empty lines, use descriptive names etc. Strive to write code that is clear and easy to read Also: use doc comments on all your code Description: Write a program to demonstrate the use of inheritance and polymorphism. You will create 4 classes: Rectangle, Square, IsoscelesRightTriangle, and Circle In addition you will create a class called InheritanceApp. This class includes the main method. Here we test the four other classes and we demonstrate the polymorphic behavior Declare the classes as described below: Class Retangle: . Rectangle has 2 private final fields of type int: length and width It has (exactly) one parameterized constructor that initializes both fields It provides a getter (get accessor method) for each of the fields (no setter) It overrides the toString method so that it produces a result of the form Rectangle (lengthxwidth) e.g. Rectangle (5x4) Class Square: Square extends Rectangle No fields are declared in class Square It has a parameterized constructor with (only) one parameter The parameter is used to initialize both fields of Rectangle It has a method called getSide to expose the side-length of the square Override the toString method so that it will return a String of the following form Square (side) e.g. Square (4)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

InheritanceApp.java

public class InheritanceApp {

public static void main(String[] args) {

Rectangle myRectangle=new Rectangle(5,4);//rectangle object

Square mySquare=new Square(4);//square object

IsoscelesRightTriangle myIsoscelesRightTriangle=new IsoscelesRightTriangle(5);//isoscees right angle object

Circle myCircle=new Circle(4);//circle object

//printing the objects details that we have created earlier

System.out.println(myRectangle.toString()+"\nLength:"+myRectangle.getLength()+"\nWidth:"+myRectangle.getWidth());

System.out.println(mySquare.toString()+"\nSide:"+mySquare.getSide());

System.out.println(myIsoscelesRightTriangle.toString()+"\nLeg:"+myIsoscelesRightTriangle.getLeg()+"\nHypotenuse:"+myIsoscelesRightTriangle.hypotenuse());

System.out.println(myCircle.toString()+"\nDiameter:"+myCircle.diameter()+"\nCircumference:"+myCircle.circumference()+"\nRadius:"+myCircle.getRadius());

//creating another square object and assigning it to rectangle as it is parent class of square it can hold square object

Rectangle rectangle2=mySquare;

//printing the details

System.out.println("recatngle2:\n.................");

System.out.println();

System.out.println(rectangle2.toString()+"\nLength:"+rectangle2.getLength()+"\nWidth:"+rectangle2.getWidth());

System.out.println("Calling get side and see what happens"+((Square) rectangle2).getSide());

System.out.println("Rectangle Array:\n.............");

//creating the array of rectangle objects and assigining previous ly created square and rectangle objects

Rectangle[] rectangles={rectangle2,mySquare,myRectangle};

//printing the objects

for(int i=0;i<3;i++)

{

System.out.println(rectangles[i].toString()+"\nLength:"+rectangles[i].getLength()+"\nWidth:"+rectangles[i].getWidth());

}

}

}

Square.java

public class Square extends Rectangle {

public Square(int length) {//parameterized constructor

super(length, length);//calling super class constructor

}

public int getSide()//getter for side

{

return this.getLength();

}

public String toString() {//to string method

return "Square (" + getSide() + ")";

}

}

IsoscelesRightAngle.java

public class IsoscelesRightTriangle {

private int leg;//to hold value of leg

public IsoscelesRightTriangle(int leg) {

super();

this.leg = leg;

}

public double hypotenuse()//returns hypotensuse

{

return Math.sqrt(leg*leg+leg*leg);

}

public int getLeg() {//returns leg value

return leg;

}

//to string method

public String toString() {

return "IsoscelesRightTriangle (" + leg + ")";

}

}

Circle.java

public class Circle {

private int radius;//to hold radius

public Circle(int radius) {//parameterized constructor

super();

this.radius = radius;

}

public int getRadius() {//to get the radius

return radius;

}

public int diameter()//returns diameter 2*r

{

return 2*radius;

}

public double circumference()//returns circumference 2*pi*r

{

return 2*3.14*radius;

}

public String toString() {//to string method

return "Circle (" + radius + ")";

}

}

Rectangle.java

public class Rectangle {

private int length;//to hold length

private int width;//to hold width

public int getLength() {//getter for length

return length;

}

public int getWidth() {//getter for width

return width;

}

public Rectangle(int length, int width) {//parameterized constructor

super();

this.length = length;

this.width = width;

}

public String toString() {//to string methos

return "Rectangle (" + length + "X"+ width + ")";

}

}

Output:

<terminated > Inheritance Rectangle (5X4) Length:5 Width:4 Square (4) Side:4 Java Application CProgra IsoscelesRightTriangle

Add a comment
Know the answer?
Add Answer to:
What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student...

    Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student will demonstrate the ability to use inheritance in Java programs. Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following: Person Automobile Animal Based on your choice: If you choose Person, you will create a subclass of Person called Student. If you choose Automobile, you will create...

  • JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance...

    JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance and polymorphism. In this assignment we will use inheritance and polymorphism to solve a problem. Part (a) of the figure below shows a symbolic representation of an electric circuit called an amplifier. The input to the amplifier is the voltage vi and the output is the voltage vo. The output of an amplifier is proportional to the input. The constant of proportionality is called...

  • 1.   What will be the value of x after the following code is executed? int x...

    1.   What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } A.   90 B.   110 C.   210 D.   This is an infinite loop 2.   If a superclass has constructor(s): A.   then its subclass must initialize the superclass fields (attributes). B.   then its subclass must call one of the constructors that the superclass does have. C.   then its subclass does not inherit...

  • java. do it on eclipse. the same way its instructed Second Inheritance OOP assignment Outcome: ...

    java. do it on eclipse. the same way its instructed Second Inheritance OOP assignment Outcome:  Student will demonstrate the ability to understand inheritance Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following:  Person  Automobile  Animal  If you choose Person, you will create a subclass of Person called Student.  If you choose Automobile, you will create a...

  • This assignment helps you understand Inheritance. We want to create a superclass or parent class called...

    This assignment helps you understand Inheritance. We want to create a superclass or parent class called Shape. This shape class will have just one method to setBorderColor. Now we need 3 subclasses that extend the Shape class, Circle, Square and Rectangle. In these classes we will need to compute the area of each of the shapes and print it to the console using System.out.println You will need to take in an additional parameter in each of these subclasses -- side...

  •            1.         You often need to know the inheritance ____________________ of the Java API to work...

               1.         You often need to know the inheritance ____________________ of the Java API to work with its classes and subclasses.            2.         All objects have access to the methods of the ____________ class.            3.         If two classes share some common elements, you can define those elements in a ____________.            4.         To call a constructor or method of a superclass from a subclass, you use the ____________ keyword.            5.         A class that can be inherited by another...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • Java Programming assignment. 1. Create a class called Square that takes a width parameter in the...

    Java Programming assignment. 1. Create a class called Square that takes a width parameter in the constructor. The Square class should have a draw() method that will draw the square on the screen. Create a class called TestSquare that will take width from the user, create an object of Square, and invoke the draw() method on the square object. Below is a UML diagram for the Square and Rectangle class: 3. Create a zip file that contains your Java programs....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT