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

Assign 1 Java

Uploaded by

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

Assign 1 Java

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java assignment 1

Set A)
b) Write a program to calculate perimeter and area of rectangle. (hint : area = length * breadth
, perimeter=2*(length+breadth))

import java.io.*;
import java.util.*;

class rect
{
public static void main(String[] argsv)
{
int length, breadth;

Scanner sc = new Scanner (System.in);

System.out.println("Enter length : ");


length = sc.nextInt();

System.out.println("Enter breadth : ");


breadth = sc.nextInt();

System.out.println("Area of rectangle : "+length*breadth);


System.out.println("perimeter of rectangle : "+2*(length +
breadth));
}
}

/*
Output:

ty28@pc58:~/TY28/JAVA/ASSIGNMENT_01/SET_A$ java rect


Enter length :
4
Enter breadth :
3
Area of rectangle : 12
perimeter of rectangle : 14

*/
c) 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

import java.io.*;
import java.util.*;

public class menu


{
Scanner sc=new Scanner(System.in);
int r,h,num,fact=1;
double a;
void dispFact()
{
System.out.println("Enter number to find factorial : ");
num=sc.nextInt();
for(int i=num;i>0;--i)
{
fact=fact*i;
}
System.out.println(" Factorial of number : "+fact);
}
void calcVolume()
{
System.out.println("Enter Radius of cylinder : ");
r=sc.nextInt();
System.out.println("Enter Height of cylinder : ");
h=sc.nextInt();
a=3.1415*r*r*h;
System.out.println(" Area of cylinder : "+a);
}
void check()
{
System.out.println("Enter (3 digit) number to check armstrong no :
");
num=sc.nextInt();
int og=num,sum=0,rem;
while (num>0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if (og==sum)
System.out.println(og+" is armstrong number .");
else
System.out.println(og+" is not armstrong number .");
}
public static void main(String[] args)
{
Scanner sc1=new Scanner(System.in);
int choice,op=1;
while(op>=1 && op<=4)
{
System.out.println("1 : Calculate volume of cylinder ");
System.out.println("2 : Find factorial");
System.out.println("3 : check number is Armstrong ");
System.out.println("4 : Exit ");
System.out.println("Enter your choice : ");
choice=sc1.nextInt();
menu m1=new menu();
switch (choice)
{
case 1: m1.calcVolume();
break;
case 2: m1.dispFact();
break;
case 3: m1.check();
break;
case 4:System.exit(0);
break;
default:System.out.println("Enter a valid option...
");
}
}
}
}

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

import java.io.*;
import java.util.*;

public class array


{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int size;
System.out.println("Enter the size of array : ");
size=sc.nextInt();
int[] arr=new int[size];
System.out.println("Enter the array elements : ");
for (int i=0;i<size;++i)
{
System.out.println("Enter array element at index : "+i);
arr[i]=sc.nextInt();
}
System.out.println("The array elements in reversed order : ");
for (int i=size-1;i>=0;--i)
System.out.println(": "+arr[i]);
}
}

Set B)

a) Write a java program to display the system date and time in various formats shown below:
Lab Assignment public class MyClass { int num; public MyClass() { num=0; } public
MyClass(int num) { this.num = num; } public static void main(String[] args) { MyClass m1 =
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”); } } 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 Note: Use java.util.Date and java.text.SimpleDateFormat class

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

class display
{
public static void main(String[] argsv)
{
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");
System.out.println("Current date is : "+df.format(date));

SimpleDateFormat df1 = new SimpleDateFormat("EEEEEE MMMM dd


yyyy");
System.out.println("Current date is : "+df1.format(date));

SimpleDateFormat df2 = new SimpleDateFormat("E MMMM dd HH : mm :


ss a z");
System.out.println("Current date is : "+df2.format(date));

SimpleDateFormat df3 = new SimpleDateFormat("hh:mm:ss");


System.out.println("Current date is : "+df3.format(date));

SimpleDateFormat df4 = new SimpleDateFormat("w");


System.out.println("Current date is : "+df4.format(date));

SimpleDateFormat df5 = new SimpleDateFormat("w");


System.out.println("Current date is : "+df5.format(date));

SimpleDateFormat df6 = new SimpleDateFormat("d");


System.out.println("Current date is : "+df6.format(date));
}
}

b) 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.

import java.io.*;
import java.util.*;

class MyNumber
{
private int x;

MyNumber(){
x = 0;
}

MyNumber(int x){
this.x = x;
}

int isNegative()
{
if (x < 0)
return 1;
else
return 0;
}
int isPositive()
{
if (x > 0)
return 1;
else
return 0;
}

int isZero()
{
if (x == 0)
return 1;
else
return 0;
}

int isEven()
{
if (x%2 == 0)
return 1;
else
return 0;
}

int isOdd()
{
if (x%2 == 1)
return 1;
else
return 0;
}

public static void main( String[] args)


{
int x;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number : ");
x = sc.nextInt();

MyNumber m = new MyNumber(x);

int k1 =m.isNegative();
if (k1 == 1)
System.out.println("\nThe NUmber is NEgative ");

int k2 =m.isPositive();
if (k2 == 1)
System.out.println("\nThe NUmber is Positive ");

int k3 =m.isZero();
if (k3 == 1)
System.out.println("\nThe NUmber is Zero ");

int k4 =m.isEven();
if (k4 == 1)
System.out.println("\nThe NUmber is Even ");

int k5 =m.isOdd();
if (k5 == 1)
System.out.println("\nThe NUmber is Odd ");
}
}

c) 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

import java.io.*;
import java.util.*;

class Mul_mat {

public static void main(String[] args) {

int n, i, j, op = 1, m, p, q, k;

Scanner sc = new Scanner(System.in);

System.out.println(" \nEnter rows for matrix 1: ");


m = sc.nextInt();

System.out.println(" \n Enter cols for matrix 1: ");


n = sc.nextInt();

int[][] a = new int[m][n];

// initialise array a with 0


for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i][j] = 0;

// accepting matrix from user


System.out.println(" \n Enter matrix 1 : ");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i][j] = sc.nextInt();

// display 2D array matrix


System.out.println("1 Matrix is : \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
System.out.print(" " + a[i][j] + " ");
}
System.out.println(" ");
}

System.out.println("\n Enter rows for matrix 2: ");


p = sc.nextInt();

System.out.println(" \nEnter cols for matrix 2: ");


q = sc.nextInt();

int[][] b = new int[p][q];


int[][] c = new int[m][q];

// initialise array b with 0

for (i = 0; i < p; i++)


for (j = 0; j < q; j++)
b[i][j] = 0;

// acceptin matrix from user


System.out.println("\n Enter matrix 2: ");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
b[i][j] = sc.nextInt();

// display 2D array matrix


System.out.println("\n Matrix 2 is : \n ");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
System.out.print(" " + b[i][j] + " ");
}
System.out.println(" ");
}

System.out.println(" _____");
while (op > 0 && op <= 4) {
System.out.println(" 1: add ");
System.out.println(" 2: mul");
System.out.println(" 3: transpose ");
System.out.println(" 4:exit\n ");

System.out.print(" Enter option: ");


op = sc.nextInt();

switch (op)

case 1:

System.out.println(" _____");

if (m == p && n == q) {
System.out.println(" \nAdd is :\n ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
// System.out.println(" Add is : \n");
System.out.print(" " + c[i][j] + " ");
}
System.out.println(" ");
}
} else
System.out.println(" \nAdd not possible: ");

System.out.println(" _____");
System.out.println(" \n ");
break;
// System.out.println(" \n " );

case 2:

System.out.println(" _____");
if (n == p) {
System.out.println(" \nmul is is :\n ");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {

c[i][j] += a[i][k] * b[k][j];

}
System.out.print(" " + c[i][j] + " ");
}
System.out.println(" ");
}
}
else
System.out.println(" \n Multiplication is not
possible:\n");
break;

case 3:

break;
case 4:
System.exit(0);
break;

}
}
}
}

You might also like