0% found this document useful (0 votes)
9 views

Assignment On Java

Uploaded by

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

Assignment On Java

Uploaded by

b001230002gideon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment on Java

Date of Submission - 4/10/23


Name - Gideon Swer
Department - MCA
MLCU
YEAR - 2023
Q) Create a java program to calculate the area of a square,rectangle,triangle and circle.
Soln:
import java.util.Scanner;

public class AreaCalculator {


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

do {
System.out.println("Area Calculator Menu:");
System.out.println("1. Calculate the Area of a Square");
System.out.println("2. Calculate the Area of a
Rectangle");
System.out.println("3. Calculate the Area of a
Triangle");
System.out.println("4. Calculate the Area of a Circle");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");

choice = scanner.nextInt();

switch (choice) {
case 1:
calculateSquareArea(scanner);
break;
case 2:
calculateRectangleArea(scanner);
break;
case 3:
calculateTriangleArea(scanner);
break;
case 4:
calculateCircleArea(scanner);
break;
case 5:
System.out.println("Exiting the program.
Goodbye!");
break;
default:
System.out.println("Invalid choice. Please enter
a valid option (1-5).");
break;
}
} while (choice != 5);

scanner.close();
}
public static void calculateSquareArea(Scanner scanner) {
System.out.print("Enter the side length of the square: ");
double sideLength = scanner.nextDouble();
double area = sideLength * sideLength;
System.out.println("The area of the square is: " + area);
}

public static void calculateRectangleArea(Scanner scanner) {


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 area = length * width;
System.out.println("The area of the rectangle is: " + area);
}

public static void calculateTriangleArea(Scanner scanner) {


System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
double area = 0.5 * base * height;
System.out.println("The area of the triangle is: " + area);
}

public static void calculateCircleArea(Scanner scanner) {


System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double area = Math.PI * radius * radius;
System.out.println("The area of the circle is: " + area);
}
}

OUTPUT

Area Calculator Menu:


1. Calculate the Area of a Square
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Calculate the Area of a Circle
5. Exit
Enter your choice (1-5): 2
Enter the length of the rectangle: 5
Enter the width of the rectangle: 5
The area of the rectangle is: 25.0
Area Calculator Menu:
1. Calculate the Area of a Square
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Calculate the Area of a Circle
5. Exit
Enter your choice (1-5):

Q)Write a java program for a basic arithmetic operation?


Soln:

import java.util.Scanner;
public class Arithmetic_Operation {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
while(true)
{
System.out.println(" ");
System.out.println("Enter the two numbers to
perform operations");
System.out.println("Enter the first number: ");
int x = s.nextInt();
System.out.println("Enter the second number: ");
int y = s.nextInt();
System.out.println("Choose the operation you want
to perform");
System.out.println("Choose 1 for ADDITION");
System.out.println("Choose 2 for SUBTRACTION");
System.out.println("Choose 3 for MULTIPLICATION");
System.out.println("Choose 4 for DIVISION");
System.out.println("Choose 5 for MODULUS");
System.out.println("Choose 6 for EXIT");
int n = s.nextInt();
switch(n)
{
case 1:
int add;
add = x+y;
System.out.println("Addition of two numbers:
"+add);
break;

case 2:
int sub;
sub = x-y;
System.out.println("Subtraction of two numbers:
"+sub);
break;

case 3:
int mul;
mul = x*y;
System.out.println("Multiplication of two
numbers: "+mul);
break;

case 4:
float div;
div = (float)x/y;
System.out.println("Division of two numbers:
"+div);
break;

case 5:
int mod;
mod = x % y;
System.out.println("Modulus of two numbers:
"+mod);
break;

case 6:
System.exit(0);

}
}
}

OUTPUT

Enter the two numbers to perform operations


Enter the first number:
30
Enter the second number:
4
Choose the operation you want to perform
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
2
Subtraction of two numbers: 26

Enter the two numbers to perform operations


Enter the first number:

Q)Write a basic java program for arithmetic operation using


classes?
Soln:

import java.util.Scanner;
class ArithmeticOperations {
private int x;
private int y;
public ArithmeticOperations(int x,int y) {
this.x = x;
this.y = y;
}
public int add() {
return x + y;
}
public int subtract() {
return x-y;
}
public int multiply() {
return x*y;
}
public float divide() {
if(y!=0) {
return(float)x/y;
}
else {
System.out.println("Even: Division by zero!");
return Float.NaN; //Error Handling of division by
zero
}
}
public int modulus() {
if(y!=0) {
return x % y;
}
else {
System.out.println("Error: Modulus by zero!");
return 0;
}
}

public class Calculator {

public void main(String[] args) {


Scanner s = new Scanner(System.in);
while(true)
{
System.out.println(" ");
System.out.println("Enter the two numbers to
perform operations");
System.out.println("Enter the first number:
");
int x = s.nextInt();
System.out.println("Enter the second number:
");
int y = s.nextInt();
System.out.println("Choose the operation you
want to perform");
System.out.println("Choose 1 for ADDITION");
System.out.println("Choose 2 for
SUBTRACTION");
System.out.println("Choose 3 for
MULTIPLICATION");
System.out.println("Choose 4 for DIVISION");
System.out.println("Choose 5 for MODULUS");
System.out.println("Choose 6 for EXIT");
int n = s.nextInt();
switch(n)
{
case 1:
int add;
add = x+y;
System.out.println("Addition of two
numbers: "+add);
break;

case 2:
int sub;
sub = x-y;
System.out.println("Subtraction of two
numbers: "+sub);
break;

case 3:
int mul;
mul = x*y;
System.out.println("Multiplication of two
numbers: "+mul);
break;

case 4:
float div;
div = (float)x/y;
System.out.println("Division of two
numbers: "+div);
break;

case 5:
int mod;
mod = x % y;
System.out.println("Modulus of two
numbers: "+mod);
break;

case 6:
System.exit(0);

}
}
}
}

OUTPUT:

Enter the two numbers to perform operations


Enter the first number:
2
Enter the second number:
2
Choose the operation you want to perform
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
5
Modulus of two numbers: 0

Enter the two numbers to perform operations


Enter the first number:

---------------------XXXXXXXXXXXXXX---------------------------
---------

You might also like