Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment,

Similar presentations


Presentation on theme: "Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment,"— Presentation transcript:

1 Basic Java Programming CSCI 392 Week Two

2 Stuff that is the same as C++ for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment, decrement, etc result *= factorial--; switch statements switch (answer) { case 'y': case 'Y': some stuff break; default: }

3 Java Statements Not in C++ Labeling blocks of code outerloop: for (int i=0;…) for (int j=0; … if (something_bad) break outerloop; Exception Handling try { value = x/y; } catch (ArithmeticException exceptvar) { System.out.println("Math Error"); }

4 Basic Data Types Boolean boolean - 1 bit Integers byte- 8 bits short- 16 bits int- 32 bits long- 64 bits Characters char- 16 bits Floats float double Strings String still be careful when comparing

5 Boolean - So what? This C++ feature is not legal in Java: int factorial, result = 1;... while (factorial) result *= factorial--; But neither is this common C++ error: if (count = 10) // not legal Java

6 Variable Declaration Oddities You must initialize a variable before it can be used, so most declarations include an initialization. You can set a "constant's" value at run time. final int count = getCount(); Arrays require both a definition and declaration. int[] myarray; myarray = new int[25]; int[] myarray = new int[25]; char[] name = {'B', 'o', 'b'};

7 Type Checking You can assign small sized variables into bigger variables, but big variables don't fit into small variables. int X = 99; long Y = 0; Y = X; // legal X = Y; // not legal X = (int) Y; // legal

8 Comments Just like C /* multi-line */ // single line Except… /** document comment **/ not thrown away by compiler

9 Simple Output Printing to "Standard Output" System.out.print ("Hello " + name ); System.out.println (); Formatted Output NumberFormat layout = NumberFormat.getNumberInstance(); layout.setMaximumFractionDigits(4); double x = 12.3, y = 45.6; String outstr = layout.format(x/y); System.out.println("x/y = " + outstr);

10 Input - not so simple import java.io.* ; public class array_test_1 { public static void main(String[] args) throws Exception { InputStreamReader stdio = new InputStreamReader(System.in); BufferedReader instream = new BufferedReader (stdio); int count; System.out.print ("Enter an integer: "); String indata = instream.readLine(); count = Integer.parseInt (indata); for (int i=0; i<count; i++) System.out.print (i + " "); System.out.println(); } improvement: catch the NumberFormatException


Download ppt "Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment,"

Similar presentations


Ads by Google