Assignment On Java
Assignment On Java
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);
}
OUTPUT
import java.util.Scanner;
public class Arithmetic_Operation {
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
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;
}
}
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:
---------------------XXXXXXXXXXXXXX---------------------------
---------