0

This is my first time doing java codes, and I am confused on this. I supposed to compile 2 files, the first is Lingkaran.java which contains class lingkaran, and the second is MLingkaran.java which is the main file. The Lingkaran.java compiles just fine, but when I tried to compile the MLingkaran.java, these errors came out.

C:\Users\LENOVO PC>javac F:\SchoolSums-Praktikum\Semester4\PemrogramanBerbasisObjek\14.03.17\2\MLingkaran.java F:\SchoolSums-Praktikum\Semester4\PemrogramanBerbasisObjek\14.03.17\2\MLingkaran.java:11: error: cannot find symbol lingkaran l = new lingkaran(); ^ symbol: class lingkaran location: class mLingkaran F:\SchoolSums-Praktikum\Semester4\PemrogramanBerbasisObjek\14.03.17\2\MLingkaran.java:11: error: cannot find symbol lingkaran l = new lingkaran(); ^ symbol: class lingkaran location: class mLingkaran 2 errors

The following are the source code:

Lingkaran.java:

class lingkaran {
    private double jari;
    private double luas;
    private double keliling;
    private double phi;

    public lingkaran(){
        this.phi = 3.14;
        this.jari = 10;
        this.keliling =  this.jari*2*this.phi;
        this.luas = this.phi * this.jari * this.jari ;
    }

    public double getLuas() {
        return this.luas;
    }

    public double getKeliling() {
        return this.keliling;
    }
}

and the MLingkaran.java:

class mLingkaran {
    public static void main (String [] args) {

        lingkaran l = new lingkaran();

        System.out.println("keliling = " +l.getLuas());
        System.out.println("luas = " +l.getKeliling());
    }
}    
smttsp
  • 4,011
  • 3
  • 33
  • 62

1 Answers1

0

You need to import the other files to be able to use anything in them.

import <package>.Lingkaran;

If the file isn't in a package (it should be though), then you can leave the first part out.

Let's take the following project structure:

package1
    MLingkaran
    package2
        Lingkaran

Then in MLingkaran you need to use

import package1.package2.Lingkaran;
Bálint
  • 4,009
  • 2
  • 16
  • 27
  • ummm.... i don't think that i should use package... my lecturer did it without "package" thing.. so what if i don't use it? is there a way without using the "package"? – Ronaldo Kristianto Mar 13 '17 at 21:48
  • @Ronaldo As I told you, there's a way, but then you have to okit the part before and including the dot. Your lecturer will probably get to packages eventually. – Bálint Mar 13 '17 at 22:02
  • I tried to compile those again, now in eclipse IDE. it works, but I have to put those two in a project.. it seems that i eclipse automatically put both files in a same project.. don't know how though.. I'll try to repair the codes again, thanks for your advice :3 – Ronaldo Kristianto Mar 13 '17 at 22:23