Assign 1 Java
Assign 1 Java
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;
/*
Output:
*/
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.*;
d) Write a program to accept the array element and display in reverse order
import java.io.*;
import java.util.*;
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));
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;
}
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 ");
}
}
import java.io.*;
import java.util.*;
class Mul_mat {
int n, i, j, op = 1, m, p, q, k;
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 ");
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++) {
}
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;
}
}
}
}