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

JAVA Assignment

The document contains 3 sets of programming assignments with sample code solutions in Java. Set 1 includes programs to calculate the area and perimeter of a rectangle, and to reverse the elements of an array. Set 2 includes programs to display the system date and time in various formats, and a menu-driven program to perform matrix addition, multiplication, and transpose. Set 3 includes programs to sort country names in descending order, perform diagonal operations on a 2D array, and display the multiplication tables from 1 to 15.

Uploaded by

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

JAVA Assignment

The document contains 3 sets of programming assignments with sample code solutions in Java. Set 1 includes programs to calculate the area and perimeter of a rectangle, and to reverse the elements of an array. Set 2 includes programs to display the system date and time in various formats, and a menu-driven program to perform matrix addition, multiplication, and transpose. Set 3 includes programs to sort country names in descending order, perform diagonal operations on a 2D array, and display the multiplication tables from 1 to 15.

Uploaded by

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

Assignment No: Class: Batch No: Roll No: Date:

Title: Java Tools and IDE, Simple java Programs Time:

Set
1)Write a program to calculate perimeter and area of rectangle.
(hint : area = length * breadth , perimeter=2*(length+breadth)
Ans=>
import java.util.Scanner;
public class Rectangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Length of Rectangle : ");
int length = sc.nextInt();
System.out.println("Enter breadth of Rectangle : ");
int breadth = sc.nextInt();
int area = length * breadth;
System.out.println("Area of Reactangle : " + area);
int Perimeter = 2 * (length + breadth);
System.out.println("Perimeter of Reactangle : " + Perimeter);
sc.close();
}

}
Output:-

2)Write a program to accept the array element and display in reverse


order.
Ans=>
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array :");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter Elements in Array");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Array elements:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\nArray elements in Reverse Order :");
for (int i = n - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
sc.close();
}
}
Output:-

SET B
1)Write a java program to display the system date and time in various
formats shown below:
Ans=>
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatter {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String Str = sdf.format(date);
System.out.println("Current date is: " + Str);
sdf = new SimpleDateFormat("MM-dd-yyyy");
Str = sdf.format(date);
System.out.println("Current date is: " + Str);
sdf = new SimpleDateFormat("EEEE MMMM dd yyyy");
Str = sdf.format(date);
System.out.println("Current date is: " + Str);
sdf = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");
Str = sdf.format(date);
System.out.println("Current date and time is: " + Str);
sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");
Str = sdf.format(date);
System.out.println("Current date and time is: " + Str);
sdf = new SimpleDateFormat("hh:mm:ss");
Str = sdf.format(date);
System.out.println("Current time is: " + Str);
sdf = new SimpleDateFormat("w");
Str = sdf.format(date);
System.out.println("Current week of year is: " + Str);
sdf = new SimpleDateFormat("W");
Str = sdf.format(date);
System.out.println("Current week of the month is: " + Str);
sdf = new SimpleDateFormat("D");
Str = sdf.format(date);
System.out.println("Current day of the year: " + Str);
}
}
Output:-

2)Write a menu driven program to perform the following operations on


multidimensional array ie matrix :
i. Addition
ii. Multiplication
iii. Transpose of any matrix.
iv. Exit
Ans=>
import java.util.Scanner;
public class MenuMatrix {
// to calculate Addition of matrix
public void addition() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of row and column: ");
int r = sc.nextInt();
int c = sc.nextInt();
int[][] m1 = new int[r][c];
int[][] m2 = new int[r][c];
System.out.println("\nEnter values of matrix M1:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.printf("Enter element at index %d and %d :", i, j);
m1[i][j] = sc.nextInt();
}
}
System.out.println("\nEnter values of matrix M2:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.printf("Enter element at index %d and %d :", i, j);
m2[i][j] = sc.nextInt();
}
}
System.out.println("\nSum of M1 and M2:");
int[][] sum = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
sum[i][j] = m1[i][j] + m2[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
sc.close();
}// Addition
// To Multiplicate Matrices
public void multiplication() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of row and column: ");
int r = sc.nextInt();
int c = sc.nextInt();
int[][] m1 = new int[r][c];
int[][] m2 = new int[r][c];
System.out.println("\nEnter values of matrix M1:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.printf("Enter element at index %d and %d :", i, j);
m1[i][j] = sc.nextInt();
}
}
System.out.println("\nEnter values of matrix M2:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.printf("Enter element at index %d and %d :", i, j);
m2[i][j] = sc.nextInt();
}
}
System.out.println("\nMultiplication of M1 and M2:\n");
int[][] mul = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
for (int k = 0; k < c; k++) {
mul[i][j] = mul[i][j] + m1[i][k] * m2[k][j];
}}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(mul[i][j] + " ");
}
System.out.println();
}
sc.close();
}// Multiplication
// Transpose of Matrix
public void transpose() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of row and column: ");
int r = sc.nextInt();
int c = sc.nextInt();
int[][] m = new int[r][c];
System.out.println("Enter values of Matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.printf("Enter element at index %d and %d :", i, j);
m[i][j] = sc.nextInt();
}}
System.out.println("\nEntered Matrix...\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
System.out.println("\nTranspose Matrix...\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(m[j][i] + " ");
}
System.out.println();
}
sc.close();
}// Transpose
public static void main(String[] args) {
MenuMatrix m = new MenuMatrix();
Scanner sc = new Scanner(System.in);
System.out.println("1.Addition of Matrix.");
System.out.println("2.Multiplication of Matrix.");
System.out.println("3.Transpose of Matrix.");
System.out.println("4.Exit\n");
System.out.println("Enter Your Choice : ");
int choice = sc.nextInt();
switch (choice) {
case 1:
m.addition();
break;
case 2:
m.multiplication();
break;
case 3:
m.transpose();
break;
}
sc.close();
}
}
Output:-(1)

Output:-(2)

Output:-(3)
Set C
1) Write a program to accept n names of country and display them in
descending
order.
Ans=>
import java.util.*;
public class countrysort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter total no of Countries: ");
int n = sc.nextInt();
String country[] = new String[n];
sc.nextLine();
for (int i = 0; i < n; i++) {
System.out.println("Enter country no :" + (i + 1));
country[i] = sc.nextLine();
}
System.out.println("Country names without Sorting:");
for (String ele : country) {
System.out.println("" + ele);
}
String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (country[i].compareTo(country[j]) < 0) {
temp = country[i];
country[i] = country[j];
country[j] = temp;
}}}
System.out.println("Countries in Descending order:");
for (String ele : country) {
System.out.println("" + ele);
}
sc.close();
}}
Output:-

2) Write a menu driven program to perform the following operations on 2D


array:
i. Sum of diagonal elements
ii. Sum of upper diagonal elements
iii. Sum of lower diagonal elements iv. Exit
Ans=> import java.util.*;
public class DiagonalMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of row and column: ");
int n = sc.nextInt();
int sum = 0;
int[][] m = new int[n][n];
System.out.println("\nEnter values of matrix :\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m[i][j] = sc.nextInt();
}
}
System.out.println("\nEntered Matrix...\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
do {
System.out.println("\nMatrix Operations");
System.out.println("1.Sum of Diagonal Elements.");
System.out.println("2.Sum of Upper Diagonal Elements.");
System.out.println("3.Sum of Lower Diagonal Elements.");
System.out.println("4.Exit");
System.out.println("Enter Your Choice :");
int ch = sc.nextInt();
switch (ch) {
case 1:
sum = 0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
if (i==j) {
sum = sum+m[i][j];
}
}
}
System.out.println("\nAddition of Diagonal Elements:"+sum);
break;

case 2:
sum = 0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
if (j>i) {
sum = sum+m[i][j];
}
}
}
System.out.println("\nAddition of Upper Diagonal Elements:"+sum);
break;
case 3:
sum = 0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
if (i>j) {
sum = sum+m[i][j];
}
}
}
System.out.println("\nAddition of Lower Diagonal Elements:"+sum);
break;
case 4:
System.exit(0);
default:
break;
}
} while (true);

}
}

Output:-
3) Write a program to display the 1 to 15 tables.
(1*1=1 2 * 1 = 2 ........ 15 * 1 = 15
1*2=2 2*2=4 15 * 2 = 30
1*3=3 2*3=6 15 * 3 = 45
1 * 10 = 10 2 * 10 = 20 15 * 10 = 150)
Ans=>
public class table {
public static void main(String[] args) {
for (int i = 1; i <= 15; i++) {
System.out.println("Table of "+(i)+" number:");
for (int j = 1; j <= 10; j++) {
System.out.println(i + "*" + j + "=" +i*j);
}
System.out.println("----------------------");
}}}
Output:-

You might also like