PROGRAM No.1.
Question: Write a program to convert temperature from Fahrenheit to Celsius. Accept the
Fahrenheit value as a parameter.
public class FahrenheitToCelsius {
public static double convertFahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
public static void main(String[] args) {
double fahrenheit = 98.6; // Example Fahrenheit value
System.out.println(fahrenheit + " Fahrenheit = " +
convertFahrenheitToCelsius(fahrenheit) + " Celsius");
}
}
PROGRAM No.1.2
Question: Write a program to compute the perimeter of a rectangle. Accept length and
width as parameters.
public class RectanglePerimeter {
public static double computePerimeter(double length, double width) {
return 2 * (length + width);
}
public static void main(String[] args) {
double length = 5.0; // Example length
double width = 3.0; // Example width
System.out.println("Perimeter of the rectangle: " + computePerimeter(length,
width));
}
}
PROGRAM No.1.3
Question: Write a program to calculate kinetic energy. Accept mass (kg) and velocity
(m/s) as parameters.
public class KineticEnergy {
public static double computeKineticEnergy(double mass, double velocity) {
return 0.5 * mass * velocity * velocity;
}
public static void main(String[] args) {
double mass = 10.0; // Example mass in kg
double velocity = 5.0; // Example velocity in m/s
System.out.println("Kinetic Energy: " + computeKineticEnergy(mass, velocity) + "
Joules");
}
}
PROGRAM No.1.4
Question: Write a program to calculate the distance between two points (x1, y1) and (x2,
y2). Accept coordinates as parameters.
public class DistanceBetweenPoints {
public static double computeDistance(double x1, double y1, double x2, double y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
public static void main(String[] args) {
double x1 = 1.0, y1 = 2.0, x2 = 4.0, y2 = 6.0; // Example coordinates
System.out.println("Distance: " + computeDistance(x1, y1, x2, y2));
}
}