| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A curated collection of Java study notes and code snippets organized in Markdown. Each file explores core concepts with runnable examples, clear explanations, and practical exercises. Perfect for quick reviews, interview prep, or learning Java fundamentals.
Fundamental algorithms and array operations:
Object memory models and AWT/Swing graphics:
Object-oriented programming fundamentals:
Clone the repository
git clone https://github.com/maxwell-hauser/java_examples_gh.git
cd java_examples_ghBrowse examples in your editor or GitHub
Copy snippets into your IDE
Create a test file Test.java:
public class Test {
public static void main(String[] args) {
// Test integer square root
System.out.println("Integer sqrt of 42: " + integerK(42)); // Expected: 6
// Test pi approximation
System.out.println("Pi (10 terms): " + piApprox(10));
// Test standard deviation
double[] data = {2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0};
System.out.println("Std dev: " + standardDeviation(data));
}
// Paste methods from java_example_1.md here
public static int integerK(int n) {
int k = 0;
while ((k + 1) * (k + 1) <= n) {
k++;
}
return k;
}
// Add other methods...
}Compile and run:
javac Test.java
java TestCreate GraphicsDemo.java:
import javax.swing.*;
import java.awt.*;
public class GraphicsDemo extends JFrame {
public GraphicsDemo() {
setTitle("Graphics Demo");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
int ww = getWidth();
int wh = getHeight();
// Paste paint logic from java_example_2.md here
g.drawRect(20, 40, ww - 40, wh - 60);
// Example: Draw concentric circles
int diameter = Math.min(ww, wh) - 40;
for (int r = diameter; r > 0; r -= 40) {
g.drawOval((ww - r) / 2, (wh - r) / 2, r, r);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(GraphicsDemo::new);
}
}Compile and run:
javac GraphicsDemo.java
java GraphicsDemo| Topic | Tip |
|---|---|
| Pi Approximation | More terms = better accuracy, but diminishing returns after ~1000 terms |
| Array Reversal | Recursive approach is elegant but uses stack space; iterative is more efficient |
| Matrix Operations | Always validate dimensions match before operations |
| Swing Painting | Use repaint() to trigger redraws; never call paint() directly |
| Inheritance | Use instanceof to check object types before casting |
| Access Modifiers | Default (no modifier) = package-private, not the same as public |
Contributions welcome! To add new examples or improve existing ones:
This project is provided for educational purposes. Feel free to use, modify, and share.
Built with ❤️ for Java learners
| Back | FazBrowse Home | New Git URL |