+ 4
Java program not running
Hey guys, I have made a code but its output is " An error occured" . please check in my profile. i have only 1 code saved.. TIA
3 Answers
+ 7
// You didn't put the ending brace of the Employee class. You forgot to put a semi-colon at the end of the print statement. You didn't use 'f' for floats. This is the working code:
class Employee
{
int id;
String name;
float salary;
void insert(int i, String n, float s)
{
id=i;
name=n;
salary=s;
}
void display()
{
System.out.println(id+" "+name+" "+salary);
}
}
public class Employee1
{
public static void main(String[] args)
{
Employee e1= new Employee();
Employee e2= new Employee();
Employee e3= new Employee();
e1.insert(101,"Akshit",20000f);
e2.insert(102,"Akshay",30000f);
e3.insert(103,"Deepak",40000f);
e1.display();
e2.display();
e3.display();
}
}
+ 5
Now your code is ok. Remove the extra class Employee1 and execute your code and also mention the termination ";" in void display (){
System.out.println(id+" "+name+" "+salary);
}
class Employee
{
int id;
String name;
float salary;
void insert(int i, String n, float s)
{
id=i;
name=n;
salary=s;
}
void display()
{
System.out.println(id+" "+name+" "+salary);
}
public static void main(String[] args)
{
Employee e1= new Employee();
Employee e2= new Employee();
Employee e3= new Employee();
e1.insert(101,"Akshit",20000);
e2.insert(102,"Akshay",30000);
e3.insert(103,"Deepak",40000);
e1.display();
e2.display();
e3.display();
}
}
+ 3
Your errors are corrected and I fixed up some of the formatting for you, as well as went by some of the common standard conventions. It's better to use encapsulation to protect your member variables, so utilize setters/getters to access the now private members. Unless you're messing with counters in a loop, it's better to use properly named variables instead of vague variables that don't describe. Sure, it'll work without using common practices, but it's worth mentioning since you may be working in a professional or team situation one day. Also, I created a constructor to handle your initial objects.
https://code.sololearn.com/cU60Le5WWxf9/#java
class Employee
{
private int id;
private String name;
private float salary;
public Employee(int id, String name, float salary){
setID(id);
setName(name);
setSalary(salary);
}
public void setID(int id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setSalary(float salary){
this.salary = salary;
}
public int getID(){
return this.id;
}
public String getName(){
return this.name;
}
public float getSalary(){
return this.salary;
}
void display()
{
System.out.print("[ID: " + getID() + "] ");
System.out.print(getName() + "'s salary is ");
System.out.println(getSalary());
}
}
public class Employee1
{
public static void main(String[] args)
{
Employee employee1 = new Employee(101, "Akshit", 20000);
Employee employee2 = new Employee(102, "Akshay", 30000);
Employee employee3 = new Employee(103, "Deepak", 40000);
employee1.display();
employee2.display();
employee3.display();
}
}