java programming language

Learn from examples

1. Program to output an integer, a string, and a floating point number, initially just one method. exj1.java source code // exj1.java first example simple output integer, string, double // note // makes rest of line a comment import java.io.*; public class exj1 // class name same as file name { public exj1() // constructor same as class name { int i = 7; // declare variable i with initial value 7 char ac = 'a'; // single character String msg = "sample string"; // declare msg with string in quotes float x = 37.95f; // declare floating point x with initial value double y = 127.34e10; // declare double precision with initial value System.out.println("exj1.java running"); // simple print title System.out.println("i="+i); // for decimal System.out.println("ac="+ac); // for character System.out.println("msg="+msg);// for string System.out.println("x="+x); // for simple floating point System.out.println("y="+y); // for double with exponent } // end constructor exj1 optional comment public static void main (String[] args) // "main" required { new exj1(); // construct and execute } } // end class exj1 optional comment exj1_java.out Output of execution exj1.java running i=7 ac=a msg=sample string x=37.95 y=1.2734E12 2. commands to compile and execute the source code at a minimum, Windows, Linux, MacOSX. Windows: javac exj1.java java exj1 Linux: javac -cp . exj1.java java -cp . exj1 MacOSX javac -cp . exj1.java java -cp . exj1 save output to a file: java -cp . exj1 > exj1_java.out 3. You must be able to declare variables and list and arrays and matrix of various types. exj3.java source code exj3_java.out Output of execution // exj3.java example declare variables, list, arrays import java.io.*; // needed for input and output public class exj3 // class name same as file name { // declarations for entire class may go here public exj3() // constructor same as class name { // declarations for this procedure go here // typically first, yet may be in code, just before use int i = 1; // optional initialization 32 bit byte i8 = 1; // 8 bit short i16 = 1; // 16 bit long i64 = 0xabcdef01; // 64 bit initialized with hexadecimal float x = 39.27F; // needs F else warning double y = 127.5E200; boolean me = true; // could be false String msg = "short"; // uppercase S for type String int vec[] = {1, 2, 4, 8}; // initialize an array, any type double av[] = {1.5, 2.2, 9.1}; String as[] = {"abc", "xyz", "more"}; int mat[][] = {{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; // 4 by 3 matrix int big[] = new int[100]; // allocate space for 100 integers double small[] = new double[5]; // space for 5 doubles double matrix[][] = new double[4][3]; // matrix 4 rows of 3 doubles System.out.println("exj3.java running"); System.out.println("i="+i); // System.out.println("mat="+mat); must print with loops } // end constructor exj3 optional comment public static void main (String[] args) { new exj3(); } } // end class exj3 optional comment Execution output: exj3.java running i=1 4. You need to be able to have loops, iteration statements exj4.java source code exj4_java.out Output of execution // exj4.java example file iteration, loops public class exj4 // class name same as file name { public exj4() // constructor same as class name { System.out.println("exj4.java running"); int i; // declare loop variable int mat[][] = {{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; // 4 by 3 matrix int sum = 0; for(i=0; i<4; i++) // columns of mat { // the } could be at end of previous line for(int j=0; j<3; j++) // j only exists inside loop { sum += mat[i][j]; } // end j optional comment } // end i optional comment System.out.println("sum="+sum); for(i=2; i≤10; i=i+2) { System.out.println("i="+i); // even numbers } } // end constructor exj4 // optional comment public static void main (String[] args) { new exj4(); } } // end class exj4 optional comment Execution output: exj4.java running sum=42 i=2 i=4 i=6 i=8 i=10 5. You need if then else conditional statements exj5.java source code exj5_java.out Output of execution // exj5.java example file iteration, if then else public class exj5 // class name same as file name { public exj5() // constructor same as class name { System.out.println("exj5.java running"); double x = 2.0; // declare test variable int i = 3; if(x < 3.0) // < > <= >= == != compare operations { System.out.println("compare < > <= >= == != x="+x); } if(x > 3.0 || i == 3 && i > 2) // || is or, && is and { System.out.println("logic || is or, && is and i="+i); } if( x > 3.0) { System.out.println("x > 3.0"); } else if(i < 3) // optional { System.out.println("i < 3"); } else // optional, get here if none of the above true { System.out.println("none of the above"); } // end if optional comment } // end constructor exj5 optional comment public static void main (String[] args) { new exj5(); } } // end class exj5 optional comment Execution output: exj5.java running compare < > <= >= == != x=2.0 logic || is or, && is and i=3 none of the above 6. You need to be able to create functions, procedures, subroutines. exj6.java source code exj6_java.out Output of execution // exj6.java example file create function or procedure and call import java.io.*; // needed for input and output public class exj6 // class name same as file name { // declarations for entire class may go here public exj6() // constructor same as class name { // declarations for this procedure go here // typically first, yet may be in code, just before use int i, j; int jj[] = new int[1]; // single value made into an array to get return double arr[][]={{1.0,2.0,3.0},{4.0,5.0,6.0}}; double val; System.out.println("exj6.java running"); // call proc1, all functions and procedures after constructor proc1(7); // use funct1 i = funct1(2, 3); System.out.println("funct1(2,3) returns "+i); val = sum(arr); // size of array found in sum System.out.println("sum(arr)= "+val); i = ret2(i, jj); // pass jj by address so it can be changed System.out.println("ret2 i="+i+", jj="+jj[0]); System.out.println("end exj6.java"); } // end constructor exj6 double sum(double A[][]) // determine size inside { double asum = 0.0; int n = A.length; int m = A[0].length; for(int i=0; iexj7.java source code exj7_java.out Output of execution 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, operating system commands, header files, etc. exj8.java source code exj8_java.out Output of execution

  Other examples that may be of interest:

  obj.java source code
  obj_java.out Output of execution
  matmul_thread4.java source code
  matmul_thread4_java.out Output of execution
  matmulb_thread4.java source code
  matmulb_thread4_java.out Output of execution

Last updated 11/23/2020