Converting Java Help3 System into Class and Subroutine Practice

This is Java for Beginner’s Guide. Java is one of an ideal Object Oriented Programming (OOP) language to learn for your programming career. Along the way, you will encounter branches and conditional statements such as if, switch, for, while, do-while, break, and continue statement. Learning Blocks {} is essential to continue this course since it separates logic of your syntax coded. As for the basic programming, you may encounter the basic Help 3 System in Java. See source code at  http://www.phil.uu.nl/java/java02/osborne/projects/Module3/Help3.java.

Help3 System source code is a finish code to start your programming practice. There are free programming environment such as eclipse and netbeans to handle the source code but if you prefer to use the native Command-Line-Interface (Windows) or Terminal (Mac) will still work.

Subroutine Java Help3 System: An Example Source Code

The image below is the output from the console.
java help 3 system

Name your Class as Help3.java source code to run in your programming environment.

/**
* An Example of Class with Subroutines for Java Help3 System.
*
*This program provides a help system that displays the syntax for the Java control statements such as such as if, switch, for, while, do-while, break, and continue statement. 
* It will ask the user which options will be displayed.
*
**/

public class Help3 {
    public static void main(String args[]) 
        throws java.io.IOException {

        for(;;) {

        	libClass.incUnderForLoop(); //libClass and incUnderForLoop subroutine embedded
        									

        }
    }								
}

class libClass { 							// Another class has been created

	static char choice;						// Member variable

	public static void incUnderForLoop() {			

		Switches();							// Switches subroutine is embedded here

        System.out.println("\n");

        switch(choice) {
            case '1':
                System.out.println("The if:\n");
                System.out.println("if(condition) statement;");
                System.out.println("else statement;");
                break;
            case '2':
                System.out.println("The switch:\n");
                System.out.println("switch(expression) {");
                System.out.println(" case constant:");
                System.out.println(" statement sequence");
                System.out.println(" break;");
                System.out.println(" // ...");
                System.out.println("}");
                break;
            case '3':
                System.out.println("The for:\n");
                System.out.print("for(init; condition; iteration)");
                System.out.println(" statement;");
                break;
            case '4':
                System.out.println("The while:\n");
                System.out.println("while(condition) statement;");
                break;
            case '5':
                System.out.println("The do-while:\n");
                System.out.println("do {");
                System.out.println(" statement;");
                System.out.println("} while (condition);");
                break;
            case '6':
                System.out.println("The break:\n");
                System.out.println("break; or break label;");
                break;
            case '7':
                System.out.println("The continue:\n");
                System.out.println("continue; or continue label;");
                break;

        }
        System.out.println();	

	}

	static void Switches() {							// 		Switches Subroutine

		do {
            System.out.println("Help on:");
            System.out.println(" 1. if");
            System.out.println(" 2. switch");
            System.out.println(" 3. for");
            System.out.println(" 4. while");
            System.out.println(" 5. do-while");
            System.out.println(" 6. break");
            System.out.println(" 7. continue\n");
            System.out.print("Choose one (q to quit): ");
            doLoop(); 										// doLoop subroutine embedded
        } while( choice < '1' | choice > '7' & choice != 'q');

		optionExit();									// optionExit subroutine embedded
	}

	static void doLoop() {								// doLoop Subroutine
		do {
            choice = TextIO.getChar();
        } while(choice == '\n' | choice == '\r'); 

	}

	static void optionExit(){
		if (choice == 'q') {
			System.exit(0);									// I change break; to System.exit(0); function
		}
	}

}	   													// end of libClass 

 

The code above chopped the original Java Help3 System source code, it will still display same result with the practice of subroutines and classes. You can revise it yourself to make more classes and subroutines.

Handling with Multi-Dimentional Variable Arrays in Java

Handling with Multi-Dimentional Variable Arrays in Java

I want to be straightforward on this topic on how to Handle Multi-Dimentional Variable Arrays in Java.

This variable array, composed of 2 rows and 5 columns as you can see below

// int[][]  A  =  new int[2][5];

can be initial initialize with the following array values

int[][]  A  =  {  {  1,  0, 12, -1, 9 }, // First Row
{  7, -3,  2,  5, 10 } // Second Row
};

Here’s the better figure for your deeper comprehension.
two dimensional array
Source: http://math.hws.edu/javanotes/c7/s5.html

But this time, I am dealing with 2 x 5 matrix.

Printing Individual Variable Value

To print a particular a particular element from Row 1, let say we want to print “12”,
Just add this line after variable declaration

System.out.println(A[0][2]) // [0] means, First Row and [2] means 3rd element in the First Row
                            // This will print "12" 

Printing Values in Each Rows and Columns

public class TwoDimentionalArray {

    public static void main( String args[] ) {
    try {
    //	int[][]  A  =  new int[2][5];
    	
    	int[][]  A  =  {  {  1,  0, 12, -1, 9 },
                {  7, -3,  2,  5, 10 }
             };

    	for (int i = 0; i < A[0].length; i++) {
    	        
    	       TextIO.put(A[0][i] + "\t");  // Printing all values in the First Row
    	       TextIO.putln(A[1][i]);       // Printing all values in the Second Row
    	 
    	}
    		
    } catch ( ArrayIndexOutOfBoundsException e ) {TextIO.putln("Exceeded!"); }
    	
    }
}

To Handle all Variable Arrays in one line

public class ArrayMultidimensional {
    public static void main( String args[] ) {
    try {
    //	int[][]  A  =  new int[2][5];
    	
    	int[][]  A  =  {  {  1,  0, 12, -1, 9 },
                {  7, -3,  2,  5, 10 }
             };

    	for (int i = 0; i < A.length; i++) {
    	   for (int j = 0; j < 5; j++) {
    	       TextIO.putln( A[i][j]);
    	   }
    	 
    	}
    		
    } catch ( ArrayIndexOutOfBoundsException e ) {System.out.println("Exceed"); }
    	
    }
}
    

Storing values in Variable arrays will save you time to handle many variables. In Java, there’s no other method on dealing with large variables. I used to program using TextIO, which you can also replace it with System.out.print as a native class in Java package.