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

JAVA

The document contains multiple Java programs demonstrating various functionalities such as generating multiplication tables, calculating sums of odd numbers, handling command line arguments, and managing employee and student data. Each program includes user input, processing logic, and output display, showcasing skills in array manipulation, object-oriented programming, and basic I/O operations. The outputs of each program are also provided as examples of expected results.

Uploaded by

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

JAVA

The document contains multiple Java programs demonstrating various functionalities such as generating multiplication tables, calculating sums of odd numbers, handling command line arguments, and managing employee and student data. Each program includes user input, processing logic, and output display, showcasing skills in array manipulation, object-oriented programming, and basic I/O operations. The outputs of each program are also provided as examples of expected results.

Uploaded by

kashishj405
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

2A

import java.util.*;

public class table{

public static void main(String args[]){

Scanner console = new Scanner(System.in);

System.out.print("Enter any number: ");

int num = console.nextInt();

System.out.println("The table of " + num + " is...");

for (int i = 1; i <= 10; i++){

System.out.println(num + " X " + i + " = " + (num *

i));

Output:

Enter any number: 2

The table of 2 is...

2X1=2

2X2=4

2X3=6

2X4=8

2 X 5 = 10

2 X 6 = 12

2 X 7 = 14

2 X 8 = 16

2 X 9 = 18

2 X 10 = 20

2B

import java.util.*;

public class SumOdd{


public static void main(String args[]){

int sum = 0;

System.out.print("The sum of all two-digit odd numbers is ");

for (int i = 10; i < 100; i++){

if (i % 2 != 0){

sum += i;

System.out.println(sum);

Output:

The sum of all two-digit odd numbers is 2475

3A

import java.util.*;

public class commandLine{

public static void main(String args[]){

String rollnumber = args[0];

String name = args[1];

String branch = args[2];

String year = args[3];

System.out.println("Roll number : " + rollnumber);

System.out.println("Name: " + name);

System.out.println("Branch: " + branch);

System.out.println("Year: " + year);

Output:

java commandLine 64 Kashish Comps SE

Roll number : 64
Name: Kashish

Branch: Comps

Year: SE

3b

import java.util.*;

public class MinMax{

public static void main(String args[]){

int max = Integer.parseInt(args[0]);

int min = Integer.parseInt(args[0]);

for (int i = 1; i < 3; i++){

if (Integer.parseInt(args[i]) > max){

max = Integer.parseInt(args[i]);

if (Integer.parseInt(args[i]) < min){

min = Integer.parseInt(args[i]) ;

System.out.println("The maximum number is " + max + " and the

minimum number is " + min);

Output:

java MinMax 10 40 20

The maximum number is 40 and the minimum number is 10

4a

import java.util.*;

public class employeeDetails{

public static void main(String args[]){

employeeDetails e1 = new employeeDetails();


e1.read();

void read(){

Scanner console = new Scanner(System.in);

System.out.print("Enter the employee code: ");

int empCode = console.nextInt();

System.out.print("Enter the name: ");

String name = console.next();

System.out.print("Enter the year of joining: ");

int yearOfJoining = console.nextInt();

System.out.print("Enter the salary: ");

int salary = console.nextInt();

display(empCode, name, yearOfJoining, salary);

void display(int num1, String s, int num2, int num3){

System.out.println("Employee Code: " + num1);

System.out.println("Name: " + s);

System.out.println("Year of joining: " + num2);

System.out.println("Salary: " + num3);

Output:

Enter the employee code: 101

Enter the name: Kashish

Enter the year of joining: 2000

Enter the salary: 100000

4b

import java.util.*;

public class max{


public static void main(String args[]){

max1 m = new max1();

System.out.println("The maximum number is " + m.read());

class max1{

int read(){

Scanner console = new Scanner(System.in);

System.out.print("Enter number 1: ");

int num1 = console.nextInt();

System.out.print("Enter number 2: ");

int num2 = console.nextInt();

System.out.print("Enter number 3: ");

int num3 = console.nextInt();

int maximum = maxOfThree(num1, num2, num3);

return maximum;

int maxOfThree(int num1, int num2, int num3){

int max = num1;

max = ((num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 >

num3) ? num2 : num3));

return max;

Output:

Enter number 1: 10

Enter number 2: 42

Enter number 3: 34

The maximum number is 42

5a
import java.io.*;

public class OneD {

public static void main(String args[]) throws Exception{

OneD obj = new OneD();

int arr1[] = obj.readArr();

obj.calculation(arr1);

int[] readArr() throws Exception{

int arr[];

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.println("Enter the size of array: ");

int size = Integer.parseInt(br.readLine());

arr = new int[size];

System.out.println("Enter the array elements: ");

for (int i = 0; i < size; i++){

arr[i] = Integer.parseInt(br.readLine());

return arr;

void calculation(int arr[]){

int even = 0, odd = 0;

for (int i = 0; i < arr.length; i++){

if (arr[i] % 2 == 0){

even++;

else{

odd++;

System.out.println("The number of even elements is " + even +


" and the number of odd elements is " + odd);

Output:

Enter the size of array:

Enter the array elements:

10

11

12

13

14

The number of even elements is 3 and the number of odd elements is 2

5b

import java.io.*;

public class ArrayMarks {

public static void main(String[] args) throws Exception {

ArrayMarks obj = new ArrayMarks();

int arr1[] = obj.readArr();

obj.calculation(arr1);

int[] readArr() throws Exception{

int arr[];

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.println("Enter the number of students: ");

int size = Integer.parseInt(br.readLine());

arr = new int[size];

System.out.println("Enter the marks: ");


for (int i = 0; i < size; i++){

arr[i] = Integer.parseInt(br.readLine());

return arr;

void calculation(int arr[]){

int totalMarks = 0, highestMarks = 0;

for (int i = 0; i < arr.length; i++){

if (arr[i] > highestMarks){

highestMarks = arr[i];

totalMarks = totalMarks + arr[i];

System.out.println("The total marks of all the students are "

+ totalMarks );

System.out.println("The highest marks are " + highestMarks);

Output:

Enter the number of students:

Enter the marks:

98

100

85

42

73

The total marks of all the students are 398

6a
import java.io.*;

public class TwoD {

public static void main(String args[]) throws Exception{

TwoD obj = new TwoD();

int mat[][] = obj.readArray();

obj.transpose(mat);

int[][] readArray() throws Exception{

int mat1[][];

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("Enter the number of rows: ");

int rows = Integer.parseInt(br.readLine());

System.out.print("Enter the number of columns: ");

int cols = Integer.parseInt(br.readLine());

mat1 = new int[rows][cols];

System.out.println("Enter the array elements: ");

for (int i = 0; i < rows; i++){

for (int j = 0; j < cols; j++){

mat1[i][j] = Integer.parseInt(br.readLine());

return mat1;

void transpose(int arr[][]){

System.out.println("Array before transpose is ");

for (int i = 0; i < arr.length; i++){

for (int j = 0; j < arr[0].length; j++){

System.out.print(arr[i][j] + " ");

System.out.println();
}

System.out.println("Array after transpose is ");

for (int i = 0; i < arr[0].length; i++){

for (int j = 0; j < arr.length; j++){

System.out.print(arr[j][i] + " ");

System.out.println();

Output:

Enter the number of rows: 3

Enter the number of columns: 2

Enter the array elements:

Array before transpose is

12

34

56

Array after transpose is

135

246

6b

import java.io.*;

public class diagonal {


public static void main(String[] args) throws Exception {

diagonal obj = new diagonal();

obj.check();

void check() throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("Enter the number of rows: ");

int rows = Integer.parseInt(br.readLine());

System.out.print("Enter the number of columns: ");

int cols = Integer.parseInt(br.readLine());

int arr1[][] = new int[rows][cols];

if (rows == cols){

readAndCalculateArr(arr1);

else{

System.out.println("Sum of diagonal elements cannot be

found of non-square matrices");

void readAndCalculateArr(int arr[][]) throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

int sum = 0;

System.out.println("Enter the array elements...");

for (int i = 0; i < arr.length; i++){

for (int j = 0; j < arr.length; j++){

arr[i][j] = Integer.parseInt(br.readLine());

System.out.println("Matrix is...");
for (int i = 0; i < arr.length; i++){

for (int j = 0; j < arr.length; j++){

System.out.print(arr[i][j] + " ");

if (i == j){

sum += arr[i][j];

System.out.println();

System.out.println("The sum of diagonal elements is " + sum);

Output:

Enter the number of rows: 3

Enter the number of columns: 3

Enter the array elements...

10

20

30

40

50

60

70

80

90

Matrix is...

10 20 30

40 50 60

70 80 90

The sum of diagonal elements is 150


7a

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class ArrayOfObjects1 {

public static void main(String[] args) throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("Enter the number of employees: ");

int num = Integer.parseInt(br.readLine());

EmpDetails e[] = new EmpDetails[num];

for (int i = 0; i < num; i++){

e[i] = new EmpDetails(); //declaring the object

System.out.println("Enter details of employee " + (i+1));

e[i].getData();

e[i].calculate();

System.out.println("\t\t\tEmployee Details");

System.out.println("\tID\t\tName\t\tNo.of hours\t\tWages");

for (int i = 0; i < num; i++){

e[i].display();

int max = e[0].wages;

int pos = 0;

for (int i = 0; i < num; i++){

if(e[i].wages > max){

max = e[i].wages;

pos = i;

System.out.println("Employee " + e[pos].name + " has the

highest salary " + max);


}

class EmpDetails{

int id = 0, hours = 0, wages = 0;

String name;

void getData() throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("Enter the id: ");

id = Integer.parseInt(br.readLine());

System.out.print("Enter the name: ");

name = br.readLine();

System.out.print("Enter the number of hours: ");

hours = Integer.parseInt(br.readLine());

void display(){

System.out.println("\t" + id + "\t\t" + name + "\t\t\t" +

hours + "\t\t" + wages);

void calculate(){

wages = hours * 100;

Output:

Enter the number of employees: 3

Enter details of employee 1

Enter the id: 101

Enter the name: Kashish

Enter the number of hours: 22

Enter details of employee 2

Enter the id: 102


Enter the name: Harsh

Enter the number of hours: 25

Enter details of employee 3

Enter the id: 103

Enter the name: Disha

Enter the number of hours: 11

Employee Details

ID Name No.of hours Wages

101 Kashish 22 2200

102 Harsh 25 2500

103 Disha 11 1100

Employee Harsh has the highest salary 2500

7b

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class StudentData {

public static void main(String[] args) throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("Enter the number of students: ");

int size = Integer.parseInt(br.readLine());

Student s[] = new Student[size];

int max = 0;

int pos = 0;

for (int i = 0; i < size; i++){

s[i] = new Student();

System.out.println("Enter the marks of student no." + (i

+ 1));

s[i].getData();

s[i].calculate();
if (s[i].total > max){

max = s[i].total;

pos = i;

for (int i = 0; i < size; i++){

System.out.println("The total marks obtained by student

no." + (i+1) + " are " + s[i].total);

System.out.println("The highest total marks are " + max + "

and they are obtained by student no." + (pos + 1));

class Student{

int phy, chem, math, total;

void getData() throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.print("Enter the marks of physics of student no. :

");

phy = Integer.parseInt(br.readLine());

System.out.print("Enter the marks of chemistry: ");

chem = Integer.parseInt(br.readLine());

System.out.print("Enter the marks of mathematics: ");

math = Integer.parseInt(br.readLine());

void calculate(){

total = phy + chem + math;

Output:
Enter the number of students: 2

Enter the marks of student no.1

Enter the marks of physics of student no. : 100

Enter the marks of chemistry: 90

Enter the marks of mathematics: 95

Enter the marks of student no.2

Enter the marks of physics of student no. : 100

Enter the marks of chemistry: 100

Enter the marks of mathematics: 100

The total marks obtained by student no.1 are 285

The total marks obtained by student no.2 are 300

The highest total marks are 300 and they are obtained by student no.

You might also like