Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Java Fundamentals: Mastering Flow Control and String Manipulation

Tech 1

Logic Control Exercises

To reinforce flow control concepts, consider the following implementtaions for common algorithmic tasks.

Parity Check

Determining whether an integer is even or odd using modular arithmetic:

int value = 7;
if (value % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}

Rendering a Diamond Pattern

Constructing a symmetrical shape using nested loops requires managing spaces and symbols relative to the row index:

int height = 5;
int midpoint = height / 2 + 1;

// Upper section
for (int row = 1; row <= midpoint; row++) {
    for (int spaces = 0; spaces < midpoint - row; spaces++) System.out.print(" ");
    for (int stars = 0; stars < 2 * row - 1; stars++) System.out.print("*");
    System.out.println();
}

// Lower section
for (int row = midpoint - 1; row >= 1; row--) {
    for (int spaces = 0; spaces < midpoint - row; spaces++) System.out.print(" ");
    for (int stars = 0; stars < 2 * row - 1; stars++) System.out.print("*");
    System.out.println();
}

Series Summation

Calculating the sum of factorials (1 + 1/2! + 1/3! + ... + 1/20!) efficiently using a single loop:

double totalSum = 0;
double currentFactorial = 1;
for (int i = 1; i <= 20; i++) {
    currentFactorial *= i;
    totalSum += 1.0 / currentFactorial;
}
System.out.println(totalSum);

Working with Strings

In Java, String is a class rather than a primitive type. While you can instantiate strings using various constructors, literal assignment is the standard practice.

Initialization Methods

// Using character arrays
char[] data = {'j', 'a', 'v', 'a'};
String str1 = new String(data);

// Sub-segment extraction
String str2 = new String(data, 0, 2); // Result: "ja"

// Literal assignment (preferred for memory efficiency via the String Pool)
String str3 = "Java Programming";

String Concatenation

Java supports simple concatenation using the + operator. When mixing non-string types with strings, Java automatically converts the primitive values into their string representations. Operator precedence is critical when performing arithmetic within a concatanation statement:

int pages = 50;
double hours = 2.5;

// Implicit conversion
System.out.println("Read " + pages + " pages in " + hours + " hours.");

// Explicit grouping for arithmetic
System.out.println("Total work units: " + (pages + hours));

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.