Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again
Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again .
import java.io.*;
class Employee
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String f_name;
String l_name;
int sal;
Employee()
{
f_name="sarvajeet";
l_name="suman";
sal=120000;
}
public String getf_name()throws IOException
{
System.out.println("Enter First Name");
f_name=br.readLine();
//System.out.println(f_name);
return f_name;
}
public String getl_name()throws IOException
{
System.out.println("Enter Last Name");
l_name=br.readLine();
return l_name;
}
public int get_sal()throws IOException
{
System.out.println("Enter Salary");
sal=Integer.parseInt(br.readLine());
return sal;
}
public String setf_name(String fname)
{
f_name=fname;
return f_name;
}
public String setl_name(String lname)
{
l_name=lname;
return l_name;
}
public int set_sal(int salary)
{
sal=salary;
sal=(sal>0)?sal:0;
return sal;
}
public void display()
{
System.out.printf("\nFirst name\t"+f_name+"\nLast name\t"+l_name+"\nSalary\t"+sal+"\n");
}
public void inc_sal()
{
System.out.println("Salary after increment "+(sal+sal*0.1));
}
}
public class TestEmployee
{
public static void main(String [] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.getf_name();
emp1.getl_name();
emp1.get_sal();
emp2.setf_name("Jiya");
emp2.setl_name("Mehra");
emp2.set_sal(100000);
emp1.display();
emp2.display();
emp1.inc_sal();
emp2.inc_sal();
}
}
import java.io.*;
class Employee
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String f_name;
String l_name;
int sal;
Employee()
{
f_name="sarvajeet";
l_name="suman";
sal=120000;
}
public String getf_name()throws IOException
{
System.out.println("Enter First Name");
f_name=br.readLine();
//System.out.println(f_name);
return f_name;
}
public String getl_name()throws IOException
{
System.out.println("Enter Last Name");
l_name=br.readLine();
return l_name;
}
public int get_sal()throws IOException
{
System.out.println("Enter Salary");
sal=Integer.parseInt(br.readLine());
return sal;
}
public String setf_name(String fname)
{
f_name=fname;
return f_name;
}
public String setl_name(String lname)
{
l_name=lname;
return l_name;
}
public int set_sal(int salary)
{
sal=salary;
sal=(sal>0)?sal:0;
return sal;
}
public void display()
{
System.out.printf("\nFirst name\t"+f_name+"\nLast name\t"+l_name+"\nSalary\t"+sal+"\n");
}
public void inc_sal()
{
System.out.println("Salary after increment "+(sal+sal*0.1));
}
}
public class TestEmployee
{
public static void main(String [] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.getf_name();
emp1.getl_name();
emp1.get_sal();
emp2.setf_name("Jiya");
emp2.setl_name("Mehra");
emp2.set_sal(100000);
emp1.display();
emp2.display();
emp1.inc_sal();
emp2.inc_sal();
}
}
How to see all
ReplyDelete