Questions:

  1. What is the purpose of static methods and variables?
  2. Write a program to define a class Student having the attribute regNo, stdName, branch and CGPA. The class has two methods to accept and display the student details. Read the details of 20 students using an array of Student class object.
  3. Write a program to create a class Area with members length and breadth. The class has two methods: setDim() initialises length and breadth and getArea () returns the area of the rectangle. Create a driver class to create the objects of class Area and test the functionalities of the Area class.
  4. Write a java program to define a class Employee with data members empId, empName and salary. The class has default and parameterized constructor to initialize the members of the class and a method display() to show the employee details. Create another driver class to read and display the details of 5 employees using an array of Employee class object.

Answers:

  1. Static variables are used to share common data across all instances of a class since they belong to the class instead of a specific object; this helps save memory because the variable is stored in one place and not copied for every object. Static methods similarly belong to the class rather than any object, allowing them to be called without creating an instance. These methods can only directly use static variables and other static methods, making them ideal for operations or utilities that work on general class data rather than on individual objects.
import java.util.Scanner;

class Student {
    int regNo;
    String stdName;
    String branch;
    double CGPA;

    void input() 
    {
        System.out.print("Enter Registration Number: ");
        regNo = sc.nextInt();
        System.out.print("Enter Student Name: ");
        stdName = sc.nextLine();
        System.out.print("Enter Branch: ");
        branch = sc.nextLine();
        System.out.print("Enter CGPA: ");
        CGPA = sc.nextDouble();
    }
    
    void display() 
    {
        System.out.println("RegNo: " + regNo + ", Name: " + stdName + ", Branch: " + branch + ", CGPA: " + CGPA);
    }
}

public class StudentDemo 
{
    public static void main(String[] args) 
    {
        Student[] students = new Student[20];

        for (int i = 0; i < students.length; i++) {
            System.out.println("\\nEnter details of student " + (i + 1));
            students[i] = new Student();
            students[i].input();
        }

        System.out.println("\\n--- Student Details ---");
        for (int i = 0; i < students.length; i++) 
        {
            System.out.print("Student " + (i + 1) + ": ");
            students[i].display();
        }
    }
}

import java.util.Scanner;

class Area
{
			double length, bredth;
			
			void setDim()
			{
						Scanner sc = new Scanner(System.in);
						System.out.print("Enter length: ");
						length = sc.nextDouble();
						System.out.print("Enter bredth: ");
						bredth = sc.nextDouble();
			}
			
			double getArea()
			{
						return length * bredth;
			}
}

class Driver
{
			public static void main(String[] arg)
			{
					Area a = new Area();
					a.setDim();
					System.out.println("Area: " + a.getArea());
			}
}
import java.util.Scanner;

class Employee {
    int empId;
    String empName;
    double salary;

    Employee() 
    {
        empId = 0;
        empName = " ";
        salary = 0.0;
    }
    
    Employee(int id, String name, double sal) 
    {
        empId = id;
        empName = name;
        salary = sal;
    }

    void display() 
    {
        System.out.println("ID: " + empId + ", Name: " + empName + ", Salary: " + salary);
    }
}

public class Driver 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);

        Employee[] employees = new Employee[5];

        for (int i = 0; i < employees.length; i++) {
            System.out.println("\\nEnter details of Employee " + (i + 1));
            System.out.print("Enter Employee ID: ");
            int id = sc.nextInt();
            System.out.print("Enter Employee Name: ");
            String name = sc.nextLine();
            System.out.print("Enter Salary: ");
            double sal = sc.nextDouble();

            employees[i] = new Employee(id, name, sal);
        }

        System.out.println("\\n--- Employee Details ---");
        for (int i = 0; i < employees.length; i++) {
            System.out.print("Employee " + (i + 1) + ": ");
            employees[i].display();
        }

    }
}