0% found this document useful (0 votes)
25 views2 pages

OOPJ Practical No-6

This document provides a practical guide on creating a class in Java, including defining a class, instantiating objects, and accessing class members. It includes a sample program that defines an 'Employee' class with methods to input employee details, display them, and show the employee's grade based on salary. The document outlines the syntax and structure necessary for implementing these concepts in Java.

Uploaded by

omraul2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

OOPJ Practical No-6

This document provides a practical guide on creating a class in Java, including defining a class, instantiating objects, and accessing class members. It includes a sample program that defines an 'Employee' class with methods to input employee details, display them, and show the employee's grade based on salary. The document outlines the syntax and structure necessary for implementing these concepts in Java.

Uploaded by

omraul2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No: 6

Title: Program to create class with members and methods.


Theory:
Defining a class
The basic syntax to define class is:
class ClassName
{
[fields declaration]
[method declaration]
}
Here,
ClassName is any valid identifier.

Creating Object
 Creating an object is referred to as instantiating an object.
 Object in java is created using new operator.
 The new operator creates an object of specified class and returns reference to that object.
Syntax:
ClassName objName;
objName = new ClassName();
or
ClassName objName = new ClassName();

Accessing class members


 To access data members and methods use object of a class and dot (.) operator.
Syntax:
objName.variableName=value;
objName.methodName(parameter-list);
Here,
objName:- Name of object
variableName:- Name of instance variable that we wish to access
methodsName:- Name of instance method that we wish to call.
parameter-list:- Comma separated list of actual values that must match in type and number with
the parameter list of methodName defined in a class.
Programs:
1. Create a class employee with data member’s empid, empname, designation and salary. Write
methods getemployee() to take user input, showgrade() to display grade of employee based on salary,
showemployee() to display details of employee.
import java.util.*;
public class Employee
{
int empId;
String empName, designation;
float salary;
void getEmployee()
{
Scanner s = new Scanner(System.in);

System.out.println("Enter employee Name: ");


empName=s.nextLine();

System.out.println("Enter employee Id: ");


empId=s.nextInt();

System.out.println("Enter Designation: ");


designation=s.next();

System.out.println("Enter employee Salary: ");


salary=s.nextFloat();
}
void showEmployee()
{
System.out.println("Employee ID: "+empId);
System.out.println("Employee Name: "+empName);
System.out.println("Designation: "+designation);
System.out.println("Salary: "+salary);
}
void showGrade()
{
if(salary<25000)
System.out.println("Grade C");
else if(salary<50000)
System.out.println("Grade B");
else
System.out.println("Grade A");
}
public static void main(String args[])
{
Employee e=new Employee();
e.getEmployee();
e.showEmployee();
e.showGrade();
}
}

You might also like