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

java hemraj

The document contains a series of Java programming assignments that cover various topics such as using javap to view class methods, calculating the area and perimeter of a rectangle, performing matrix operations, and implementing a menu-driven program for different mathematical calculations. Each assignment includes sample code, expected outputs, and hints for implementation. The assignments are designed to enhance understanding of Java programming concepts and practical applications.

Uploaded by

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

java hemraj

The document contains a series of Java programming assignments that cover various topics such as using javap to view class methods, calculating the area and perimeter of a rectangle, performing matrix operations, and implementing a menu-driven program for different mathematical calculations. Each assignment includes sample code, expected outputs, and hints for implementation. The assignments are designed to enhance understanding of Java programming concepts and practical applications.

Uploaded by

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

ASSIGNMENT NO.

1: Java Tools and IDE,Simple Java Programs


SET A
(1) Using javap, view the methods of the following classes from the lang
package: java.lang.Object, java.lang.String and java.util.Scanner. and
also Compile sample program 8. Type the following command and
view the bytecodes. javap -c MyClass PROGRAM:
package as1setaa;
public class MyClass
{
int num;
publicyClass()
{
num=0;
}
public MyClass(int num);
{
this.num=num;
}
public static void main(String[] args)
{
MyClass ml = new MyClass();
if(args.length>0)
{
int n = Integer.parseInt(args[0]);
MyClass m2 = new MyClass(n);
System.out.println(m1.num);
System.out.println(m2.num);
}
else
System.out.println("Insufficient arguments");
}
}
OUTPUT:
(2) Write a program to calculate perimeter and area of rectangle.(hint :
area = length * breadth , perimeter=2*(length+breadth)) PROGRAM:
package recatangle; import
java.util.Scanner;
public class demo {

public static void main(String[] args) {

System.out.println("first java project...rectangle");


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:

(3) Write a menu driven program to perform the following operations


i. Calculate the volume of cylinder. (hint : Volume: π × r² × h) ii. Find the
factorial of given number. iii. Check the number is Armstrong or not. iv.
Exit
PROGRAM:
package asi1seta;
import java.util.Scanner;
public class numericalsmenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("\n1.Volume of Cylinder. \n2.Factorial of Number. \
n3.Armstrong Number. \n4.Exit");
System.out.println("Enter Your Choice : ");
int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.println("Enter Radius:");
Float r = sc.nextFloat();
System.out.println("Enter Height:");
Float h = sc.nextFloat();
double Volume = Math.PI * r * r * h;
System.out.printf("Volume of Cylinder: %f" ,Volume);
break;

case 2:
System.out.println("Enter Number for Finding Factorial : ");
int num = sc.nextInt();
long fact = 1;
for (int i = 1; i <= num; ++i) {
fact = fact * i;
}
System.out.printf("Factorial of %d = %d\n", num, fact);
break;

case 3:
System.out.println("Enter Number for Finding Armstrong Number : ");
int n = sc.nextInt();
int leng = 0;
int t1 = n;

while (t1 != 0) {
t1 = t1 / 10;
leng = leng + 1;
}

int t2 = n;
int arm = 0;
int rem;

while (t2 != 0) {
int mult = 1;
rem = t2 % 10;
for (int i = 1; i <= leng; i++) {
mult = mult * rem;
}
arm = arm + mult;
t2 = t2 / 10;
}
if (arm == n) {
System.out.println("The given number is armstrong..!");
} else {
System.out.println("The given number is not armstrong..!");
}
break;
case 4:
System.exit(0);
default:
break;
}
sc.close();

}
}
OUTPUT:
(4) Write a program to accept the array element and display in
reverseorder.
PROGRAM:
package ass1;
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: Current date is : 31/08/2021
Current date is : 08-31-2021
Current date is : Tuesday August 31 2021
Current date and time is : Fri August 31 15:25:59 IST 2021
Current date and time is : 31/08/21 15:25:59 PM+0530
Current time is : 15:25:59
Current week of year is : 35
Current week of month : 5 Current
day of the year is : 243
PROGRAM:
package as1setb1;
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("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) Define a class MyNumber having one private int data member. Write a
default constructor to initialize it to 0 and another constructor to
initialize it to a value (Use this). Write methods isNegative, isPositive,
isZero, isOdd, isEven. Create an object in main. Use command line
arguments to pass a value to the object (Hint : convert string
argument to integer) and perform the above tests. Provide javadoc
comments for all constructors and methods and generate the html
help file.
PROGRAM:
Package as1setbb
public class MyNumber {
private int x;
public MyNumber(){
x=0;
}
public MyNumber(int x){
this.x=x;
}
public boolean isNegative(){
if(x<0) return
true; else
return false;
}
public boolean isPositive(){
if(x>0) return
true;
else return false;
}
public boolean isZero(){
if(x==0) return
true;
else return false;
}
public boolean isOdd()
{ if(x%2!=0) return true;
else return false;
}
public boolean isEven()
{ if(x%2==0) return true;
else return false;
}
public static void main(String [] args) throws ArrayIndexOutOfBoundsException
{
int x=Integer.parseInt(args[0]); MyNumber
m=new MyNumber(x);
if(m.isNegative())
System.out.println("Number is Negative");
if(m.isPositive())
System.out.println("Number is Positive"); if(m.isEven())
System.out.println("Number is Even");
if(m.isOdd())
System.out.println("Number is Odd"); if(m.isZero())
System.out.println("Number is Zero");
}
}
OUTPUT:

(3) 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
PROGRAM:
import java.util.Scanner;
public class MatrixOperations {
/**
* Displays the given matrix.
*
* @param matrix The matrix to be displayed.
*/
public static void displayMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
/**
* Adds two matrices.
*
* @param matrix1 The first matrix.
* @param matrix2 The second matrix.
* @return The result of adding matrix1 and matrix2.
*/
public static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {
int rows = matrix1.length;
int cols = matrix1[0].length;
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return result;
}
/**
* Multiplies two matrices.
*
* @param matrix1 The first matrix.
* @param matrix2 The second matrix.
* @return The result of multiplying matrix1 and matrix2.
*/
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;
if (cols1 != rows2) {
throw new IllegalArgumentException("Matrix multiplication is not possible:
column count of the first matrix must be equal to row count of the second
matrix.");
}
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
/**
* Computes the transpose of a matrix.
*
* @param matrix The matrix to be transposed.
* @return The transpose of the matrix.
*/
public static int[][] transposeMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] transposed = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = matrix[i][j];
}
}
return transposed;
}
/**
* Reads a matrix from the user.
*
* @param rows The number of rows.
* @param cols The number of columns.
* @return The matrix entered by the user.
*/
public static int[][] readMatrix(int rows, int cols) {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[rows][cols];
System.out.println("Enter the matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = scanner.nextInt();
}
}
return matrix;
}
/**
* Main method to drive the menu-based matrix operations.
*
* @param args Command-line arguments (not used).
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Add Matrices");
System.out.println("2. Multiply Matrices");
System.out.println("3. Transpose Matrix");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the number of rows for both matrices: ");
int rowsAdd = scanner.nextInt();
System.out.print("Enter the number of columns for both matrices: ");
int colsAdd = scanner.nextInt();
System.out.println("Enter elements for the first matrix:");
int[][] matrix1Add = readMatrix(rowsAdd, colsAdd);
System.out.println("Enter elements for the second matrix:");
int[][] matrix2Add = readMatrix(rowsAdd, colsAdd);
int[][] sumMatrix = addMatrices(matrix1Add, matrix2Add);
System.out.println("Sum of the matrices:");
displayMatrix(sumMatrix);
break;
case 2:
System.out.print("Enter the number of rows and columns for the first
matrix: ");
int rows1 = scanner.nextInt();
int cols1 = scanner.nextInt();
System.out.print("Enter the number of rows and columns for the second
matrix: ");
int rows2 = scanner.nextInt();
int cols2 = scanner.nextInt();
if (cols1 != rows2) {
System.out.println("Matrix multiplication is not possible.");
break;
}
System.out.println("Enter elements for the first matrix:");
int[][] matrix1Mul = readMatrix(rows1, cols1);
System.out.println("Enter elements for the second matrix:");
int[][] matrix2Mul = readMatrix(rows2, cols2);
int[][] productMatrix = multiplyMatrices(matrix1Mul, matrix2Mul);
System.out.println("Product of the matrices:");
displayMatrix(productMatrix);
break;
case 3:
System.out.print("Enter the number of rows for the matrix: ");
int rowsTrans = scanner.nextInt();
System.out.print("Enter the number of columns for the matrix: ");
int colsTrans = scanner.nextInt();
System.out.println("Enter elements for the matrix:");
int[][] matrixTrans = readMatrix(rowsTrans, colsTrans);
int[][] transposedMatrix = transposeMatrix(matrixTrans);
System.out.println("Transposed matrix:");
displayMatrix(transposedMatrix);
break;
case 4:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please choose a valid option.");
break;
}
}
}
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy