Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC 201: Computer Programming I B. S. Afolabi 1 Introduction 3 unit course 2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm 3 hours of practical/week To be determined next week Course Lecturers: Dr. A. O. Ajayi Dr. F. O. Asaiah Mrs. H. O. Odukoya Dr. B. S. Afolabi (Coordinator) Dr. P. A. Idowu Dr. A. R. Iyanda Mr. I. P. Gambo Mr. Okegbile Miss Lawal 2 Textbooks No textbook is required For programming beginners: Java Software Solutions: Foundations of Program Design, John Lewis and William Loftus, Publisher: Addison Wesley, 2002 For experienced programmers: — Learning the JavaTM Language at http://docs.oracle.com/javase/tutorial/ / — Thinking in Java, Bruce Eckel, Prentice Hall Software is required Download Java onto your home machine http://www.oracle.com/technetwork/java/javase/downloads /index.html Follow the instructions to install it Then, follow the instructions to install Eclipse https://eclipse.org/downloads/ Functional versus imperative languages Functional languages are ideal for expressing the functional (the problem to be solved) component of any problem however... at least 50% of all programs deal with input/output rather than a problem and functional languages aren’t very good at input/output. Think of the programs you use now: editor mail language translator() web browser Functional programming should have taught you to appreciate concise elegant programs. The Computer Central Processing Unit (CPU) Memory Input / Output Devices CPU Instructions z=x+y Read location x Read location y AddWrite to location z Programming Languages Easier to understand than CPU instructions Needs to be translated for the CPU to understand it JAVA “Most popular” language Runs on a “virtual machine” (JVM) More complex than some (eg. Python) Simpler than others (eg. C++) Compiling Java Source Code (.javac ) javac Byte Code (.class) java First Program class Hello { public static void main(String[] arguments){ //Program execution begins here System.out.println("Hello world."); } } Program Structure class CLASSNAME public static void main(String[] arguments){ STATEMENTS } } Output System.out.println(some String) outputs to the console Example: System.out.println(“output”); Second Program class Hello2 { public static void main(String[] arguments){ //Program execution begins here System.out.println("Hello world."); //Print once System.out.println(“Line number 2."); // Again } } Types Kinds of values that can be stored and manipulated. boolean: Truth value (true or false). int: Integer (0, 1, -47). double: Real number (3.14, 1.0, -2.1). String: Text (“hello”, “example”). Variables Named location that stores a value of one particular type. Form: TYPE NAME; Example: String foo; Assignment Use “=“ to give variables a value. Example: String foo; foo = “IAP 6.092”; Assignment Can be combined with a variable declaration. Example: double badPi = 3.14; boolean isJanuary = true; class Hello3 { public static void main(String[] arguments){String foo = "IAP 6.092"; System.out.println(foo);foo = "Something else"; System.out.println(foo); } } Operators Symbols that perform simple computations Assignment: = Addition: + Subtraction: Multiplication: * Division: / Order of Operations Follows standard math rules: Parentheses Multiplication and division Addition and subtraction class DoMath { public static void main(String[] Arguments){ double score = 1.0 + 2.0 * 3.0; System.out.println(score); score = score / 2.0; System.out.println(score); } } class DoMath { public static void main(String[] Arguments){ double score = 1.0 + 2.0 * 3.0; System.out.println(score); double copy = score; copy =copy/ 2.0; System.out.println(copy); System.out.println(score); } } String Concatenation (+) String text = "hello" + " world"; text = text + " number " + 5; // text = "hello world number 5" Examples 1 class Comment { // This is a one-line comment; it extends to the end of the line /* This is a delimited comment, extending over several lines */ int /* A delimited comment, extending over part of a line */ x = 117; } 25 Examples 2 class Layout { // Class declaration int a; Layout(int a) { this.a = a; } // One-line constructor body int sum(int b) { // Multi-line method body if (a > 0) // If statement return a + b; // Single statement else if (a < 0) { // Nested if-else, block statement int res = -a + b; return res * 117; } else { // a == 0 // Terminal else, block statement int sum = 0; for (int i=0; i<10; i++) // For loop sum += (b - i) * (b - i); return sum; } } 26 …Examples 2 static boolean checkdate(int mth, int day) { int length; switch (mth) { // Switch statement case 2: // Single case length = 28; break; case 4: case 6: case 9: case 11: // Multiple case length = 30; break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: length = 31; break; default: return false; } return (day >= 1) && (day <= length); } } 27 Example 4 class Scope { ... // void m1(int x) { // declaration of parameter x (#1) ... // x #1 in scope } // ... // void m2(int v2) { // ... // x #5 in scope } // ... // void m3(int v3) { // ... // x #5 in scope int x; // declaration of variable x (#2) ... // x #2 in scope } // ... // 28 …Example 4 void m4(int v4) { // ... // x #5 in scope { // int x; // declaration of variable x (#3) ... // x #3 in scope } // ... // x #5 in scope { // int x; // declaration of variable x (#4) ... // x #4 in scope } // ... // x #5 in scope } // ... // int x; // declaration of field x (#5) ... // x #5 in scope } 29 30