Java Programming Day 2

☕ Java Day 2 – Exploring Math, Formatting, and Financial Calculations
Welcome back to my Java Learning Series!
Today was Day 2, and I dived into the Math class, explored number formatting using printf()
, and even implemented real-world calculators like compound interest, hypotenuse, and circle geometry formulas.
This session helped me understand how Java can be used for real-world applications in fields like finance, engineering, and data representation.
🔢 1. Java Math Functions Practice
✅ Program:
javaCopyEditpublic class MathPractice {
public static void main(String[] args) {
System.out.println(Math.abs(-6)); // Absolute value
System.out.println(Math.ceil(4.5)); // Rounds up
System.out.println(Math.floor(4.5)); // Rounds down
System.out.println(Math.round(4.4)); // Nearest int
System.out.println(Math.max(3, 9)); // Max value
System.out.println(Math.min(3, 9)); // Min value
System.out.println(Math.sqrt(36)); // Square root
System.out.println(Math.pow(2, 3)); // Power
}
}
📘 Notes & What I Learned:
Method | Description | Output |
Math.abs() | Returns the absolute (positive) value | 6 |
Math.ceil() | Rounds the number up to the nearest whole number | 5.0 |
Math.floor() | Rounds the number down to the nearest whole number | 4.0 |
Math.round() | Rounds to the nearest integer | 4 |
Math.max(x, y) | Returns the larger of two values | 9 |
Math.min(x, y) | Returns the smaller of two values | 3 |
Math.sqrt() | Calculates square root | 6.0 |
Math.pow(x, y) | Returns x raised to the power of y | 8.0 |
These functions are extremely useful in scientific, statistical, and financial calculations.
📐 2. Hypotenuse Calculator
✅ Program:
javaCopyEditimport java.util.Scanner;
public class HypotenuseCalculator {
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();
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println("The hypotenuse is: " + c);
scanner.close();
}
}
📘 Notes:
Applied the Pythagorean Theorem:
c=a2+b2c = \sqrt{a^2 + b^2}c=a2+b2Used
Math.pow()
to square each side andMath.sqrt()
to find the hypotenuse.This program is useful in geometry, physics, and game development where calculating distances is common.
🌐 3. Circle Area, Circumference, and Volume
✅ Program:
javaCopyEditimport java.util.Scanner;
public class CircleMath {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double pi = Math.PI;
System.out.print("Enter the radius (cm): ");
double radius = scanner.nextDouble();
double area = pi * Math.pow(radius, 2);
double circumference = 2 * pi * radius;
double volume = (4.0 / 3) * pi * Math.pow(radius, 3);
System.out.printf("Area = %.2f cm²\n", area);
System.out.printf("Circumference = %.2f cm\n", circumference);
System.out.printf("Volume = %.2f cm³\n", volume);
scanner.close();
}
}
📘 Notes:
Formula | Meaning |
Area = π × r² | Surface area of the circle |
Circumference = 2 × π × r | Perimeter of the circle |
Volume = (4/3) × π × r³ | Volume of a sphere with radius r |
Math.PI
gives us an accurate value of π.Math.pow()
handles exponentiation.printf("%.2f", value)
was used for formatting up to 2 decimal places.
🎯 4. Number Formatting with printf()
✅ Program:
javaCopyEditpublic class FormatPractice {
public static void main(String[] args) {
double price1 = 9.99;
double price2 = 100.15;
double price3 = -54.01;
double price4 = 9000.99;
double price5 = 1000000.15;
double price6 = -5400000.01;
int a = 1;
int c = 663;
System.out.printf("%.1f\n", price1); // One decimal
System.out.printf("%.2f\n", price2); // Two decimals
System.out.printf("%.3f\n", price3); // Three decimals
System.out.printf("%+.1f\n", price4); // Show + sign
System.out.printf("%,.2f\n", price5); // Comma separator
System.out.printf("%(.2f\n", price6); // Negative in ()
System.out.printf("% .1f\n", price6); // Space for sign
System.out.printf("%04d\n", a); // Pad with zeros
System.out.printf("%04d\n", c);
System.out.printf("%4d\n", a); // Right aligned
System.out.printf("%4d\n", c);
System.out.printf("%-4d\n", c); // Left aligned
}
}
📘 Notes:
Format | Description |
%.2f | Fixed decimal precision |
%+ | Forces sign display (+/-) |
%,.2f | Adds comma separator (e.g. 1,000,000.00) |
%(...) | Negative numbers inside parentheses |
%04d | Pads integers with leading 0s |
%4d | Aligns output to 4 characters |
%-4d | Left-aligns output |
Such formatting is essential in financial reports, invoices, tables, and data presentation.
💸 5. Compound Interest Calculator
✅ Program:
javaCopyEditimport java.util.Scanner;
public class CompoundInterest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double principle, rate, amount;
int timesCompounded, years;
System.out.print("Enter the principle amount : ");
principle = scanner.nextDouble();
System.out.print("Enter the interest rate (in %) : ");
rate = scanner.nextDouble() / 100;
System.out.print("Enter the number of times compounded per year : ");
timesCompounded = scanner.nextInt();
System.out.print("Enter the # of years : ");
years = scanner.nextInt();
amount = principle * Math.pow(1 + rate / timesCompounded, timesCompounded * years);
System.out.printf("The amount after %d years is : $%.2f", years, amount);
scanner.close();
}
}
🧮 Formula Used:
Amount=P×(1+RN)N×T\text{Amount} = P \times \left(1 + \frac{R}{N}\right)^{N \times T}Amount=P×(1+NR)N×T
Where:
P = Principal
R = Annual Interest Rate
N = Number of times interest is compounded per year
T = Number of years
📘 Notes:
A practical financial application.
Used
Math.pow()
for exponentiation andprintf()
for clean currency formatting.This calculator can be extended to banking software or investment planning tools.
✅ Final Summary
✨ What I Achieved Today:
✔ Explored Java’s Math class to simplify complex calculations
✔ Applied geometry formulas in real-time programs
✔ Created a compound interest calculator – useful in finance
✔ Practiced formatting output like a pro with printf()
✔ Enhanced understanding of Java syntax, logic, and real-world utility
Subscribe to my newsletter
Read articles from Himanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Himanshi
Himanshi
Hi! I'm a curious and self-driven programmer currently pursuing my BCA 🎓 and diving deep into the world of Java ☕ from Day 1. I already have a foundation in programming, and now I'm expanding my skills one concept, one blog, and one project at a time. I’m learning Java through Bro Code’s YouTube tutorials, experimenting with code, and documenting everything I understand — from basic syntax to real-world applications — to help others who are just starting out too. I believe in learning in public, progress over perfection, and growing with community support. You’ll find beginner-friendly Java breakdowns, hands-on code snippets, and insights from my daily coding grind right here. 💡 Interests: Java, Web Dev, Frontend Experiments, AI-curiosity, Writing & Sharing Knowledge 🛠️ Tools: Java • HTML/CSS • JavaScript • Python (basics) • SQL 🎯 Goal: To become a confident Full Stack Developer & AI Explorer 📍 Based in India | Blogging my dev journey #JavaJourney #100DaysOfCode #CodeNewbie #LearnWithMe