Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Assignment Java

Uploaded by

Vaibhav Shukla
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)
9 views

Assignment Java

Uploaded by

Vaibhav Shukla
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/ 11

ASSIGNMENT-1

NAME: - VAIbhAV ShuklA


Sub:- jAVA
STd: - TY bSc (cS)
dIV: -A
Roll No.: - 233331072
bATch: - b4
Set A

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;

Public class RectangleCalculator {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter length of rectangle: “);

Double length = scanner.nextDouble();

System.out.print(“Enter breadth of rectangle: “);

Double breadth = scanner.nextDouble();

Double area = length * breadth;

Double perimeter = 2 * (length + breadth);

System.out.println(“Area of rectangle: “ + area);

System.out.println(“Perimeter of rectangle: “ + perimeter);

Scanner.close();

}
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.util.Scanner;

public class MenuDrivenProgram {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("Menu:");

System.out.println("1. Calculate the volume of a cylinder");

System.out.println("2. Find the factorial of a number");

System.out.println("3. Check if a number is Armstrong");

System.out.println("4. Exit");

System.out.print("Enter your choice: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.print("Enter radius of the cylinder: ");

double radius = scanner.nextDouble();

System.out.print("Enter height of the cylinder: ");

double height = scanner.nextDouble();

double volume = Math.PI * Math.pow(radius, 2) * height;

System.out.println("Volume of the cylinder: " + volume);

break;

case 2:
System.out.print("Enter a number to find factorial: ");

int num = scanner.nextInt();

int factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

System.out.println("Factorial of " + num + ": " + factorial);

break;

case 3:

System.out.print("Enter a number to check for Armstrong: ");

int armstrongNum = scanner.nextInt();

int originalNum = armstrongNum;

int sum = 0;

int numberOfDigits = (int) Math.log10(armstrongNum) + 1;

while (armstrongNum > 0) {

int digit = armstrongNum % 10;

sum += Math.pow(digit, numberOfDigits);

armstrongNum /= 10;

if (sum == originalNum) {

System.out.println(originalNum + " is an Armstrong number.");

} else {

System.out.println(originalNum + " is not an Armstrong number.");

break;

case 4:

System.out.println("Exiting the program.");

scanner.close();

System.exit(0);

default:

System.out.println("Invalid choice. Please select a valid option.");


}

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

Import java.util.Scanner;

Public class ReverseArray {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter the number of elements in the array: “);

Int n = scanner.nextInt();

Int[] arr = new int[n];

System.out.println(“Enter the elements of the array:”);

For (int I = 0; I < n; i++) {

Arr[i] = scanner.nextInt();

System.out.println(“Array elements in reverse order:”);

For (int I = n – 1; I >= 0; i--) {

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;

public class DateTimeFormats {

public static void main(String[] args) {

Date currentDate = new Date();

SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");

SimpleDateFormat format2 = new SimpleDateFormat("MM-dd-yyyy");

SimpleDateFormat format3 = new SimpleDateFormat("EEEE MMMM dd yyyy");

SimpleDateFormat format4 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

SimpleDateFormat format5 = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");

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

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

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

System.out.println("Current date and time is : " + format4.format(currentDate));

System.out.println("Current date and time is : " + format5.format(currentDate));

System.out.println("Current time is : " + new


SimpleDateFormat("HH:mm:ss").format(currentDate));

System.out.println("Current week of year is : " + new


SimpleDateFormat("w").format(currentDate));
System.out.println("Current week of month : " + new
SimpleDateFormat("W").format(currentDate));

System.out.println("Current day of the year is : " + new


SimpleDateFormat("D").format(currentDate));

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 class MyNumber {

private int value;

/**

* Default constructor to initialize the value to 0.

*/

public MyNumber() {

this.value = 0;

/**

* Constructor to initialize the value with the specified integer.

* @param value The initial value.

*/

public MyNumber(int value) {


this.value = value;

/**

* Checks if the number is negative.

* @return true if the number is negative, otherwise false.

*/

public boolean isNegative() {

return value < 0;

/**

* Checks if the number is positive.

* @return true if the number is positive, otherwise false.

*/

public boolean isPositive() {

return value > 0;

/**

* Checks if the number is zero.

* @return true if the number is zero, otherwise false.

*/

public boolean isZero() {

return value == 0;

/**
* Checks if the number is odd.

* @return true if the number is odd, otherwise false.

*/

public boolean isOdd() {

return value % 2 != 0;

/**

* Checks if the number is even.

* @return true if the number is even, otherwise false.

*/

public boolean isEven() {

return value % 2 == 0;

public static void main(String[] args) {

if (args.length != 1) {

System.out.println("Usage: java MyNumber <integer>");

System.exit(1);

int inputValue = Integer.parseInt(args[0]);

MyNumber number = new MyNumber(inputValue);

System.out.println("Number: " + number.value);

System.out.println("Is Negative? " + number.isNegative());

System.out.println("Is Positive? " + number.isPositive());

System.out.println("Is Zero? " + number.isZero());

System.out.println("Is Odd? " + number.isOdd());


System.out.println("Is Even? " + number.isEven());

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.util.Scanner;

Public class MatrixOperations {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

While (true) {

System.out.println(“Menu:”);

System.out.println(“1. Addition of Matrices”);

System.out.println(“2. Multiplication of Matrices”);

System.out.println(“3. Transpose of Matrix”);

System.out.println(“4. Exit”);

System.out.print(“Enter your choice: “);

Int choice = scanner.nextInt();

Switch (choice) {

Case 1:

// Matrix addition code here

Break;

Case 2:

// Matrix multiplication code here

Break;

Case 3:
// Matrix transpose code here

Break;

Case 4:

System.out.println(“Exiting the program.”);

Scanner.close();

System.exit(0);

Default:

System.out.println(“Invalid choice. Please select a valid option.”);

You might also like