study.com work

This commit is contained in:
Zakaria
2025-11-28 12:06:47 -05:00
commit f2b14c75c2
86 changed files with 2525 additions and 0 deletions
BIN
View File
Binary file not shown.
+118
View File
@@ -0,0 +1,118 @@
/*
* in this Java program, the user can calculate the area of a rectangle,
* and the volumes of a cube, cylinder, and sphere.
* The user will be prompt to enter the length and width of a rectangle,
* and then choose a shape to calculate the volume of (cube, cylinder, or sphere).
*/
import java.util.InputMismatchException;
import java.util.Scanner;
class Rectangle {
public static double rectangleArea(double length, double width) {
return length * width;
}
}
class Cube {
public static double cubeVolume(double side) {
return Math.pow(side, 3);
}
}
class Cylinder {
public static double cylinderVolume(double radius, double height) {
return Math.PI * Math.pow(radius, 2) * height;
}
}
class Sphere {
public static double sphereVolume(double radius) {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
//Main Class
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
// User prompt for the length and width
System.out.println("Rectangle Area");
System.out.print("Please enter length: ");
double length = scanner.nextDouble();
System.out.print("Please enter width: ");
double width = scanner.nextDouble();
// making sure that the user put positive numbers otherwise we throw a //warning
if (length <= 0 || width <= 0) {
System.out.println("WARNING!: Length and width must be positive numbers.");
} else {
double area = Rectangle.rectangleArea(length, width);
System.out.println("Rectangle Area: " + area);
}
// Choose shape for volume calculation
System.out.println("");
System.out.println("Volume Calculation");
System.out.println("Choose a shape to calculate volume:");
System.out.println("1. Cube");
System.out.println("2. Cylinder ");
System.out.println("3. Sphere ");
System.out.print("Enter your choice (1-3): ");
int userChoice = scanner.nextInt();
switch (userChoice) {
case 1:
System.out.print("Enter side length of cube: ");
double side = scanner.nextDouble();
if (side <= 0) {
System.out.println("Error: Side must be a positive number.");
} else {
double cubeVolume = Cube.cubeVolume(side);
System.out.println("Cube Volume: " + cubeVolume);
}
break;
case 2:
System.out.print("Enter radius of cylinder: ");
double cylinderRadius = scanner.nextDouble();
System.out.print("Enter height of cylinder: ");
double cylinderHeight = scanner.nextDouble();
if (cylinderRadius <= 0 || cylinderHeight <= 0) {
System.out.println("Error: Radius and height must be positive numbers.");
} else {
double cylinderVolume = Cylinder.cylinderVolume(cylinderRadius, cylinderHeight);
System.out.println("Cylinder Volume: " + cylinderVolume);
}
break;
case 3:
System.out.print("Enter radius of sphere: ");
double sphereRadius = scanner.nextDouble();
if (sphereRadius <= 0) {
System.out.println("Error: Radius must be a positive number.");
} else {
double sphereVolume = Sphere.sphereVolume(sphereRadius);
System.out.println("Sphere Volume: " + sphereVolume);
}
break;
default:
System.out.println("Invalid choice.");
}
} catch (InputMismatchException e) {
System.out.println("Input error: Please enter valid numeric values.");
} finally {
scanner.close();
}
}
}
//Please copy and paste this code to an IDE and run
Binary file not shown.
+85
View File
@@ -0,0 +1,85 @@
import java.util.Scanner;
/*
* The program below is a java script that helps find the perfect numbers using
* Nested Loops.
* A perfect numbersber is a positive integer that equals the sum of its
* proper positive divisors (excluding the number itself)
* */
public class PerfectNumbers {
public static void main(String[] args) {
// Part 1: Find perfect numbers between 1 and 200
System.out.println("Perfect Numbers between 1 and 200:");
// findAndPrintPerfectNumber(200);
// Add a separator line in the output.
System.out.println("\n----------------------------------------\n");
// Part 2: User input range (up to maximum of 1000)
Scanner scanner = new Scanner(System.in);
int upperBound;
do {
System.out.print("Enter an upper bound between 1 and 1000 for checking perfect numbers: ");
while (!scanner.hasNextInt()) { // Validate integer input.
System.out.println("That's not a valid number. Please enter an integer.");
scanner.next(); // discard invalid input
System.out.print("Enter an upper bound between 1 and 1000: ");
}
upperBound = scanner.nextInt();
if (upperBound < 1 || upperBound > 9000) {
System.out.println("Please enter a number between 1 and 1000.");
} else {
break;
}
} while (true);
// Now check for perfect numbers in the user-specified range.
System.out.println("\nPerfect Numbers between 1 and " + upperBound + ":");
// findAndPrintPerfectNumber(upperBound);
scanner.close();
}
/**
* This method finds and prints all perfect numbers up to a given limit
* (inclusive).
*/
public static void perfectNums() {
// Let's get the perfect numbers in the a range of positive integers ( 1 to 200)
// OUTER LOOP
for (int i = 1; i <= 200; i++) {
int total = 0;
// INNER LOOP
for (int j = 1; j <= i / 2; j++) {
total += j;
}
if (total == i) {
System.out.println(i + " is a perfect number");
}
}
}
// public static void findAndPrintPerfectNumber(int limit) {
// for (int i = 1; i <= limit; i++) { // Loop through each number
// int sumOfDivisors = 0;
//
// // Find proper divisors using nested loops.
// // Note: Instead of a nested loop, we can iterate up to half of the number,
// // but since you requested a nested loop approach, here's how it might look:
// for (int j = 1; j <= i / 2; j++) { // Only need to check up to i/2
// if (i % j == 0) {
// sumOfDivisors += j;
// }
// }
//
// // Check if the number is perfect.
// if (sumOfDivisors == i && i != 1) { // Exclude 1 as it's not considered a
// perfect number
// System.out.println(i);
// }
// }
// }
}