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 #17

🧮 Objective:
Read real-valued parameters x1, y1, x2, y2 and print the Euclidean distance between the points (x1, y1) and (x2, y2). Use Math.sqrt().

💡 Java Code


import java.util.Scanner;

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

        System.out.print("Enter first parameter for x: ");
        int x1 = scanner.nextInt();
        System.out.print("Enter first parameter for y: ");
        int y1 = scanner.nextInt();
        System.out.print("Enter second parameter for x: ");
        int x2 = scanner.nextInt();
        System.out.print("Enter second parameter for y: ");
        int y2 = scanner.nextInt();

        int q = (x2 - x1) * (x2 - x1);
        int p = (y2 - y1) * (y2 - y1);
        int a = q + p;
        double euclideanD = Math.sqrt(a);

        System.out.println(euclideanD);
    }
}
    

Princeton Web Exercises #18

🔢 Objective:
Read three integers x, y, z. Create boolean b that is true if the values are strictly ascending or descending, otherwise false. Print b.

💡 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 first number: ");
        int x = scanner.nextInt();
        System.out.println("Enter second number: ");
        int y = scanner.nextInt();
        System.out.println("Enter third number: ");
        int z = scanner.nextInt();

        boolean b;
        if ((x > y && y > z) || (x < y && y < z))
            b = true;
        else
            b = false;

        System.out.println(b);
    }
}
    

Princeton Web Exercises #19

Objective:
Read two integers and print true if both are divisible by 7, else false.

💡 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:
Compute the area of a triangle given side lengths a, b, c using Heron’s formula: s = (a + b + c) / 2, area = sqrt(s(s-a)(s-b)(s-c)).

💡 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.0;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

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

Princeton Creative Exercises #21

🌠 Objective:
Convert equatorial coordinates (declination δ, hour angle H, latitude φ) to horizontal coordinates (Altitude, Azimuth) using:
Altitude = asin( sin φ sin δ + cos φ cos δ cos H )
Azimuth = acos( (cos φ sin δ − sin φ cos δ cos H) / cos(Altitude) )
(Use radians for Java trig functions.)

💡 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 declination δ (radians): ");
        double dec = scanner.nextDouble();
        System.out.println("Enter hour angle H (radians): ");
        double angle = scanner.nextDouble();
        System.out.println("Enter latitude φ (radians): ");
        double lat = scanner.nextDouble();

        double altitude = Math.asin(Math.sin(lat) * Math.sin(dec) +
                                    Math.cos(lat) * Math.cos(dec) * Math.cos(angle));

        double azimuth  = Math.acos((Math.cos(lat) * Math.sin(dec) -
                                     Math.sin(lat) * Math.cos(dec) * Math.cos(angle)) /
                                     Math.cos(altitude));

        System.out.println("Position in horizontal coordinates:");
        System.out.print(altitude + ", ");
        System.out.print(azimuth);
    }
}
    

Princeton Web Exercises #22

⚖️ Objective:
Compute BMI given weight (kg) and height (m): BMI = weight / (height * height).

💡 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 Creative Exercises #25

🧊 Objective:
Compute wind chill given temperature t (°F, |t| ≤ 50) and wind speed v (mph, 3–120):
w = 35.74 + 0.6215·t + (0.4275·t − 35.75)·v^0.16

💡 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 #27

🧮 Objective:
For x^3 + b x^2 + c x + d, compute the discriminant:
b^2 c^2 − 4 c^3 − 4 b^3 d − 27 d^2 + 18 b c d.

💡 Java Code


import java.util.Scanner;

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

        System.out.println("x^3 + b x^2 + c x + d");
        System.out.print("Enter b: ");
        int b = scanner.nextInt();
        System.out.print("Enter c: ");
        int c = scanner.nextInt();
        System.out.print("Enter d: ");
        int d = scanner.nextInt();

        double discriminant = b*b*c*c - 4*c*c*c - 4*b*b*b*d - 27*d*d + 18*b*c*d;
        System.out.println(discriminant);
    }
}
    

Princeton Web Exercises #28

🌌 Objective:
Given masses m1, m2 and separation a, compute distance from the center of the more massive body to the barycenter:
r1 = a · m2 / (m1 + m2).

💡 Java Code


import java.util.Scanner;

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

        System.out.print("Enter m1 (earth-masses): ");
        double m1 = in.nextDouble();

        System.out.print("Enter m2 (earth-masses): ");
        double m2 = in.nextDouble();

        System.out.print("Enter a (km): ");
        double a = in.nextDouble();

        double r1 = a * m2 / (m1 + m2);
        System.out.println(r1);
    }
}
    

Princeton Creative Exercises #31

🧭 Objective:
Illustrate Math.sin, Math.cos, Math.tan. Input angle in degrees via command-line arg; convert with Math.toRadians.

💡 Java Code


// Trig.java
public class Trig {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: java Trig ");
            return;
        }

        double deg = Double.parseDouble(args[0]);
        double rad = Math.toRadians(deg);

        double s = Math.sin(rad);
        double c = Math.cos(rad);
        double t = Math.tan(rad);

        System.out.println("Angle (deg): " + deg);
        System.out.println("Angle (rad): " + rad);
        System.out.println("sin(theta): " + s);
        System.out.println("cos(theta): " + c);
        System.out.println("tan(theta): " + t);

        double identity = s*s + c*c; // should be ~1
        System.out.println("sin^2 + cos^2 = " + identity);
    }
}