[go: up one dir, main page]

0% found this document useful (0 votes)
92 views11 pages

Java Programming Assignment Solutions

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)
92 views11 pages

Java Programming Assignment Solutions

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

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:
[Link] , [Link] and [Link]. and also Compile sample program .
Type the following command and view the bytecodes. javap -c MyClass.

Javap [Link]

javap [Link]

javap [Link]

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

Import [Link];

Public class RectangleCalculator {

Public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link](“Enter length of rectangle: “);

Double length = [Link]();

[Link](“Enter breadth of rectangle: “);

Double breadth = [Link]();

Double area = length * breadth;

Double perimeter = 2 * (length + breadth);

[Link](“Area of rectangle: “ + area);

[Link](“Perimeter of rectangle: “ + perimeter);

[Link]();

}
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 [Link];

public class MenuDrivenProgram {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

while (true) {

[Link]("Menu:");

[Link]("1. Calculate the volume of a cylinder");

[Link]("2. Find the factorial of a number");

[Link]("3. Check if a number is Armstrong");

[Link]("4. Exit");

[Link]("Enter your choice: ");

int choice = [Link]();

switch (choice) {

case 1:

[Link]("Enter radius of the cylinder: ");

double radius = [Link]();

[Link]("Enter height of the cylinder: ");

double height = [Link]();

double volume = [Link] * [Link](radius, 2) * height;

[Link]("Volume of the cylinder: " + volume);

break;

case 2:
[Link]("Enter a number to find factorial: ");

int num = [Link]();

int factorial = 1;

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

factorial *= i;

[Link]("Factorial of " + num + ": " + factorial);

break;

case 3:

[Link]("Enter a number to check for Armstrong: ");

int armstrongNum = [Link]();

int originalNum = armstrongNum;

int sum = 0;

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

while (armstrongNum > 0) {

int digit = armstrongNum % 10;

sum += [Link](digit, numberOfDigits);

armstrongNum /= 10;

if (sum == originalNum) {

[Link](originalNum + " is an Armstrong number.");

} else {

[Link](originalNum + " is not an Armstrong number.");

break;

case 4:

[Link]("Exiting the program.");

[Link]();

[Link](0);

default:

[Link]("Invalid choice. Please select a valid option.");


}

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

Import [Link];

Public class ReverseArray {

Public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link](“Enter the number of elements in the array: “);

Int n = [Link]();

Int[] arr = new int[n];

[Link](“Enter the elements of the array:”);

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

Arr[i] = [Link]();

[Link](“Array elements in reverse order:”);

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

[Link](arr[i] + “ “);

[Link]();

}
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 [Link] IST 2021
Current date and time is : 31/08/21 [Link] PM +0530
Current time is : [Link]
Current week of year is : 35
Current week of month : 5
Current day of the year is : 243
Note: Use [Link] and [Link] class

import [Link];

import [Link];

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");

[Link]("Current date is : " + [Link](currentDate));

[Link]("Current date is : " + [Link](currentDate));

[Link]("Current date is : " + [Link](currentDate));

[Link]("Current date and time is : " + [Link](currentDate));

[Link]("Current date and time is : " + [Link](currentDate));

[Link]("Current time is : " + new


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

[Link]("Current week of year is : " + new


SimpleDateFormat("w").format(currentDate));
[Link]("Current week of month : " + new
SimpleDateFormat("W").format(currentDate));

[Link]("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() {

[Link] = 0;

/**

* Constructor to initialize the value with the specified integer.

* @param value The initial value.

*/

public MyNumber(int value) {


[Link] = 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 ([Link] != 1) {

[Link]("Usage: java MyNumber <integer>");

[Link](1);

int inputValue = [Link](args[0]);

MyNumber number = new MyNumber(inputValue);

[Link]("Number: " + [Link]);

[Link]("Is Negative? " + [Link]());

[Link]("Is Positive? " + [Link]());

[Link]("Is Zero? " + [Link]());

[Link]("Is Odd? " + [Link]());


[Link]("Is Even? " + [Link]());

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 [Link];

Public class MatrixOperations {

Public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

While (true) {

[Link](“Menu:”);

[Link](“1. Addition of Matrices”);

[Link](“2. Multiplication of Matrices”);

[Link](“3. Transpose of Matrix”);

[Link](“4. Exit”);

[Link](“Enter your choice: “);

Int choice = [Link]();

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:

[Link](“Exiting the program.”);

[Link]();

[Link](0);

Default:

[Link](“Invalid choice. Please select a valid option.”);

You might also like