Practical 1 Java Programming
Practical 1 Java Programming
SET A)
b) Write a program to calculate perimeter and area of
rectangle. (hint : area = length * breadth ,
perimeter=2*(length+breadth))
// RectangleCalculator.java
import java.util.Scanner;
/**
* Calculates the perimeter of a rectangle.
*
* @param length the length of the rectangle
* @param width the width of the rectangle
* @return the perimeter of the rectangle
*/
public static double calculatePerimeter(double
length, double width) {
return 2 * (length + width);
}
/**
* Calculates the area of a rectangle.
*
* @param length the length of the rectangle
* @param width the width of the rectangle
* @return the area of the rectangle
*/
public static double calculateArea(double length,
double width) {
return length * width;
}
}
OUTPUT:
Enter the length of the rectangle: 4
Enter the width of the rectangle: 3
Perimeter of the rectangle: 14.0
Area of the rectangle: 12.0
// MenuDrivenProgram.java
import java.util.Scanner;
do {
System.out.println("Menu:");
System.out.println("1. Calculate the volume of
cylinder");
System.out.println("2. Find the factorial of given
number");
System.out.println("3. Check the number is
Armstrong or not");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
calculateCylinderVolume(scanner);
break;
case 2:
findFactorial(scanner);
break;
case 3:
checkArmstrong(scanner);
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please
try again.");
}
} while (choice != 4);
}
/**
* Calculates the volume of a cylinder.
*
* @param scanner the scanner object to read input
*/
public static void calculateCylinderVolume(Scanner
scanner) {
System.out.print("Enter the radius of the cylinder:
");
double radius = scanner.nextDouble();
System.out.print("Enter the height of the cylinder:
");
double height = scanner.nextDouble();
/**
* Finds the factorial of a given number.
*
* @param scanner the scanner object to read input
*/
public static void findFactorial(Scanner scanner) {
System.out.print("Enter a number: ");
int number = scanner.nextInt();
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial of " + number + ": " +
factorial);
}
/**
* Checks if a number is Armstrong or not.
*
* @param scanner the scanner object to read input
*/
public static void checkArmstrong(Scanner scanner) {
System.out.print("Enter a number: ");
int number = scanner.nextInt();
while (number != 0) {
int digit = number % 10;
sum += (int) Math.pow(digit, digitCount);
number /= 10;
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is an
Armstrong number.");
} else {
System.out.println(originalNumber + " is not an
Armstrong number.");
}
}
}
OUTPUT:
Menu:
1. Calculate the volume of cylinder
2. Find the factorial of given number
3. Check the number is Armstrong or not
4. Exit
Enter your choice: 1
Enter the radius of the cylinder: 4
Enter the height of the cylinder: 12
Volume of the cylinder: 603.1857894892403
Menu:
1. Calculate the volume of cylinder
2. Find the factorial of given number
3. Check the number is Armstrong or not
4. Exit
Enter your choice: 2
Enter a number: 2
Factorial of 2: 2
Menu:
1. Calculate the volume of cylinder
2. Find the factorial of given number
3. Check the number is Armstrong or not
4. Exit
Enter your choice: 3
Enter a number: 5
5 is an Armstrong number.
Menu:
1. Calculate the volume of cylinder
2. Find the factorial of given number
3. Check the number is Armstrong or not
4. Exit
Enter your choice: 4
Exiting...
import java.util.Scanner;
System.out.println("Original array:");
displayArray(array);
reverseArray(array);
System.out.println("Reversed array:");
displayArray(array);
}
/**
* Displays the elements of an array.
*
* @param array the array to display
*/
public static void displayArray(int[] array) {
for (int element : array) {
System.out.print(element + " ");
}
System.out.println();
}
/**
* Reverses the elements of an array in place.
*
* @param array the array to reverse
*/
public static void reverseArray(int[] array) {
int start = 0;
int end = array.length - 1;
start++;
end--;
}
}
}
OUTPUT:
Enter the size of the array: 6
Enter the elements of the array:
234578
Original array:
234578
Reversed array:
875432
// DateFormatDemo.java
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* Returns the first day of the month for a given date.
*
* @param date the date
* @return the first day of the month
*/
public static int getFirstDayOfMonth(Date date) {
SimpleDateFormat dateFormat = new
SimpleDateFormat("MM/dd/yyyy");
String firstDayOfMonthString =
dateFormat.format(date).replace(date.getDate() + "", "1");
Date firstDayOfMonthDate =
dateFormat.parse(firstDayOfMonthString, new
java.text.ParsePosition(0));
int firstDayOfMonth = firstDayOfMonthDate.getDay();
return firstDayOfMonth;
}
}
OUTPUT:
Current date is : 27/07/2024
Current date is : 07-27-2024
Current date is : Saturday July 27 2024
Current date and time is : Sat Jul 27 15:03:07 GMT 2024
Current date and time is : 27/07/24 03:03:07 PM +0000
Current time is : 15:03:07
Current week of year is : 4
Current week of month : 4
Current day of the year is : 209
/**
* Represents a number with various properties.
*/
public class MyNumber {
/**
* The private integer data member.
*/
private int number;
/**
* Default constructor to initialize the number to 0.
*/
public MyNumber() {
this.number = 0;
}
/**
* Constructor to initialize the number to a specified value.
*
* @param number the value to initialize
*/
public MyNumber(int number) {
this.number = number;
}
/**
* Checks if the number is negative.
*
* @return true if the number is negative, false otherwise
*/
public boolean isNegative() {
return number < 0;
}
/**
* Checks if the number is positive.
*
* @return true if the number is positive, false otherwise
*/
public boolean isPositive() {
return number > 0;
}
/**
* Checks if the number is zero.
*
* @return true if the number is zero, false otherwise
*/
public boolean isZero() {
return number == 0;
}
/**
* Checks if the number is odd.
*
* @return true if the number is odd, false otherwise
*/
public boolean isOdd() {
return number % 2 != 0;
}
/**
* Checks if the number is even.
*
* @return true if the number is even, false otherwise
*/
public boolean isEven() {
return number % 2 == 0;
}
public static void main(String[] args) {
if (args.length > 0) {
int value = Integer.parseInt(args[0]);
MyNumber myNumber = new MyNumber(value);
OUTPUT:
Insufficient arguments