-1

I have written a piece of code which is giving me some errors please look at it and debug it:

package measurement;
import java.util.*;

class Cylinder {
    static double sa(float h,float r) { return 2*3.14*r*h; }
    static double vol(float h,float r) { return 3.14*r*r*h; }
}

class Sphere {
    static double sa(float r) { return 4*3.14*r*r; }
    static double vol(float r) { return 4/3*3.14*r*r*r; }
}

class Cube {
    static public double sa(float a) { return 6*a*a; }
    static public double vol(float a) { return a*a*a; }
}

public class Measure {
    public static void main(String arg[]) {}
    public static void cubeSA(float a) { Cube.sa(a); }
    public static void cubeVol(float a) { Cube.vol(a); }
}

Above piece of code is package I have created but in my other program it is giving error

Class measure cant be access

my main(other) program is:
import measurement.*;
import java.util.*;
public class PackExample {

    public static void main (String args[]) {
        Scanner sc=new Scanner (System.in);
        Measure m=new Measure();
        Cylinder cy=new Cylinder();
        Sphere s=new Sphere();
        while (true)
        {   System.out.println("Enter:\n1.CYLINDER\n2.CUBE3.SPHERE");
            int x=sc.nextInt();
            if(x==1)
            {
                System.out.println("Enter height of cylinder:");
                float a=sc.nextfloat();
                System.out.println("Enter radius of base of cylinder:");
                float b=sc.nextfloat();
                System.out.println("Surface area is "+cy.Cylinder.sa(a,b)+" unit sq.");
                System.out.println("Surface area is "+cy.Cylinder.vol(a,b)+" unit cube");
            }
            else if(x==2)
            {
                System.out.println("Enter its Edge length:");
                float a=sc.nextFloat();
                System.out.println("Surface area is "+m.CubeSA(a)+" unit sq.");
                System.out.println("Surface area is "+m.CubeVol(a)+" unit cube");
            }
            else if(x==3)
            {  
                System.out.println("Enter Radius of Sphere:");
                float r=sc.nextFloat();
                System.out.println("Surface area is "+s.Sphere.sa(r)+" unit sq.");
                System.out.println("Surface area is "+s.Sphere.vol(r)+" unit cube");
            }
            else System.exit(0);
        }

    }
}

Although there must be other errors too but

Error:cannot access Measure

This is compiler error is need to debug first.

0xC0d3
  • 77
  • 1
  • 10
  • What IDE are you using ? And what is the name of the first class ? – Joachim Huet Nov 07 '17 at 07:31
  • Can you please provide the exact output? – Robert Kock Nov 07 '17 at 07:31
  • @RobertKock there is not output having compile time error. – 0xC0d3 Nov 07 '17 at 07:33
  • You can take a look to this [thread](https://www.quora.com/Can-we-keep-more-than-one-class-in-a-single-java-file) and try fixing it ? – Joachim Huet Nov 07 '17 at 07:34
  • @JoachimHuet using 'Geany' as my IDE and name of first class ( in which i've created my package) is Measure.java and the other one PackExample.java – 0xC0d3 Nov 07 '17 at 07:44
  • And what is your file hierarchy ? In the package measurements you only have one class ? And where is PackExample ? – Joachim Huet Nov 07 '17 at 07:52
  • And have you compiled your package ? I don't know how much Geany is doing for you. If you are a beginner in coding you should use an ide offering advanced functionalities like netbeans or eclipse for example :) – Joachim Huet Nov 07 '17 at 07:55
  • @JoachimHuet i have folder JavaCode in which my both programs are saved and package measurement itself in the JavaCode. – 0xC0d3 Nov 07 '17 at 07:57
  • @JoachimHuet i complied my package on cmd(terminal) using _javac -d . Measure.java_ – 0xC0d3 Nov 07 '17 at 08:00
  • Oh that's why I guess, you should compile the whole package in order for your main class to find it, maybe similar to [here](https://stackoverflow.com/questions/19382593/how-to-compile-java-package-structures-using-javac) ? – Joachim Huet Nov 07 '17 at 08:02
  • Try to do from JavaCode folder : `javac ./measurement/*.java` and `javac PackExample.java` So the sources will then be in the same folder. And then `java PackExample` – Joachim Huet Nov 07 '17 at 08:10
  • @HimanshuBhatt Is my answer okay ? Have fun coding :) – Joachim Huet Nov 07 '17 at 08:39
  • @JoachimHuet tried but showing error javac: file not found: .\measurement\*.java – 0xC0d3 Nov 07 '17 at 08:42
  • @HimanshuBhatt The same as before or with something else than measure ? I wrote an answer, try following my indications below. – Joachim Huet Nov 07 '17 at 08:43
  • @HimanshuBhatt use the name of your file, instead, but from the upper folder and don't forget the `/`. You are on Windows or linux ? – Joachim Huet Nov 07 '17 at 08:49
  • @Joachim on Windows – 0xC0d3 Nov 07 '17 at 08:58
  • Please report error messages **accurately.** It does **not** say 'Class measure cant be access'. – user207421 Feb 28 '18 at 01:22

1 Answers1

0

So to have a clear answer, first of all you should have your compiled sources from the package and from the main at the same location.

Do javac ./measurement/*.java and javac PackExample.java from your JavaCode folder. And then java PackExample to run your main.

But you will then be confronted to another problem that I tried to explain to you at first, a java file can only have one class public, so you won't be able to call the others one. (See that)

So you should create another file for each class :

measurement/Measure.java:

public class Measure{
    public static void cubeSA(float a)
    { 
        Cube.sa(a);
    }

    public static void cubeVol(float a){
        Cube.vol(a);
    }
}

measurement/Cylinder.java:

public class Cylinder {
    static double sa(float h,float r)
    {
        return 2*3.14*r*h;
    }

    static double vol(float h,float r)
    {
        return 3.14*r*r*h;
    }
}

Etc, etc ... (You don't need to have a main function in each class of your package)

Hope this helps,

And just for better coding I will explain another thing to you Interfaces and Abstract classes.

As you have different shapes all using the same methods you can use an abstract class describing a shape for example :

public abstract class Shape {
    abstract double sa(float h, float r);
    abstract double vol(float h, float r);
}

And then you extend it for all shapes :

public class Cylinder extends Shape {
    private final float PI = 3.14159;

    @Override
    double sa(float h, float r)
    {
        return 2*PI*r*h;
    }

    @Override
    double vol(float h, float r)
    {
        return PI*r*r*h;
    }
}

And you should also look a bit on static methods, in your case you don't necessarily need them. You can give the parameters to your object directly via the constructor.

public class Cylinder extends Shape {
    private final float PI = 3.14159;
    private final float height;
    private final float radius;

    public Cylinder(float h, float r) {
        height = h;
        radius = r;
    }

    @Override
    double sa()
    {
        return 2*PI*radius*height;
    }

    @Override
    double vol()
    {
        return PI*radius*radius*height;
    }
}

With abstract class :

public abstract class Shape {
    abstract double sa();
    abstract double vol();
} 
Joachim Huet
  • 422
  • 3
  • 10