assignment1
assignment1
// package as1setaa;
// Uncomment this if you're using a package
// Default constructor
public MyClass() {
num = 0;
}
// Parameterized constructor
public MyClass(int num) {
this.num = num;
}
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
sc.close();
}
}
OUTPUT:
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;
}
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;
System.out.println("Array elements:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
import java.text.SimpleDateFormat;
import java.util.Date;
(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;
}
}
/*Command: javac MyNumber.java
Java MyNumber -9 */
OUTPUT:
import java.util.Scanner;
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 M2:\n");
}
}
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++) {
sc.close();
}// Addition
// To Multiplicate Matrices
m1[i][j] = sc.nextInt();
}
}
System.out.printl n("\nEnter values of matrix M2:\n");
m2[i][j] = sc.nextInt();
}
}
}
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++) {
switch (choice) {
case 1:
m.addition();
break;
case 2:
m.multiplication();
break;
case 3:
m.transpose();
break;
}
sc.close();
}
}
OUTPUT: