240 likes | 392 Views
IDS 201 Introduction to Business Programming (Fall 2013). Week1. Platform Independent. JRE: java. JDK: javac. *.java. *.class. j avac and java can be found in the directory of bin/. What Java Can Do. Science and Technology Data mining library: Mahout, Weka Web development
E N D
IDS 201Introduction to Business Programming (Fall 2013) Week1
Platform Independent JRE: java JDK: javac *.java *.class javac and java can be found in the directory of bin/
What Java Can Do • Science and Technology • Data mining library: Mahout, Weka • Web development • Mobile application development • Graphical User Interface: menu, windows, … http://oreilly.com/catalog/javanut/examples/
Fundamental Blocks of Java Programs • Data • Variables, Types • E.g. intvar_a = 5; intvar_b = var_a * 10; • 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 keywords in Java” • Save this file as HelloWorld.java. The filename must be the same as the class name. • Compiling and running • javac HelloWord.java • java HelloWorld • 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 }