0% found this document useful (0 votes)
30 views13 pages

assignment1

Uploaded by

sonyabhongale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views13 pages

assignment1

Uploaded by

sonyabhongale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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;
// Uncomment this if you're using a package

public class MyClass {


int num;

// Default constructor
public MyClass() {
num = 0;
}

// Parameterized constructor
public MyClass(int num) {
this.num = num;
}

public static void main(String[] args) {


MyClass ml = new MyClass(); // Creating an instance with default constructor

if (args.length > 0) {
int n = Integer.parseInt(args[0]); // Parsing command-line argument to integer
MyClass m2 = new MyClass(n); // Creating an instance with parameterized
constructor

System.out.println(ml.num); // Print the default instance's num


System.out.println(m2.num); // Print the parameterized instance's 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:
//ass-1-set-a-2 //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:
//ass-1-set-a-3 //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 reverse
order. PROGRAM:
//ass-1-set-a-4
//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 ReverseOrder :");

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:
//ass-1-set-b-1
//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:
//ass-1-set-b-2
//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");
}

}
/*Command: javac MyNumber.java
Java MyNumber -9 */

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:
//ass-1-set-b-3
//package as1setb3;

import java.util.Scanner;

public class menumatrix{


// tocalculate 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.printl n("\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("\nEntervalues 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.printl n("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("\nEntervalues 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.printl n("\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()
Scannersc=newScanner(Syst
em.in);
System.out.printl n("Ente
size of row and column: ");

int r = sc.nextInt();
int
c = sc.nextInt();
int[][] m = new
int[r][c];
System.out.printl n("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.printl n("\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.printl n("\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.printl n("\n1.Addition of Matrix. \n2.Multiplication of Matrix.\n3.Transpose of
Matrix.\n4.Exit");
System.out.println("E nter 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:

You might also like