Saya tidak tahu apa yang salah dengan kode di bawah ini ketika saya dengan cermat mengikuti modul yang diberikan kepada saya. tolong, ajari saya mengapa saya mendapatkan kesalahan itu. Saya benar-benar seorang pemula di Jawa sementara profesor saya tidak membalas saya.

  public class Member{
  String name;
  int age;
  String number;
  String address;
  int salary;

  public void printSalary(){
    System.out.println(salary);
  }

}

class Employee extends Member{
  String specialization;
}

class Manager extends Member{
  String department;
}

class Ans{
  public static void main(String[] args){
    Employee e = new Employee();
    e.name = "Lado";
    e.age = 21;
    e.number = "0919467670";
    e.address = "Tunasan";
    e.salary = 10000;
    e.specialization = "Computer Coding";

    Manager m = new Manager();
    m.name = "Lala";
    m.age = 23;
    m.number = "09216t767";
    m.address = "Tunasan";
    m.salary = 0;
    m.department = "CITS Office";
  }
  
1
Alain Tristan Lado 12 Mei 2021, 07:03

2 jawaban

Jawaban Terbaik
// Remove "public"
 class Member{
  String name;
  int age;
  String number;
  String address;
  int salary;

  public void printSalary(){
    System.out.println(salary);
  }

}

class Employee extends Member{
  String specialization;
}

class Manager extends Member{
  String department;
}

// Add "public", don't forget that the name of this java file must be "Ans.java"
public class Ans{
  public static void main(String[] args){
    Employee e = new Employee();
    e.name = "Lado";
    e.age = 21;
    e.number = "0919467670";
    e.address = "Tunasan";
    e.salary = 10000;
    e.specialization = "Computer Coding";

    Manager m = new Manager();
    m.name = "Lala";
    m.age = 23;
    m.number = "09216t767";
    m.address = "Tunasan";
    m.salary = 0;
    m.department = "CITS Office";
  }
0
Victor Polo De Gyves Montero 12 Mei 2021, 04:10

Anda harus meletakkan metode main di kelas publik atau metode itu tidak akan diambil. Karena Anda hanya dapat memiliki satu kelas publik per file (unit kompilasi), Anda harus membuat Member tidak publik atau memindahkannya ke file lain:

class Member{
    String name;
    int age;
    String number;
    String address;
    int salary;

    public void printSalary(){
        System.out.println(salary);
    }

}

class Employee extends Member{
    String specialization;
}

class Manager extends Member{
    String department;
}

public class Ans{
    public static void main(String[] args){
        Employee e = new Employee();
        e.name = "Lado";
        e.age = 21;
        e.number = "0919467670";
        e.address = "Tunasan";
        e.salary = 10000;
        e.specialization = "Computer Coding";

        Manager m = new Manager();
        m.name = "Lala";
        m.age = 23;
        m.number = "09216t767";
        m.address = "Tunasan";
        m.salary = 0;
        m.department = "CITS Office";
   }

}

Juga nama file sumber .java harus selalu cocok dengan nama kelas publik, jadi ini harus dalam Ans.java.

0
geanakuch 12 Mei 2021, 05:45