简体   繁体   中英

How do you call a method from another class which has a switch statement ready to call two methods in the same class?

I don't know how to phrase that question without it sounding confusing but it's essentially this:

TL;DR: I have been trying to call circleChoice() located in the subclass Circle from a switch statement in inputProcess() located in the main class Shapes . I don't know how, but i get a java.lang.StackOverflowError on the Console. I might have screwed up the logic. I could fix this easily if it wasn't a school project that required the usage of subclasses.

    package shapes;
import java.lang.Math.*;
import java.util.Scanner;

public class Shapes {
    //CONSTRUCTORS

        //choosing
static Scanner sc = new Scanner(System.in);
        //inputs
protected String radDiameter;   
protected int choice;
protected double a;
protected double b;   //b1
protected double d;   //b2
protected double c;
protected double h;
protected double radius;
        //outputs
protected double perimeter;
protected double area;
    //CONSTUCTORS   
    
public class Circle extends Shapes{  //error

    public void circleChoice() {
        System.out.println("Pick your poison: ");
        System.out.println("[P] Perimeter");
        System.out.println("[A] Area");
        radDiameter = sc.next();
        this.radDiameter.toUpperCase();
        switch (radDiameter) {
        case "P":
            System.out.println("Enter Radius for Perimeter: ");
            CirclePerimeter();
            break;
        case "A":
            System.out.println("Enter Radius for Area: ");
            CircleArea();
            break;
        }       
    }
    
    public double CirclePerimeter() {
        super.radius = sc.nextDouble();
        this.perimeter = 2 * Math.PI * super.radius;
        System.out.println(perimeter);
        return perimeter;
    }
    
    public double CircleArea() {
        super.radius = sc.nextInt();
        this.area = Math.PI * (super.radius*super.radius);
        System.out.println(this.area);
        return area;
    }
    
}   
public class Triangle extends Shapes{
    public void triangleChoice() {
        switch (choice) {
        case 1:
            System.out.println("Enter Radius for Perimeter: ");
            TrianglePerimeter();
            break;
        case 2:
            System.out.println("Enter Radius for Area: ");
            TriangleArea();
            break;
        }
        
    }

    public double TrianglePerimeter() {
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        
        perimeter = a*b*c;
        System.out.println(this.perimeter);
        return perimeter;
    }
    public double TriangleArea() {
        b = sc.nextInt();
        h = sc.nextInt();
        area = 12 * (b*h);
        System.out.println(this.area);
        return area;
    }
}


private Circle obj1 = new Circle();       //error
private Triangle obj2 = new Triangle();


    public void inputProcess() {

         Shapes s = new Shapes();
         
            System.out.println("Geometric Shapes");
            System.out.println("Choose Shape to continue:");
            System.out.println("[1] Circle");
            System.out.println("[2] Triangle");
            System.out.println("[0] Exit");
            System.out.println("Enter your choice: ");
            s.choice = sc.nextInt();
            
        switch (s.choice) {
            case 1 :
            obj1.circleChoice();
                break;
                
            
            case 2 :
            obj2.triangleChoice();  
                break;
                
            case 0:
                System.exit(s.choice);
                break;
            default :
                System.out.println("Invalid output! Please run again!");
                
                break;
            }

    }


    
    public static void main(String[] args) {    
        
        Shapes p = new Shapes();
        p.inputProcess();
    }

}

How the program should work: I have a class that contains the method inputProcess which has a switch statement that should call either method circleChoice() from class Circle , or method triangleChoice from class Triangle depending on the user's input. Let's focus on Circle to make it a bit easier.

circleChoice() has a Scanner input from radDiameter and a switch statement that will call either circlePerimeter() or circleArea() . The previous two methods have formulas that will solve a circle's perimeter or area given by another Scanner input ( radius ) in each method, which works just fine.

After that, I created the object private Circle obj1 = new Circle(); just outside every subclass, but still inside the main class Shapes in order to call the methods from Circle in inputProcess() .

inputProcess() has a switch statement that should call either obj1.circleChoice(); or obj2.triangleChoice(); depending on the value of s.choice .

Problem: When I try to run it, it just shows a java.lang.StackOverflowError on console which is highlighting line 24 and 93. It's obviously a logical error, since Eclipse didn't show any warning highlights.

Normally, I would be able to complete this task without using any additional classes, but this is for a school project that requires me to use them.

Answer is simple. Because Circle is a subclass to Shape, when you instantiate Shape, and then Circle as it's attribute, then Circle instantiates Circle as it's attribute (because Circle IS Shape), and then Circle instantiates Circle as it's attribute and so on... So you have the infinite recursion.

You really should refactor your code, SuperClass should not aggregate SubClasses. And they should be also should not be inner classes of their SuperClass. Then it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM