Sunday, November 3, 2024

Circle Using a Class in Java

Circle Using a Class in Java

 


Program Listing

import java.text.DecimalFormat;


class Circle {

    private double radius;


    public Circle(double radius) {

        this.radius = radius;

    }


    public double getRadius() {

        return radius;

    }


    public double calculateArea() {

        return Math.PI * Math.pow(radius, 2);

    }


    public double calculateCircumference() {

        return 2 * Math.PI * radius;

    }

}


public class Main {

    public static void main(String[] args) {

        Circle circle = new Circle(8.15);


        DecimalFormat df = new DecimalFormat("#.00");


        System.out.println("\n\tCircle Using a Class in Java\n");

        

        System.out.println("\tRadius: " + df.format(circle.getRadius()));

        System.out.println("\tArea: " + df.format(circle.calculateArea()));

        System.out.println("\tCircumference: " + df.format(circle.calculateCircumference()));

         System.out.println("\n\tEnd of Program. Thank you for Using This Program\n");

    }

}


Wednesday, October 23, 2024

Temperature Converter Using a Class in Python

Temperature Converter Using a Class in Python

 


Program Listing

class TemperatureConverter:

    @staticmethod

    def celsius_to_fahrenheit(celsius):

        return (celsius * 9 / 5) + 32


    @staticmethod

    def fahrenheit_to_celsius(fahrenheit):

        return (fahrenheit - 32) * 5 / 9



celsius_temperature = 34.36

fahrenheit_temperature = 134.98


print("\n\tTemperature Converter Using a Class in Python\n")


print("\tCelsius to Fahrenheit: {:.2f}°C = {:.2f}°F".format(celsius_temperature, TemperatureConverter.celsius_to_fahrenheit(celsius_temperature)))

print("\tFahrenheit to Celsius: {:.2f}°F = {:.2f}°C".format(fahrenheit_temperature, TemperatureConverter.fahrenheit_to_celsius(fahrenheit_temperature)))


print("\n\tEnd of Program. Thank you for Using This Program\n")


Monday, October 21, 2024

Simple Calculator Using Class in Python

Simple Calculator Using a Class in Python

 


Program Listing

class Calculator:

    def __init__(self):

        print("\n\tSimple Calculator Using a Class in Python\n\n")


    def get_user_input(self):

        self.num1 = float(input("\tEnter the first number: "))

        self.operator = input("\tEnter the operator (+, -, *, /): ")

        self.num2 = float(input("\tEnter the second number: "))


    def calculate_result(self):

        if self.operator == '+':

            self.result = self.num1 + self.num2

        elif self.operator == '-':

            self.result = self.num1 - self.num2

        elif self.operator == '*':

            self.result = self.num1 * self.num2

        elif self.operator == '/':

            if self.num2 != 0:

                self.result = self.num1 / self.num2

            else:

                print("\tError: Division by zero is not allowed.")

                exit(1)

        else:

            print("\tError: Invalid operator")

            exit(1)


    def display_result(self):

        print("\n\tThe Result is  ", self.result, ".")

        print("\n\tEnd of Program")


# Create an instance of the Calculator class

calculator = Calculator()


# Get user input

calculator.get_user_input()


# Calculate and display the result

calculator.calculate_result()

calculator.display_result()