Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.7 kB
0
Indexable
Never
import java.util.Scanner;

public class Temperature{

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Choose an option:");
        System.out.println("1. Convert Celsius to Fahrenheit");
        System.out.println("2. Convert Fahrenheit to Celsius");

        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                convertCelsiusToFahrenheit(scanner);
                break;
            case 2:
                convertFahrenheitToCelsius(scanner);
                break;
            default:
                System.out.println("Invalid choice. Please choose 1 or 2.");
        }
    }
    private static void convertCelsiusToFahrenheit(Scanner scanner) {
        System.out.print("Enter temperature in Celsius: ");
        double celsius = scanner.nextDouble();
        double fahrenheit = (celsius * 9 / 5) + 32;
        System.out.println(celsius + " Celsius is equal to " + fahrenheit + " Fahrenheit.");
    }
    private static void convertFahrenheitToCelsius(Scanner scanner) {
        System.out.print("Enter temperature in Fahrenheit: ");
        double fahrenheit = scanner.nextDouble();
        double celsius = (fahrenheit - 32) * 5 / 9;
        System.out.println(fahrenheit + " Fahrenheit is equal to " + celsius + " Celsius.");
    }
}





2.import java.lang.Math;

class DistanceCalculator {
    private double x1, y1, x2, y2;
    public DistanceCalculator(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
    public double calculateDistance() {
        double distanceCal = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
        return distanceCal;
    }

    public static void main(String[] args) {
        double x1 = 1.0, y1 = 2.0, x2 = 4.0, y2 = 5.0;
        DistanceCalculator distanceCalculator = new DistanceCalculator(x1, y1, x2, y2);
        double distance = distanceCalculator.calculateDistance();
        System.out.println("Distance between points: " + distance);
    }
}




3.public class Calculator {

    public static double add(double num1, double num2) {
        return num1 + num2;
    }
    public static double subtract(double num1, double num2) {
        return num1 - num2;
    }
    public static double multiply(double num1, double num2) {
        return num1 * num2;
    }
    public static double divide(double num1, double num2) {
        if (num2 != 0) {
            return num1 / num2;
        } else {
            System.out.println("Division by zero is not allowed");
            return Double.NaN;
        }
    }
    public static void main(String[] args) {
        double operand1 = 10;
        double operand2 = 5;
        System.out.println("Addition: " + add(operand1, operand2));
        System.out.println("Subtraction: " + subtract(operand1, operand2));
        System.out.println("Multiplication: " + multiply(operand1, operand2));
        System.out.println("Division: " + divide(operand1, operand2));
    }
}


4.import java.util.Scanner;
class Circle {
    private double radius;
    public Circle(double radius) {
        this.radius = radius;
    }
    public double calculateArea() {
        return Math.PI *Math.pow(radius,2);
    }
}
public class AreaCircle{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();
        Circle circle = new Circle(radius);
        System.out.println("Area of the circle: " + circle.calculateArea());
    }
}



5.
Leave a Comment