1

Package and class hierarchy

Hello, I am new to programming and would love to get some help. I am try to make a simple program in eclipse to work out few basic equations. I have made 2 packages, "Equations", and "Workspace".

This is the class "quadraticEquation" in equations package.

package Equations;
import java.util.*;

public class quadraticEquation {

    //ax^2 + bx + c = 0

    double a,b,c;
    Scanner input= new Scanner(System.in);

    void calculateQuadEq() {
        System.out.println("Enter a");
        a = input.nextDouble(); 
        System.out.println("Enter b");
        b = input.nextDouble();
        System.out.println("Enter c");
        c = input.nextDouble();

        double z = Math.pow(b, 2) - (4*a*c);
        if(z<0) {
            System.out.println("This equation has no real roots");
        } else if(z==0) {
            double m = -b/(2*a);
            System.out.println("There are 2 equal roots to this equation, " + m + " and " + m);
        } else if(z>0) {
            double m = (-b + (Math.pow(b, 2) - 4*a*c))/2*a;
            double n = (-b - (Math.pow(b, 2) - 4 *a*c))/2*a;

            System.out.println("There are 2 real roots to this equation, " + m + " and " + n);
        }
    }
}

I am successfully able to run the variable calculateQuadEq in the same package (have tried) but when it comes to a different package, I am not able to. I tired importing the class file like this.

Variable not available

What can I do to achieve this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Rockboy987
  • 131
  • 1
  • 6
  • 3
    `calculateQuadEq` is a method. It is not a variable. – Yohannes Gebremariam Jun 25 '18 at 17:18
  • It's not available because the variable is package-private. https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html Use a modifier like public. – Compass Jun 25 '18 at 17:19
  • 2
    your method is package protected, use public void calculateQuadEq – Peter1982 Jun 25 '18 at 17:22
  • 1
    Also use Java naming conventions. Classes (and other types like interfaces, enum) should start with uppercase. This helps us to distinguish `fields` and `methods(..)` from `Types` and constructors like `Type(..)` while reading the code. – Pshemo Jun 25 '18 at 17:27
  • Thanks guys! Making it public worked for me. Also sorry for the repeat question :( – Rockboy987 Jun 25 '18 at 20:42

0 Answers0