0% found this document useful (0 votes)
11 views

Java P Code

The document contains Java code that defines a class called Employee with private fields for name, year of joining, salary, and address. The Employee class has a constructor to initialize these fields and a displayInfo method to output the employee details. The main method creates an Employee object by passing values to the constructor and calls displayInfo to print the information.

Uploaded by

Chandan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java P Code

The document contains Java code that defines a class called Employee with private fields for name, year of joining, salary, and address. The Employee class has a constructor to initialize these fields and a displayInfo method to output the employee details. The main method creates an Employee object by passing values to the constructor and calls displayInfo to print the information.

Uploaded by

Chandan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NO 4

import java.util.Scanner;

public class MatrixAddition {


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

// Input the dimensions of the matrices


System.out.print("Enter the number of rows: ");
int rows = input.nextInt();
System.out.print("Enter the number of columns: ");
int columns = input.nextInt();

// Initialize the matrices


int[][] matrix1 = new int[rows][columns];
int[][] matrix2 = new int[rows][columns];
int[][] sum = new int[rows][columns];

// Input elements of the first matrix


System.out.println("Enter elements of the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix1[i][j] = input.nextInt();
}
}

// Input elements of the second matrix


System.out.println("Enter elements of the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix2[i][j] = input.nextInt();
}
}

// Add the matrices


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result matrix


System.out.println("Resultant Matrix after addition:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}

input.close();
}
}
No 5
import java.util.Scanner;

public class DynamicArrayEquality {


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

// Input the size of the arrays


System.out.print("Enter the size of the arrays: ");
int size = input.nextInt();

// Declare two arrays of the same size


int[] array1 = new int[size];
int[] array2 = new int[size];

// Input elements for the first array


System.out.println("Enter elements for the first array:");
for (int i = 0; i < size; i++) {
array1[i] = input.nextInt();
}

// Input elements for the second array


System.out.println("Enter elements for the second array:");
for (int i = 0; i < size; i++) {
array2[i] = input.nextInt();
}

// Check if the arrays are equal


boolean areEqual = true;

if (array1.length != array2.length) {
areEqual = false;
} else {
for (int i = 0; i < size; i++) {
if (array1[i] != array2[i]) {
areEqual = false;
break;
}
}
}

if (areEqual) {
System.out.println("The arrays are equal.");
} else {
System.out.println("The arrays are not equal.");
}

input.close();
}
}
No 6
import java.util.Scanner;

public class Box {


// Instance variables
private double length;
private double width;
private double height;

// Constructor to initialize the instance variables


public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}

// Method to calculate the volume


public double calculateVolume() {
return length * width * height;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

// Input the dimensions of the box


System.out.print("Enter the length of the box: ");
double length = input.nextDouble();
System.out.print("Enter the width of the box: ");
double width = input.nextDouble();
System.out.print("Enter the height of the box: ");
double height = input.nextDouble();

// Create a Box instance with user-specified dimensions


Box myBox = new Box(length, width, height);

// Calculate the volume


double volume = myBox.calculateVolume();

// Print the volume


System.out.println("The volume of the box is: " + volume);

input.close();
}
}
NO 7
import java.util.Scanner;

public class Box {


// Instance variables
private double length;
private double width;
private double height;

// Constructor to initialize the instance variables


public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}

// Method to calculate the volume and return it


public double calculateVolume() {
return length * width * height;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

// Input the dimensions of the box


System.out.print("Enter the length of the box: ");
double length = input.nextDouble();
System.out.print("Enter the width of the box: ");
double width = input.nextDouble();
System.out.print("Enter the height of the box: ");
double height = input.nextDouble();

// Create a Box instance with user-specified dimensions


Box myBox = new Box(length, width, height);

// Calculate the volume using the method


double volume = myBox.calculateVolume();

// Print the volume


System.out.println("The volume of the box is: " + volume);

input.close();
}
}
No 8
import java.util.Scanner;

public class Box {


private double length;
private double width;
private double height;

public Box(double length, double width, double height) {


this.length = length;
this.width = width;
this.height = height;
}
public void calculateVolume() {
double volume = length * width * height;
System.out.println("The volume of the box is: " + volume);
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Enter the length of the box: ");


double length = input.nextDouble();
System.out.print("Enter the width of the box: ");
double width = input.nextDouble();
System.out.print("Enter the height of the box: ");
double height = input.nextDouble();

Box myBox = new Box(length, width, height);

myBox.calculateVolume();

input.close();
}
}

NO 9
import java.util.Scanner;

public class BoxVolumeCalculator {


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

System.out.print("Enter the length of the box: ");


double length = input.nextDouble();
System.out.print("Enter the width of the box: ");
double width = input.nextDouble();
System.out.print("Enter the height of the box: ");
double height = input.nextDouble();

double volume = length * width * height;


System.out.println("The volume of the box is: " + volume);
input.close();
}
}
No 10
import java.util.Scanner;

public class PrimeNumberChecker {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueChecking = true;

while (continueChecking) {
System.out.print("Enter a number (or enter 0 to exit): ");
int number = input.nextInt();

if (number == 0) {
continueChecking = false;
} else {
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}

input.close();
}

// Function to check if a number is prime


public static boolean isPrime(int n) {
if (n <= 1) {
return false; // 0 and 1 are not prime
}

for (int i = 2; i <= Math.sqrt(n); i++) {


if (n % i == 0) {
return false; // If n is divisible by any number from 2 to sqrt(n), it's not prime
}
}

return true; // If no divisors are found, the number is prime


}
}
NO 11
import java.util.Scanner;

public class SeasonDetermination {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueChecking = true;

while (continueChecking) {
System.out.print("Enter a month (1-12) or 0 to exit: ");
int month = input.nextInt();

if (month == 0) {
continueChecking = false;
} else {
String season = determineSeason(month);
System.out.println("The season for month " + month + " is " + season);
}
}

input.close();
}

// Function to determine the season based on the month


public static String determineSeason(int month) {
String season;

switch (month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Invalid Month";
break;
}

return season;
}
}
NO 12
class Employee {
private String name;
private int yearOfJoining;
private double salary;
private String address;

public Employee(String name, int yearOfJoining, double salary, String address) {


this.name = name;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
this.address = address;
}

public void displayInfo() {


System.out.println("Name: " + name);
System.out.println("Year of Joining: " + yearOfJoining);
System.out.println("Salary: $" + salary);
System.out.println("Address: " + address);
System.out.println();
}

public static void main(String[] args) {


Employee employee1 = new Employee("Rajesh Kumar", 2015, 60000, "456, Lakeview Avenue,
Mumbai");
Employee employee2 = new Employee("Priya Sharma", 2018, 55000, "123, Park Street,
Bangalore");
Employee employee3 = new Employee("Amit Patel", 2019, 62000, "789, Green Road, New
Delhi");

System.out.println("Employee Information:");
System.out.println("-----------------");

employee1.displayInfo();
employee2.displayInfo();
employee3.displayInfo();
}
}
NO 13
import java.util.Scanner;

public class Box {


private double length;
private double width;
private double height;
private double volume;

// Constructor with three parameters


public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
this.volume = length * width * height;
}

// Get the calculated volume


public double getVolume() {
return volume;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Enter the length of the box: ");


double length = input.nextDouble();
System.out.print("Enter the width of the box: ");
double width = input.nextDouble();
System.out.print("Enter the height of the box: ");
double height = input.nextDouble();

Box box = new Box(length, width, height);

System.out.println("Volume of the box: " + box.getVolume());

input.close();
}
}

You might also like