Projects

Project #1

Problem statements

Problem Statement Q1

πŸ–₯️ Objective:
Create a Java program that asks the user for two numbers and displays the product of those numbers.

Test data:
Input first number: 70
Input second number: 4
Expected output:
70 Γ— 4 = 280

πŸ’‘ Java Code


import java.util.Scanner;

public class ProductCalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Input first number: ");
        int num1 = input.nextInt();

        System.out.print("Input second number: ");
        int num2 = input.nextInt();

        int product = num1 * num2;

        System.out.println(num1 + " Γ— " + num2 + " = " + product);
    }
}
  

Problem Statement Q2 Ice Cream Sales Tracker

πŸ–₯️ Objective:
Create a Java program that 1- Asks the user to enter the number of ice creams sold for Chocolate, Vanilla, and Strawberry for three days. 2- Calculates the total sales and the average sales (decimal and integer using casting). 3- Gives the shop’s performance grade (no if-else).

Sample Output:
Day 1 - Chocolate sales: 30
Day 1 - Vanilla sales: 25
Day 1 - Strawberry sales: 20
Day 2 - Chocolate sales: 40
Day 2 - Vanilla sales: 30
Day 2 - Strawberry sales: 35
Day 3 - Chocolate sales: 50
Day 3 - Vanilla sales: 40
Day 3 - Strawberry sales: 30
Total sales: 300.0
Average sales (decimal): 100.0
Average sales (integer): 100
Performance Grade: A

πŸ’‘ Java Code


import java.util.Scanner;
public class MyProgram
{
    public static void main(String[] args)
    {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("What is the number of Chocolate ice creams sold on day 1? ");
        int c1 = scanner.nextInt();
        System.out.println("What is the number of Vanilla ice creams sold on day 1? ");
        int v1 = scanner.nextInt();
        System.out.println("What is the number of Strawberry ice creams sold on day 1? ");
        int s1 = scanner.nextInt();
        System.out.println("What is the number of Chocolate ice creams sold on day 2? ");
        int c2 = scanner.nextInt();
        System.out.println("What is the number of Vanilla ice creams sold on day 2? ");
        int v2 = scanner.nextInt();
        System.out.println("What is the number of Strawberry ice creams sold on day 2? ");
        int s2 = scanner.nextInt();
        System.out.println("What is the number of Chocolate ice creams sold on day 3? ");
        int c3 = scanner.nextInt();
        System.out.println("What is the number of Vanilla ice creams sold on day 3? ");
        int v3 = scanner.nextInt();
        System.out.println("What is the number of Strawberry ice creams sold on day 3? ");
        int s3 = scanner.nextInt();
        
        double total= c1+c2+c3+v1+v2+v3+s1+s2+s3;
        int totali= c1+c2+c3+v1+v2+v3+s1+s2+s3;
        double avg= total/3;
        int aveg= totali/3;
        
        System.out.println("Day 1 - Chocolate sales: " + c1);
        System.out.println("Day 1 - Vanilla sales: " + v1);
        System.out.println("Day 1 - Strawberry sales: " + s1);
        System.out.println("Day 2 - Chocolate sales: " + c2);
        System.out.println("Day 2 - Vanilla sales: " + v2);
        System.out.println("Day 2 - Strawberry sales: " + s2);
        System.out.println("Day 3 - Chocolate sales: " + c3);
        System.out.println("Day 3 - Vanilla sales: " + v3);
        System.out.println("Day 3 - Strawberry sales: " + s3);
        System.out.println("Total sales: " + total);
        System.out.println("Average sales (decimal): " + avg);
        System.out.println("Average sales (integer): " + aveg);
        
        while (avg >= 100) {
            System.out.println("Performance grade: A");
            break;
        }
        while (100> avg && avg >= 70){
            System.out.println("Performance grade: B");
            break;
        }
        while (70> avg && avg >= 50){
            System.out.println("Performance grade: C");
            break;
        }
    }
}
  

Problem Statement Q3

πŸ–₯️ Objective:
A rocket is launched vertically from Earth.
The user enters the rocket’s mass (kg), engine force (N), and burn time (s).
The program should calculate and display:
- Net acceleration = F / m
- Final velocity = a Γ— t
- Altitude = 0.5 Γ— a Γ— t Γ— t
Display both decimal and integer values.
End with a rocket ASCII art. πŸš€

Test data:
Enter rocket mass (kg): 1000
Enter engine force (N): 20000
Enter burn time (s): 10
Expected output:
Net acceleration (m/sΒ²): 20.0
Net acceleration (int): 20
Final velocity (m/s): 200.0
Final velocity (int): 200
Altitude reached (m): 1000.0
Altitude (int): 1000

πŸ’‘ Java Code


import java.util.Scanner;
public class MyProgram {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter rocket mass (kg): ");
        int mass = scanner.nextInt();
        System.out.println("Enter engine force (N): ");
        int force = scanner.nextInt();
        System.out.println("Enter burn time (s): ");
        int burn = scanner.nextInt();

        int a = force / mass;
        double acc = force / mass;

        int v = a * burn;
        double vel = acc * burn;

        int alt = (a * burn * burn) / 2;
        double altitude = 0.5 * acc * burn * burn;

        System.out.println("Net acceleration (m/s^2): " + acc);
        System.out.println("Net acceleration (int): " + a);
        System.out.println("Final velocity (m/s): " + vel);
        System.out.println("Final velocity (int): " + v);
        System.out.println("Altitude reached (m): " + altitude);
        System.out.println("Altitude reached (int): " + alt);

        System.out.println("  /\\  ");
        System.out.println(" /  \\ ");
        System.out.println(" |==| ");
        System.out.println(" |  |");
        System.out.println("/____\\");
        System.out.println("  /\\  ");
        System.out.println("  **  ");
    }
}
    

Problem Statement Q4 – Bonus Challenge

🧠 Objective:
Without using Math.sqrt, compute the square root of a number N using Newton’s Method.
Manually compute 3 iterations using:
xβ‚–β‚Šβ‚ = 1/2 * (xβ‚– + N / xβ‚–)
Start with xβ‚€ = N / 2.
Display the approximation after 3 steps in decimal and integer form.

Test data:
Enter N: 200

Expected output:
Initial guess xβ‚€ = 100.0
x₁ = 55.0
xβ‚‚ = 30.909090909090907
x₃ = 18.654434250764525
Approx sqrt(N) β‰ˆ 18.654434250764525
Approx sqrt(N) (int) = 18

πŸ’‘ Java Code


import java.util.Scanner;

public class MyProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter N: ");
        double N = scanner.nextDouble();

        double x0 = N / 2.0;

        double x1 = 0.5 * (x0 + N / x0);
        double x2 = 0.5 * (x1 + N / x1);
        double x3 = 0.5 * (x2 + N / x2);

        System.out.println("Initial guess x0 = " + x0);
        System.out.println("x1 = " + x1);
        System.out.println("x2 = " + x2);
        System.out.println("x3 = " + x3);
        System.out.println("Approx sqrt(N) β‰ˆ " + x3);
        System.out.println("Approx sqrt(N) (int) = " + (int)x3);
    }
}
    

Princeton Creative Exercises #29

πŸ“† Objective:
Write a program DayOfWeek.java that takes a date as input and prints the day of the week it falls on.
Input: month, day, year (as integers).
Output: Integer 0–6, where:
0 = Sunday, 1 = Monday, ..., 6 = Saturday

Use this formula (Zeller's Congruence variant):
y0 = y βˆ’ (14 βˆ’ m) / 12
x = y0 + y0 / 4 βˆ’ y0 / 100 + y0 / 400
m0 = m + 12 Γ— ((14 βˆ’ m) / 12) βˆ’ 2
d0 = (d + x + 31 Γ— m0 / 12) mod 7

Sample Input:
Month: 9
Day: 5
Year: 2025

Expected output:
5 β†’ Friday

πŸ’‘ Java Code


import java.util.Scanner;

public class MyProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Which month (use 1 for January, 2 for February, etc.): ");
        int m = scanner.nextInt();
        
        if (m > 12) {
            System.out.println("There are no months after 12.");
            System.exit(0); 
        }

        System.out.println("Which day: ");
        int d = scanner.nextInt();
        System.out.println("Which year: ");
        int y = scanner.nextInt();

        double y0 = y - (14 - m) / 12;
        double x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
        double m0 = m + 12 * ((14 - m) / 12) - 2;
        double d0 = (d + x + 31 * m0 / 12) % 7;
        int weekday = (int) d0;

        System.out.println(weekday);
    }
}
    

Princeton Creative Exercises #25

🧊 Objective:
Write a Java program that calculates the wind chill based on user input.
The program should ask for:
- Temperature (Β°F) β€” must be ≀ 50
- Wind Speed (mph) β€” must be between 3 and 120

It uses the formula:
w = 35.74 + 0.6215Β·t + (0.4275Β·t βˆ’ 35.75)Β·v0.16

Test input:
Temperature: 40
Wind speed: 10
Expected output:
Wind chill: ~33.45Β°F

πŸ’‘ Java Code


import java.util.Scanner;

public class WindChillCalculator {
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter temperature (Β°F): ");
        double t = scanner.nextDouble();
        if (Math.abs(t) > 50) {
            System.out.println("Temperature must be ≀ 50Β°F in magnitude.");
            return;
        }
        System.out.print("Enter wind speed (mph): ");
        double v = scanner.nextDouble();
        if (v < 3 || v > 120) {
            System.out.println("Wind speed must be between 3 and 120 mph.");
            return;
        }
        double w = 35.74 + 0.6215 * t + (0.4275 * t - 35.75) * Math.pow(v, 0.16);
        System.out.printf("Wind chill: %.2fΒ°F\n", w);
    }
}
    

Princeton Web Exercises #19

πŸ”’ Objective:
Write a Java program that checks if two integers are both divisible by 7.
Use % operator to check divisibility.

Sample Input:
a = 14
b = 21
Expected output:
true

πŸ’‘ Java Code


import java.util.Scanner;

public class Divisibility {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter first number: ");
        int a = scanner.nextInt();

        System.out.print("Enter second number: ");
        int b = scanner.nextInt();

        boolean bothDivisible = (a % 7 == 0) && (b % 7 == 0);
        System.out.println(bothDivisible);
    }
}
    

Princeton Web Exercises #20

πŸ“ Objective:
Write a Java program that calculates the area of a triangle from its three sides using Heron’s formula:
area = √[s(s-a)(s-b)(s-c)] where s = (a + b + c) / 2

Sample Input:
a = 5, b = 6, c = 7
Expected Output:
Area β‰ˆ 14.70

πŸ’‘ Java Code


import java.util.Scanner;

public class TriangleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter side a: ");
        double a = scanner.nextDouble();

        System.out.print("Enter side b: ");
        double b = scanner.nextDouble();

        System.out.print("Enter side c: ");
        double c = scanner.nextDouble();

        double s = (a + b + c) / 2;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

        System.out.println("Area of triangle: ", area);
    }
}
    

Princeton Web Exercises #22

βš–οΈ Objective:
Write a Java program that calculates the Body Mass Index (BMI) of a user using:
BMI = weight / (height Γ— height)
where weight is in kilograms and height is in meters.

Sample Input:
Weight: 70 kg
Height: 1.75 m
Expected Output:
BMI: 22.86

πŸ’‘ Java Code


import java.util.Scanner;

public class BMI {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter weight (kg): ");
        double weight = scanner.nextDouble();

        System.out.print("Enter height (m): ");
        double height = scanner.nextDouble();

        double bmi = weight / (height * height);
        System.out.println("BMI: ", bmi);
    }
}
    

Princeton Web Exercises #26

🌌 Objective:
Write a Java program that calculates the distance from the center of the more massive body to the barycenter in a two-body orbital system.

πŸ“Œ Formula:
r₁ = (a Γ— mβ‚‚) / (m₁ + mβ‚‚)
where:


Determine whether the barycenter lies inside the first body (by comparing r₁ to radius R₁).

πŸ’‘ Java Code


import java.util.Scanner;

public class Barycenter {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter mass of first body (m1): ");
        double m1 = input.nextDouble();

        System.out.print("Enter mass of second body (m2): ");
        double m2 = input.nextDouble();

        System.out.print("Enter distance between the two bodies (a): ");
        double a = input.nextDouble();

        System.out.print("Enter radius of first body (R1): ");
        double R1 = input.nextDouble();

        double r1 = (a * m2) / (m1 + m2);

        System.out.printf("Barycenter distance from center of first body: %.2f km%n", r1);

        if (r1 < R1) {
            System.out.println("The barycenter lies within the first body.");
        } else {
            System.out.println("The barycenter lies outside the first body.");
        }
    }
}