I used implementing inheritance in my program and the method in my subclass is not being called in the main method. It's showing the error "The method getArea() is not defined in type Second". the same problem with getPerimeter() method.
I've tried setting the values and changing parameters.
package firstproject;
import java.util.Scanner;
import java.util.Date;
import java.util.ArrayList;
public class Second{
public String color="red" ;
public boolean filled;
public Second() {
}
public Second(String tcolor, boolean tfilled) {
tcolor=color;
tfilled=filled;
}
public String getColor() {
return color;
}
public boolean getfilled() {
return filled;
}
public void setColor(String tcolor) {
tcolor=color;
}
public void setFilled(boolean tfilled) {
tfilled=filled;
}
public String toString() {
return "Color is =" +color+ " and it is filled or not = "
+filled;
}
class myclass extends Second {
double s1=1.0;
double s2=1.0;
double s3=1.0;
public myclass(){
}
public myclass(double s4, double s5, double s6) {
s4=s1;
s5=s2;
s6=s3;
}
double gets1() {
return s1;
}
double gets2() {
return s2;
}
double gets3() {
return s3;
}
public void sets1(double s4) {
s4=s1;
}
public void sets2(double s5) {
s5=s2;
}
public void sets3(double s6) {
s6=s3;
}
public double getArea() {
return (s2*s3)/2;
}
public double getPerimeter() {
return s1+s2+s3;
}
}
public static void main(String[] args) {
System.out.println("Enter the three sides = ");
Scanner input=new Scanner(System.in);
int side1=input.nextInt();
int side2=input.nextInt();
int side3=input.nextInt();
Second Triangle= new Second();
System.out.println("Enter the color = ");
String colo=input.next();
System.out.println("The boolean value = ");
String fil =input.next();
System.out.println("The area of the triangle is = "
+Triangle.getArea());
System.out.println("The perimeter of the triangle is = "
+Triangle.getPerimeter());
System.out.println("The color in which it is filled is = "
+Triangle.getColor());
System.out.println("If it is filled or not = "
+Triangle.getfilled());
}
}
It's showing the error "The method getArea() is not defined in type
Second". Also, the same stuff is happening with the getPerimeter()
method. So, I had the question of how to solve the code and is it an
error related to subclass? The question is something like:
(The Triangle class) Design a class named Triangle that extends
GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with
default values
1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1,
side2, and
side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this
triangle.
■ A method named toString() that returns a string description for
the triangle. For the formula to compute the area of a triangle, see Programming Exercise 2.15. The toString() method is implemented as follows: return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.