0% found this document useful (0 votes)
8 views18 pages

Java Programs for Area, Perimeter, and More

The document outlines various Java programming assignments focused on using Java tools and IDEs, including tasks like calculating the area and perimeter of rectangles, performing operations on arrays, and working with matrices. It also includes examples of Java code for different functionalities such as date formatting, menu-driven programs, and class definitions. Additionally, it emphasizes the use of command line arguments and provides hints for implementing specific methods.

Uploaded by

saihalwai01
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)
8 views18 pages

Java Programs for Area, Perimeter, and More

The document outlines various Java programming assignments focused on using Java tools and IDEs, including tasks like calculating the area and perimeter of rectangles, performing operations on arrays, and working with matrices. It also includes examples of Java code for different functionalities such as date formatting, menu-driven programs, and class definitions. Additionally, it emphasizes the use of command line arguments and provides hints for implementing specific methods.

Uploaded by

saihalwai01
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 : Java Tools and IDE, Simple Java Programs

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 8. Type the following command and view the bytecodes.
Syntax javap -c MyClass.

javap -c [Link]
javap -c [Link]
javap -c [Link]
===========================================================

//SET A b) Write a program to calculate perimeter and area of rectangle.


//(hint : area = length * breadth , perimeter=2*(length+breadth))
// Java program to create a class to
// print the area and perimeter of a
// rectangle

import [Link].*;

public class Rectangle


{
double length;
double width;

// Area Method to calculate the area of Rectangle


void Area()
{
double area;
area = [Link] * [Link];
[Link]("Area of rectangle is : "
+ area);
}

// Perimeter Method to calculate the Perimeter of


// Rectangle
void Perimeter()
{
double perimeter;
perimeter = 2 * ([Link] + [Link]);
[Link]("Perimeter of rectangle is : "
+ perimeter);
}
}

class RectangleDemo {

public static void main(String args[])


{
// Object of Rectangle class is created
Rectangle rect = new Rectangle();

[Link] = 15.854;
[Link] = 22.65;

[Link]("Length = " + [Link]);


[Link]("Width = " + [Link]);

// Calling of Area method of Rectangle Class


[Link]();

// Calling of Perimeter method of Rectangle Class


[Link]();
}
}
========================================================
// Java program to calculate the area and perimeter of a rectangle using class
concept

import [Link].*;

class Rectangle {

int length, width;

Rectangle(int length, int width) {


this. length = length;
[Link] = width;
}
public void area() {
int areaOfRectangle;
areaOfRectangle = [Link] * [Link];
[Link]("Area of rectangle with the given input is : " +
areaOfRectangle);
}

public void perimeter() {


int perimeters;
perimeters = 2 * ([Link] + [Link]);
[Link]("Perimeter of rectangle with the given input is : " +
perimeters);
}
}
public class RectangleDemo1{
public static void main(String args[]) {
Rectangle rect_obj = new Rectangle(10,5); // ojbect creation
[Link]("Length = " + rect_obj.length);
[Link]("Width = " + rect_obj.width);
rect_obj.area(); // returns area of rectangle
rect_obj.perimeter(); //returns perimeter of rectangle
}
}
public class Assig1Seta2 {
public static void main(String[] strings) {

final double width = 5.6;


final double height = 8.5;

// Calculate the perimeter of the rectangle


double perimeter = 2 * (height + width);

// Calculate the area of the rectangle


double area = width * height;

[Link]("Perimeter is 2*(%.1f + %.1f) = %.2f \n", height, width,


perimeter);
[Link]("Area is %.1f * %.1f = %.2f \n", width, height, area);
}
}
/* SET A
a) Write a program to calculate perimeter and area of rectangle.
(hint : area = length * breadth , perimeter=2*(length+breadth))
*/

import [Link];
public class Rectangle
{

public static void main(String[] args)


{
Scanner sc = new Scanner([Link]);

[Link]("Enter Length of Rectangle : ");


int length = [Link]();

[Link]("Enter breadth of Rectangle : ");


int breadth = [Link]();

int area = length * breadth;


[Link]("Area of Reactangle : " + area);

int Perimeter = 2 * (length + breadth);


[Link]("Perimeter of Reactangle : " + Perimeter);

[Link]();
}
}
==========================================================
/*SET A
b)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 NumericalsMenu {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("\[Link] of Cylinder. \[Link] of Number.


\[Link] Number. \[Link]");
[Link]("Enter Your Choice : ");
int choice = [Link]();

switch (choice) {
case 1:
[Link]("Enter Radius:");
Float r = [Link]();
[Link]("Enter Height:");
Float h = [Link]();
double Volume = [Link] * r * r * h;
[Link]("Volume of Cylinder: %f" ,Volume);
break;

case 2:
[Link]("Enter Number for Finding Factorial : ");
int num = [Link]();
long fact = 1;
for (int i = 1; i <= num; ++i) {
fact = fact * i;
}
[Link]("Factorial of %d = %d\n", num, fact);
break;

case 3:
[Link]("Enter Number for Finding Armstrong Number : ");

int n = [Link]();

int leng = 0;
int t1 = n;
while (t1 != 0) {
t1 = t1 / 10;
leng = leng + 1;
}

int t2 = n;
int arm = 0;
int rem;

while (t2 != 0) {
int mult = 1;
rem = t2 % 10;
for (int i = 1; i <= leng; i++) {
mult = mult * rem;
}
arm = arm + mult;
t2 = t2 / 10;
}
if (arm == n) {
[Link]("The given number is armstrong..!");
} else {
[Link]("The given number is not armstrong..!");
}
break;
case 4:
[Link](0);

default:
break;

}
[Link]();

}
}
==========================================================
/*SET A
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 sc = new Scanner([Link]);

[Link]("Enter size of Array :");


int n = [Link]();

int arr[] = new int[n];

[Link]("Enter Elements in Array");


for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

[Link]("Array elements:");
for (int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}

[Link]("\nArray 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 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
*/
import [Link];
import [Link];

public class DateFormatter {


public static void main(String[] args) {

Date date = new Date();


SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String Str = [Link](date);
[Link]("Current date is: " + Str);

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


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

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


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

sdf = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");


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

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


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

sdf = new SimpleDateFormat("hh:mm:ss");


Str = [Link](date);
[Link]("Current time is: " + Str);

sdf = new SimpleDateFormat("w");


Str = [Link](date);
[Link]("Current week of year is: " + Str);

sdf = new SimpleDateFormat("W");


Str = [Link](date);
[Link]("Current week of the month is: " + Str);
sdf = new SimpleDateFormat("D");
Str = [Link](date);
[Link]("Current day of the year: " + Str);
}
}

/*SET B
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 MenuMatrix {


// to calculate Addition of matrix
public void addition() {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of row and column: ");
int r = [Link]();
int c = [Link]();

int[][] m1 = new int[r][c];


int[][] m2 = new int[r][c];

[Link]("\nEnter values of matrix M1:\n");


for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link]("Enter element at index %d and %d :", i, j);
m1[i][j] = [Link]();
}
}
[Link]("\nEnter values of matrix M2:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link]("Enter element at index %d and %d :", i, j);
m2[i][j] = [Link]();
}
}
[Link]("\nSum of M1 and M2:");
int[][] sum = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
sum[i][j] = m1[i][j] + m2[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](sum[i][j] + " ");
}
[Link]();
}
[Link]();

}// Addition

// To Multiplicate Matrices

public void multiplication() {

Scanner sc = new Scanner([Link]);


[Link]("Enter size of row and column: ");
int r = [Link]();
int c = [Link]();

int[][] m1 = new int[r][c];


int[][] m2 = new int[r][c];
[Link]("\nEnter values of matrix M1:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link]("Enter element at index %d and %d :", i, j);
m1[i][j] = [Link]();
}
}
[Link]("\nEnter values of matrix M2:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link]("Enter element at index %d and %d :", i, j);
m2[i][j] = [Link]();
}
}

[Link]("\nMultiplication of M1 and M2:\n");


int[][] mul = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
for (int k = 0; k < c; k++) {
mul[i][j] = mul[i][j] + m1[i][k] * m2[k][j];
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](mul[i][j] + " ");
}
[Link]();
}
[Link]();
}// Multiplication

// Transpose of Matrix
public void transpose() {

Scanner sc = new Scanner([Link]);


[Link]("Enter size of row and column: ");
int r = [Link]();
int c = [Link]();

int[][] m = new int[r][c];


[Link]("Enter values of Matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link]("Enter element at index %d and %d :", i, j);
m[i][j] = [Link]();
}
}
[Link]("\nEntered Matrix...\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](m[i][j] + " ");
}
[Link]();
}
[Link]("\nTranspose Matrix...\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
[Link](m[j][i] + " ");
}
[Link]();
}
[Link]();
}// Transpose

public static void main(String[] args) {


MenuMatrix m = new MenuMatrix();
Scanner sc = new Scanner([Link]);

[Link]("\[Link] of Matrix. \[Link] of Matrix.


\[Link] of Matrix. \[Link]");
[Link]("Enter Your Choice : ");
int choice = [Link]();

switch (choice) {
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
}
[Link]();
}
}
/*SET B
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.
*/

public class MyNumber {


private int x;

public MyNumber() {
x = 0;
}

public MyNumber(int x) {
this.x = x;
}

public boolean isNegative() {


if (x < 0)
return true;
else
return false;
}

public boolean isPositive() {


if (x > 0)
return true;
else
return false;
}

public boolean isZero() {


if (x == 0)
return true;
else
return false;
}

public boolean isOdd() {


if (x % 2 != 0)
return true;
else
return false;
}

public boolean isEven() {


if (x % 2 == 0)
return true;
else
return false;
}

public static void main(String[] args){


int x = [Link](args[0]);
MyNumber m = new MyNumber(x);
if ([Link]()) {
[Link]("Number is Negative");
}
if ([Link]()) {
[Link]("Number is Positive");
}
if ([Link]()) {
[Link]("Number is Even");
}
if ([Link]()) {
[Link]("Number is Odd");
}
if ([Link]()) {
[Link]("Number is Zero");
}

}
/**SET C
/*
Set C
a) Write a program to accept n names of country and display them in descending
order.
*/
import [Link].*;

public class countrysort {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter total no of Countries: ");
int n = [Link]();
String country[] = new String[n];
[Link]();
for (int i = 0; i < n; i++) {
[Link]("Enter country no :" + (i + 1));
country[i] = [Link]();
}
[Link]("Country names without Sorting:");
for (String ele : country) {
[Link]("" + ele);
}
String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (country[i].compareTo(country[j]) < 0) {
temp = country[i];
country[i] = country[j];
country[j] = temp;
}
}
}
[Link]("Countries in Descending order:");
for (String ele : country) {
[Link]("" + ele);
}
[Link]();
}
}
====================================================
b) Write a menu driven program to perform the following operations on 2D array:
i. Sum of diagonal elements
ii. Sum of upper diagonal elements
iii. Sum of lower diagonal elements
iv. Exit
*/

import [Link].*;
public class DiagonalMenu {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of row and column: ");
int n = [Link]();
int sum = 0;
int[][] m = new int[n][n];
[Link]("\nEnter values of matrix :\n");

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
m[i][j] = [Link]();
}
}

[Link]("\nEntered Matrix...\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
[Link](m[i][j] + " ");
}
[Link]();
}
do {
[Link]("\nMatrix Operations\n");
[Link]("\[Link] of Diagonal Elements.");
[Link]("\[Link] of Upper Diagonal Elements.");
[Link]("\[Link] of Lower Diagonal Elements.");
[Link]("\[Link]");
[Link]("Enter Your Choice :");
int ch = [Link]();
switch (ch) {
case 1:
sum = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
if (i==j) {
sum = sum+m[i][j];
}
}
}
[Link]("\nAddition of Diagonal Elements:"+sum);
break;

case 2:
sum = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
if (j>i) {
sum = sum+m[i][j];
}
}
}
[Link]("\nAddition of Upper Diagonal Elements:"+sum);
break;

case 3:
sum = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
if (i>j) {
sum = sum+m[i][j];
}
}
}
[Link]("\nAddition of Lower Diagonal Elements:"+sum);
break;

case 4:
[Link](0);
default:
break;
}
} while (true);

}
}
/* SeET C
c) Write a program to display the 1 to 15 tables.
(1*1=1
2*1=2
…….
15 * 1 = 15
1*2=2
2*2=4
15 * 2 = 30
1*3=3

2*3=6

15 * 3 = 45
…..
1 * 10 = 10 2 * 10 = 20 15 * 10 = 150)
*/

public class table {


public static void main(String[] args) {
for (int i = 1; i <= 15; i++) {
[Link]("Table of "+(i)+" number:\n");
for (int j = 1; j <= 10; j++) {

[Link](i + "*" + j + "=" +i*j);


}
[Link]("----------------------");
}
}
}
=========================================================

You might also like