Questions:
Answers:
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();
}
}
}