[go: up one dir, main page]

0% found this document useful (0 votes)
15 views14 pages

Java Big Ans-1

The document provides Java code for an Employee Management System and an Array Rearrangement program, along with explanations of Object-Oriented Programming, Java source file structure, access specifiers, and the Java Virtual Machine. It also includes examples of Java programs for checking prime numbers, generating Fibonacci series, and defining packages and 2D arrays. Additionally, it covers various Java concepts such as constructors, data types, and JavaDoc comments.

Uploaded by

balajithiru454
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)
15 views14 pages

Java Big Ans-1

The document provides Java code for an Employee Management System and an Array Rearrangement program, along with explanations of Object-Oriented Programming, Java source file structure, access specifiers, and the Java Virtual Machine. It also includes examples of Java programs for checking prime numbers, generating Fibonacci series, and defining packages and 2D arrays. Additionally, it covers various Java concepts such as constructors, data types, and JavaDoc comments.

Uploaded by

balajithiru454
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/ 14

Employee management system:

import java.util.Scanner;

class Employee {
int empId;
String name;
String department;
double salary;

// Constructor to initialize employee details


public Employee(int empId, String name, String department,
double salary) {
this.empId = empId;
this.name = name;
this.department = department;
this.salary = salary;
}

// Display employee details


public void displayDetails() {
System.out.println("Employee ID: " + empId);
System.out.println("Name: " + name);
System.out.println("Department: " + department);
System.out.println("Salary: $" + salary);
System.out.println("------------------------------");
}
}

public class EmployeeManagementSystem {


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

// Input the number of employees


System.out.print("Enter the number of employees: ");
int numEmployees = scanner.nextInt();
scanner.nextLine(); // Consume the newline

// Array to store employees


Employee[] employees = new Employee[numEmployees];

// Input employee details


for (int i = 0; i < numEmployees; i++) {
System.out.println("\nEnter details for Employee " + (i + 1) +
":");
System.out.print("Enter Employee ID: ");
int empId = scanner.nextInt();
scanner.nextLine(); // Consume the newline

System.out.print("Enter Name: ");


String name = scanner.nextLine();

System.out.print("Enter Department: ");


String department = scanner.nextLine();

System.out.print("Enter Salary: ");


double salary = scanner.nextDouble();

// Create an Employee object using the constructor


employees[i] = new Employee(empId, name, department,
salary);
}

// Display all employee details


System.out.println("\nEmployee Details:");
for (Employee emp : employees) {
emp.displayDetails();
}

scanner.close();
}
}

Rearrange array:
import java.util.Arrays;

public class ArrayRearrangement {


public static void rearrangeArray(int[] arr) {
// Sort the array
Arrays.sort(arr);

// Create a temporary array to store the rearranged result


int[] result = new int[arr.length];

int left = 0; // Pointer for minimum element


int right = arr.length - 1; // Pointer for maximum element
int index = 0;

// Fill result array alternately with max and min


while (left <= right) {
if (index % 2 == 0) {
result[index++] = arr[right--]; // Add maximum element
} else {
result[index++] = arr[left++]; // Add minimum element
}
}
System.out.println("Rearranged Array: " +
Arrays.toString(result));
}

public static void main(String[] args) {


int[] arr = {1, 3, 2, 9, 5, 8, 7, 4};
System.out.println("Original Array: " + Arrays.toString(arr))
rearrangeArray(arr);
}

}
Two marks:
1. Define Object-Oriented Programming (OOP).

Object-Oriented Programming (OOP) is a programming paradigm


based on the concept of objects, which contain both data
(attributes) and methods (functions). OOP focuses on organizing
code into reusable structures, which makes the program more
modular, maintainable, and scalable. Key concepts include
Encapsulation, Inheritance, Polymorphism, and Abstraction.

2. Describe the Structure of a Java Source File.

A Java source file typically includes the following components:


Package Declaration (optional): Defines the package in which the
class is located.
Import Statements (optional): Imports other classes and packages
used by the program.
Class Declaration: Defines the main class, typically with a public
keyword.
Main Method: The entry point of the program, typically defined as
public static void main(String[] args).
Other Methods/Variables: Additional methods and variables can be
defined within the class.

3. Name Any Two Java Access Specifiers.

1. Public: Makes the class, method, or variable accessible


from any other class.
2. Private: Restricts access to the class, method, or variable to
within the same class only.

3. What is the Role of the Java Virtual Machine (JVM)?

The Java Virtual Machine (JVM) is responsible for interpreting


compiled Java bytecode and running Java applications. It provides
platform independence by allowing Java programs to run on any
system that has a compatible JVM installed, and it manages
memory, garbage collection, and execution.

4. What is the Function of the Main Method in a Java Program?

The main method serves as the entry point of any Java application. It
is executed when the program starts, and it contains the logic to
launch the program. The signature of the main method is typically
public static void main(String[] args).

5. Differentiate Between Static and Non-Static Members in


Java.

Static Members: These belong to the class rather than instances of


the class. They can be accessed without creating an object of the
class.
Example: public static int count;
Non-Static Members: These belong to the instances of the class.
They require an object to access.
Example: public int age;

6. Enlist the Various Data Types in Java.

Java has two types of data types:

1. Primitive Data Types: byte, short, int, long, float, double,


char, boolean.
2. Reference Data Types: Arrays, Objects, Strings.

7. Give Any Four Examples of JavaDoc Comments.


1. /** This method adds two numbers. */
2. /** This class represents a student. */
3. /** @param x the first number */
4. /** @return the result of addition */

8. Mention the Various Properties of Constructors in Java.


Name: The constructor’s name must be the same as the class
name.
No Return Type: Constructors do not have a return type, not even
void.
Invoked Automatically: Constructors are called when an object is
created.
Can Be Overloaded: You can have multiple constructors with
different parameter lists.

9. How a Java Program is Compiled and Executed?


1. Compilation: The Java source code (.java file) is compiled
using the javac compiler, converting it into bytecode (.class
file).
2. Execution: The bytecode is executed by the Java Virtual
Machine (JVM) using the java command.

10. Write a Java Program to Check Whether a Given Number is


Prime or Not.

Import java.util.Scanner;

Public class PrimeCheck {


Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
Int num = scanner.nextInt();
Boolean isPrime = true;

For (int i = 2; i <= num / 2; i++) {


If (num % i == 0) {
isPrime = false;
break;
}
}

If (isPrime && num > 1) {


System.out.println(num + “ is a prime number.”);
} else {
System.out.println(num + “ is not a prime number.”);
}
}
}
11. Write a Java Program to Generate Fibonacci Series.

Public class FibonacciSeries {


Public static void main(String[] args) {
Int n = 10, first = 0, second = 1;
System.out.print(“Fibonacci Series: “);

For (int i = 1; i <= n; ++i) {


System.out.print(first + “ “);
Int next = first + second;
First = second;
Second = next;
}
}
}
12. Identify the Various Components of a Package in Java.
A package in Java consists of:
1. Classes: The core building blocks that define objects and
methods.
2. Interfaces: Used to define a contract of methods without
implementation.
3. Sub-packages: Nested packages that help organize related
classes.
4. Access Modifiers: Used to control the visibility of classes,
methods, and variables within the package.

13. What is a Package in Java?

A package is a namespace that organizes classes and interfaces


logically. It helps to avoid name conflicts and makes it easier to
maintain and use classes. Packages can be built-in (like java.util) or
user-defined.

14. How Will You Define a 2D Array in Java?

A 2D array is defined in Java as an array of arrays:

Int[][] matrix = new int[3][3]; // 3x3 2D array


Alternatively, it can be initialized directly:

Int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

You might also like