What is the code for this in Java?
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:
What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...
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 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 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 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 = 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: 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 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 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 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 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....