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

Practical 1 Java Programming

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Practical 1 Java Programming

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PRACTICAL 1: OOP USING JAVA CS-359

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;

public class RectangleCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the length of the


rectangle: ");
double length = scanner.nextDouble();

System.out.print("Enter the width of the


rectangle: ");
double width = scanner.nextDouble();

double perimeter = calculatePerimeter(length,


width);
double area = calculateArea(length, width);

System.out.println("Perimeter of the rectangle: " +


perimeter);
System.out.println("Area of the rectangle: " +
area);
}

/**
* 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

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

// MenuDrivenProgram.java

import java.util.Scanner;

public class MenuDrivenProgram {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;

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

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


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

/**
* 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();

int originalNumber = number;


int sum = 0;
int digitCount = String.valueOf(number).length();

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...

=== Code Execution Successful ===

d) Write a program to accept the array element and


display in reverse order.
// ReverseArray.java

import java.util.Scanner;

public class ReverseArray {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();

int[] array = new int[size];

System.out.println("Enter the elements of the


array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

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;

while (start < end) {


int temp = array[start];
array[start] = array[end];
array[end] = temp;

start++;
end--;
}
}
}
OUTPUT:
Enter the size of the array: 6
Enter the elements of the array:
234578
Original array:
234578
Reversed array:
875432

=== Code Execution Successful ===


SET B)
a) Write a java program to display the system date and time in
various formats shown below: Lab Assignment public class
MyClass { int num; public MyClass() { num=0; } public
MyClass(int num) { this.num = num; } public static void
main(String[] args) { MyClass m1 = new MyClass(); if(args.length
> 0) { int n = Integer.parseInt(args[0]); MyClass m2 = new
MyClass(n); System.out.println(m1.num);
System.out.println(m2.num); } else
System.out.println(“Insufficient arguments”); } } 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.

// DateFormatDemo.java
import java.util.Date;
import java.text.SimpleDateFormat;

public class DateFormatDemo {


public static void main(String[] args) {
Date currentDate = new Date();

// Current date is : 31/08/2021


SimpleDateFormat dateFormat1 = new
SimpleDateFormat("dd/MM/yyyy");
System.out.println("Current date is : " +
dateFormat1.format(currentDate));

// Current date is : 08-31-2021


SimpleDateFormat dateFormat2 = new
SimpleDateFormat("MM-dd-yyyy");
System.out.println("Current date is : " +
dateFormat2.format(currentDate));

// Current date is : Tuesday August 31 2021


SimpleDateFormat dateFormat3 = new
SimpleDateFormat("EEEE MMMM dd yyyy");
System.out.println("Current date is : " +
dateFormat3.format(currentDate));

// Current date and time is : Fri August 31 15:25:59 IST


2021
SimpleDateFormat dateFormat4 = new
SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
System.out.println("Current date and time is : " +
dateFormat4.format(currentDate));
// Current date and time is : 31/08/21 15:25:59 PM +0530
SimpleDateFormat dateFormat5 = new
SimpleDateFormat("dd/MM/yy hh:mm:ss a Z");
System.out.println("Current date and time is : " +
dateFormat5.format(currentDate));

// Current time is : 15:25:59


SimpleDateFormat timeFormat = new
SimpleDateFormat("HH:mm:ss");
System.out.println("Current time is : " +
timeFormat.format(currentDate));

// Current week of year is : 35


SimpleDateFormat weekOfYearFormat = new
SimpleDateFormat("W");
System.out.println("Current week of year is : " +
weekOfYearFormat.format(currentDate));

// Current week of month : 5


int weekOfMonth = getWeekOfMonth(currentDate);
System.out.println("Current week of month : " +
weekOfMonth);

// Current day of the year is : 243


SimpleDateFormat dayOfYearFormat = new
SimpleDateFormat("D");
System.out.println("Current day of the year is : " +
dayOfYearFormat.format(currentDate));
}
/**
* Returns the week of the month for a given date.
*
* @param date the date
* @return the week of the month
*/
public static int getWeekOfMonth(Date date) {
int dayOfMonth = date.getDate();
int firstDayOfMonth = getFirstDayOfMonth(date);

int weekOfMonth = (dayOfMonth + firstDayOfMonth - 1) /


7 + 1;
return weekOfMonth;
}

/**
* 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

=== Code Execution Successful ===

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.

/**
* 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);

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

System.out.println("Is negative: " +


myNumber.isNegative());
System.out.println("Is positive: " +
myNumber.isPositive());
System.out.println("Is zero: " + myNumber.isZero());
System.out.println("Is odd: " + myNumber.isOdd());
System.out.println("Is even: " + myNumber.isEven());
} else {
System.out.println("Insufficient arguments");
}
}
}

OUTPUT:
Insufficient arguments

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

You might also like