24-311: Numerical Method JAVA Programming Tutorial ====================================================================== How to compile and run a simple JAVA Code. 1. write/edit source code (*.java) 2. Class file (*.class) Typical JAVA source codes consist of: function definitions variable declarations statements -------------------------------------------------- // test01 <= this is a comment /* another form of comment */ import java.io.*; public class test0 { public static void main(String arg[]) throws IOException { System.out.print("The volume of the box is "); System.out.println(11*9*40); } } -------------------------------------------------- ====================================================================== How to declare variables. identifier: a name consisting of letter and digits, the first of which must be a letter, with underscore counting as a letter variable: - the name of a chunk of computer memory - each variable refers to a chunk of memory - data type: the size of the chunk and the way the bits in the chunk are interpreted character: char integer: short, int, long floating point: float, double, long double variable declaration -------------------------------------------------- public class test1 { public static void main(String arg[]) { int height; int width; int length; ... } } -------------------------------------------------- public class test1 { public static void main(String arg[]) { int height, width, length; } } -------------------------------------------------- public class test1 { public static void main(String arg[]) { int height = 5, width = 6, length = 7; ... } } -------------------------------------------------- ====================================================================== How to write statements that read input from your keyboard. -------------------------------------------------- import java.io.*; public class test3 { public static void main (String args[])throws IOException { int height, width, length; System.out.println("Please type height, width, and length."); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); height = Integer.parseInt(b.readLine()); width = Integer.parseInt(b.readLine()); length = Integer.parseInt(b.readLine()); System.out.print("The volume is "); System.out.println(height*width*length); } } -------------------------------------------------- ====================================================================== How to write output to and read input from a file. -------------------------------------------------- import java.io.*; public class test4 { public static void main(String args[])throws IOException { int h,w,l; PrintWriter p = new PrintWriter(new FileWriter("output.txt")); p.print("I hate Micro$oft"); p.close(); BufferedReader b = new BufferedReader(new FileReader("input.txt")); h = Integer.parseInt(b.readLine()); w = Integer.parseInt(b.readLine()); l = Integer.parseInt(b.readLine()); System.out.print("Volume = "); System.out.println(h*w*l); } } -------------------------------------------------- import java.io.*; public class test4 { public static void main(String args[])throws IOException { int h,w,l; PrintStream p = new PrintStream(new FileOutputStream(new File("output.txt"))); p.print("I hate Micro$oft"); p.close(); BufferedReader b = new BufferedReader(new FileReader("input.txt")); h = Integer.parseInt(b.readLine()); w = Integer.parseInt(b.readLine()); l = Integer.parseInt(b.readLine()); System.out.print("Volume = "); System.out.println(h*w*l); } } ====================================================================== How to define simple functions. DATA_TYPE FUNCTION_NAME (DATA_TYPE PARAMETER, ..., DATA_TYPE PARAMETER) { VARIABLE_DECLARATION ... VARIABLE_DECLARATION STATEMENT ... STATEMENT } -------------------------------------------------- import java.io.*; public class test5 { public static void main(String args[]) throws IOException { int height, width, length; System.out.println("Please type height, width, and length."); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); height = Integer.parseInt(b.readLine()); width = Integer.parseInt(b.readLine()); length = Integer.parseInt(b.readLine()); display_volume(height, width, length); } public static void display_volume(int h, int w, int l) { int volume; volume = h*w*l; System.out.print("the volume is "); System.out.println(volume); } } -------------------------------------------------- ====================================================================== Skipping... How to create classes and objects. How to define constructor member functions. How to define reader and write member functins. How to benefit from data abstraction. How to protect member variables from harmful reference. How to define classes that inherit variables and functins. ====================================================================== How to perform tests using numerical predicates. ==, !=, >, <, >=, <= The value of the expression using a numerical predicate can be: 0 False 1 True Use the and operator, &&, and the or operator, ||, to combine boolean expressions. the value of (2 < 3 && 3 > 4) is 0 the value of (2 < 3 || 3 > 4) is 1 A common error is to write = when you intend to check for equality. != means "Are two numbers not equal?" ! can appear alone, in which case the ! character denotes the "not" operator, e.g., the value of !0 is 1 and !1 is 0 the value of !(6 < 3) is 1 the value of !(6!=3) is 0 If you want to force the conversion of a value of one type into the corresponding value of another type, then you must "cast" the value by the following pattern: (DATA_TYPE) EXPRESSION You must cast one to machtch the type of the other if you want to compare two numbers of different types. ====================================================================== How to write conditional statements. When boolean expression of an if statement evaluates to any integer other than 0, JAVA considers the expression to be true and excecute the embedded statement. if (BOOLEAN_EXPRESSION) IF_TRUE_STATEMENT else IF_FALSE_STATEMENT Compound statement { statement_1 ... statement_n } The value-producing conditional-operator expression: BOOLEAN_EXPRESSION ? IF_TRUE_EXPRESSION : IF_FALSE_EXPRESSION -------------------------------------------------- import java.io.*; public class test6 { public static void main(String args[]) throws IOException { int change; System.out.println("Temparature change = "); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); change = Integer.parseInt(b.readLine()); System.out.print("The temperature has changed by "); System.out.print(change); System.out.println(change==1 ? " degree" : " degrees"); } } -------------------------------------------------- ====================================================================== How to write iterative statements. while (BOOLEAN_EXPRESSION) EMBEDDED_STATEMENT for (ENTRY_EXPRESSION; BOOLEAN_EXPRESSION; CONTINUATION EXPRESSION) EMBEDDED_STATEMENT If you want to increment or decrement the value of a variable by 1: ++VARIABLE_NAME --VARIABLE_NAME -------------------------------------------------- import java.io.*; public class test { public static void main(String args[]) throws IOException{ int i; for (i=0; i<5; i++) System.out.println(i); } } -------------------------------------------------- ====================================================================== How to work with arrays of numbers. -------------------------------------------------- import java.io.*; public class test7 { public static void main(String args[]) throws IOException{ int n = 5; double[] v= {3.0,4.0,5.0,6.0,7.0}; for (int i=0; i