670 likes | 922 Views
IDS 201 Introduction to Business Programming - Java (Fall 2014). Syllabus. About this course http://kzhang6.people.uic.edu/teaching/ ids201_f14/ syllabus.html Lab sessions Assignments Tests and Final Exam Grading Tentative Schedule. Time Lecture session: MWF 12:00-12:50 PM
E N D
IDS 201Introduction to Business Programming - Java (Fall 2014)
Syllabus • About this course • http://kzhang6.people.uic.edu/teaching/ids201_f14/syllabus.html • Lab sessions • Assignments • Tests and Final Exam • Grading • Tentative Schedule
Time • Lecture session: MWF 12:00-12:50 PM • Lab session: M 11:00 – 11:45 AM • Location • Lecture: Room2BH 208 • Lab: Room: 2SCE 408 • Contact • TA: PouyaRahmati • Email: prahma2@uic.edu • Office: UH 2401 • Office hours: Monday 3:00 – 5:00 PM
Why Java? • Covers all characteristics of high-level languages • Platform independent • Easy to implement web development • Widely used for mobile development • Has been adopted in many distributed environments (Hadoop) • Graph user interface (GUI) design
Introduction to Computer Systems • Major hardware components of a computer
Megabytes, Gigabytes, Terabytes • 1 Bit = 1 binary digit • 8 Bits = 1 Byte • 1000 Bytes = 1 Kilobyte (Kb) • 1000 Kb = 1 Megabyte (Mb) • 1000 Mb = 1 Gigabyte (Gb) • 1000 Gb = 1 Terabyte (Tb)
CPU Two-level Caching System • The first level is on-chip cache (8,192 bytes). • The next level is between on-chip cache and main memory (from 32,768 to 1 megabyte).
How to Execute Programs • Computers can only recognize 0s and 1s. • Translate into languages understood by machines • Compiler • only translates once • Most high-level languages: C, C++, etc. • Interpreter: translates every time you run programs • Most scripting languages: python, perl, etc.
How Java Works JRE: java JDK: javac *.java *.class • javacand java can be found in the directory of bin/ of JDK you installed. • *: your filename.
Java Virtual Machine (JVM) Platform Independent JVM
What Java Can Do • Science and Technology • Popular machine learning libraries are written in Java: Mahout, Weka • Web development • Most web backend are developed using Java • Mobile application development • Android development platform • Graphical User Interface: menu, windows, … http://oreilly.com/catalog/javanut/examples/
Fundamental Blocks of Java Programs • Data • Variables • Each variable has values and corresponding type • E.g. intvar_a = 5; double var_b = var_a * 0.5 = 2.5; • Instructions • Control structure, Subroutine • Sequential flow • Branches: if…else…, switch • Loops: while, for… • Sub-functions
if-else Example public class IfElseDemo { public static void main(String[] args) { inttestscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } } The output will be ''Grade = C'' (no quotes)
switch example public class SwitchDemo { public static void main(String[] args) { intday= 4; String dayString; switch (day) { case 1: dayString= “Sunday"; break; case 2: dayString= “Monday"; break; case 3: dayString= “Tuesday"; break; case 4: dayString= “Wednesday"; break; case 5: dayString= “Thursday"; break; case 6: dayString= “Friday"; break; case 7: dayString= “Saturday"; break; default: dayString= "Invalid day"; break; } System.out.println(dayString); } } In this case, Wednesday is printed to standard output.
Loop example public class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } } public class ForDemo { public static void main(String[] args){ for(inti=1; i<11; i++){ System.out.println("Count is: " + i); } } } Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10
Subroutine Example public class TestMax { /** Main method */ public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); System.out.println("The maximum between " + i+ " and " + j + " is " + k); } /** Return the max between two numbers */ public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } } Output: The maximum between 5 and 2 is 5
Hello World Example public class HelloWorld{ public static void main(String[] args){ // subroutine Stringvar = “Hello World”; // variable and type System.out.println(var); // instruction } } • “Texts in red are reserved keywords in Java” • Save this file as HelloWorld.java. The filename must be the same as the class name (###case sensitive###). • Compiling: javacHelloWord.java (It will generate a HelloWorld.class file in the same directory as HelloWorld.java) • Running: java HelloWorld (NOT HelloWorld.class) • It will output “Hello World” in the screen. (no quotes)
Object-Oriented Programming (OOP) • Top-down design • Bottom-up design • Module Design - Information hiding/protection
Top-down Design Personal Online Bank System Manage Account Log-in System Manage Password Create Account View Balance Pay Credit Card Transfer
Bottom-up Design … … Print String Validity Check … Simple Math Calculator … Create Account Balance Operation … Log-in System Manage Account … Personal Online Banking System
More Examples about Objects and Classes • Class: car • Properties: speed, brake, color, price… • Methods: speedup, applyBrake, stop… • Objects: BMW, Benz, Toyota, Honda, Ford… • Class: school • Name, number_of_students, number_of_professors, address, president, … • Methods: teach, recruit, homecoming … • Objects: UIC, UIUC, Uchicago, Northwestern, IIT…
Encapsulation publicclassTwoPointClass{ privatedoublestarting_point_x; privatedoublestarting_point_y; privateString color; publicvoid draw(){ // some statements here; } publicvoidprintObject(){ // print out the object to screen; } // some other methods; }
Inheritance publicclass Line extends TwoPointObject{ privatedouble length; // additional attribute privatebooleanhasArrow; // additional attribute publicdoublegetLength(){ // additional method // get the length of this object; return length; } // some other additional methods; }
Polymorphism • Different objects can respond to the same message in different ways—is called polymorphism. • TwoPointObject rectangle= newRectangle(); // generate a rectangle object; • TwoPointObject line= new Line(); // generate a line object; • rectangle.print(); // will print out a rectangle; • line.print(); // will print out a line;
Interface • There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts.
Interface • In the Java programming language, an interface is a reference type, similar to a class • It can contain only constants, method signatures, and nested types. • There are no method bodies. • Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
Example public interface OperateCar{ // constant declarations, if any method signatures // An enum with values RIGHT, LEFT intturn(Direction direction, double radius, double startSpeed, double endSpeed); intchangeLanes(Direction direction, double startSpeed, double endSpeed); intsignalTurn(Direction direction, booleansignalOn); intgetRadarFront(double distanceToCar, double speedOfCar); intgetRadarRear(double distanceToCar, double speedOfCar); ...... // more method signatures } The method signatures have no braces and are terminated with a semicolon.
How to use Interface To use an interface, you write a class that implements the interface. When an instantiableclass implements an interface, it provides a method body for each of the methods declared in the interface. public class OperateBMW760i implementsOperateCar { // the OperateCar method signatures, with implementation – intsignalTurn(Direction direction, booleansignalOn) { // code to turn BMW's LEFT turn indicator lights on // code to turn BMW's LEFT turn indicator lights off // code to turn BMW's RIGHT turn indicator lights on // code to turn BMW's RIGHT turn indicator lights off } // all other methods declared in the interface, // other own members, for example, helper method not visible to clients of the interface }
IDE: Eclipse • An integrated development environment • Editing, compiling, running, debugging, etc. • Easy to use, user-friendly interface • Syntax checking, auto-completion, etc. • … • Download and install on your computers http://www.eclipse.org/downloads/
Identifier • An identifier in Java can be a variable name, function/subroutine/method name, class name, or object name. • It is a sequence of one or more characters. It must begin with a letter or underscore and must consist entirely of letters, digits, and underscores. • No space is allowed in identifiers. • Case-sensitive: Upper case and lower case letters are considered to be different. • Some reserved words in Java can not be used for other purposes. (e.g. public, class, int, static, main, if, while, …)
Valid or Non-valid? • N • abc • 201ids • _ids • _201ids • Hello World • helloWorld • hElloWORLD • A_long_string_name • public • public_static
Camel Case • When a name / an identifier is made up of several words, such as HelloWorld or interestRate, it is customary to capitalize each word, except possibly the first; this is sometimes referred to as camel case.
Compound Names • Consist of several ordinary names separated by periods. (Compound names are also called qualified names. e.g. System.out.println) • A compound name is a kind of path to an item through one or more levels of containment.
Variable • A name used to refer to data stored in memory is called a variable. • Can store different types of data. • Integer, real number, string, objects… • Data can change. Physical address Name variable1 00101110 00101111 00110000 variable2 … …… variable3 10111010 ……
Declare and Define Variables • Declaration means creating a primitive or object reference variable, but with no assignment of value or object respectively. • int x; // declaration of x of type int • Car myCar; // declaration of myCar an object of type Car • Definition is when we assign a value or a object to it. • int x = 5; • Car myCar = new Car();
Assignment • Two ways to assign a value to a variable. • Declare a variable first and then do assignment int x; x = 5; • Define a variable • int x = 5;
Assignment • Two steps to assign values to variables ⟨variable⟩ = ⟨expression⟩; Calculate the expression value Assign the calculated value to the variable value = 2*3; value = 5; value = value + 12;
Types and Literals • 8 primitive types • byte, short, int, long • float, double • char • boolean • Any data value stored in the computer’s memory must be represented as binary numbers: 0, 1. • One single 0 or 1 is called one bit. • 1 byte = 8 bits Integers Real Numbers Single Character One of Two Logic Values
Binary Number Each digit"1" in a binary number represents a power of two, and each "0" represents zero (index starts from 0, direction: right to left) 0001 is 2 to the zero power, or 1 0010 is 2 to the 1st power, or 2 0100 is 2 to the 2nd power, or 4 1000 is 2 to the 3rd power, or 8. 0111 = 0 + 4 + 2 + 1 = 7 An N-bit two's-complement numeral system can represent every integer in the range −(2N− 1) to +(2N− 1 − 1)
Real Numbers • The maximum value for a float is about 1038. A float can have about 7 significant digits. (So that 32.3989231134 and 32.3989234399 would both have to be rounded off to about 32.398923 in order to be stored in a variable of type float. • A double takes up 8 bytes, can range up to about 10308, and has about 15 significant digits. • Usually, we use double for real numbers.
char • It occupies 2 bytes in memory. • A character in a program must be surrounded by single quotes. ‘A’, ‘*’,’_’, etc. • The quotes are not part of the value and are not stored in the variables.
Literals • A name for a constant value is called a literal. • Character literals • ‘A’, ‘*’, ‘\t’: tab, ‘\n’: linefeed, ‘\’’: single quote, ‘\\’: backslash • Real literals • double: 1.2, 1.2e3 :1.2*103 • float: 1.2F • Integer literals • Ordinary integers such as 1000 and -32 are literals of type byte, short, or int, depending on their size. You can make a literal of type long by adding “L” as a suffix.