Assignment Java
Assignment Java
a) 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 .
Type the following command and view the bytecodes. javap -c MyClass.
Javap java.lang.Object
javap java.lang.String
javap java.util.Scanner
b) Write a program to calculate perimeter and area of rectangle. (hint : area = length * breadth ,
perimeter=2*(length+breadth))
Import java.util.Scanner;
Scanner.close();
}
c) Write a menu driven program to perform the following operations
iv. Exit
import java.util.Scanner;
while (true) {
System.out.println("Menu:");
System.out.println("4. Exit");
switch (choice) {
case 1:
break;
case 2:
System.out.print("Enter a number to find factorial: ");
int factorial = 1;
factorial *= i;
break;
case 3:
int sum = 0;
armstrongNum /= 10;
if (sum == originalNum) {
} else {
break;
case 4:
scanner.close();
System.exit(0);
default:
c) Write a program to accept the array element and display in reverse order
Import java.util.Scanner;
Int n = scanner.nextInt();
Arr[i] = scanner.nextInt();
System.out.print(arr[i] + “ “);
Scanner.close();
}
Set B
a) 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
Note: Use java.util.Date and java.text.SimpleDateFormat class
import java.util.Date;
import java.text.SimpleDateFormat;
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.
/**
* MyNumber class represents an integer and provides methods to test its properties.
*/
/**
*/
public MyNumber() {
this.value = 0;
/**
*/
/**
*/
/**
*/
/**
*/
return value == 0;
/**
* Checks if the number is odd.
*/
return value % 2 != 0;
/**
*/
return value % 2 == 0;
if (args.length != 1) {
System.exit(1);
Import java.util.Scanner;
While (true) {
System.out.println(“Menu:”);
System.out.println(“4. Exit”);
Switch (choice) {
Case 1:
Break;
Case 2:
Break;
Case 3:
// Matrix transpose code here
Break;
Case 4:
Scanner.close();
System.exit(0);
Default: