1 / 21

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Programming in JAVA – II (A Whirlwind Tour of Java). Course Lecture Slides 17 May 2010. Ganesh Viswanathan. JAVA. Java 1.0 was first released by Sun Microsystems in 1995. Java became popular for web programming.

isaura
Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides 17 May 2010 Ganesh Viswanathan

  2. JAVA Java 1.0 was first released by Sun Microsystems in 1995. Java became popular for web programming. Duke, the JAVA mascot "Write Once, Run Anywhere" (WORA) philosophy. • Key design principles: • Platform independence • Security • Stability

  3. How Java works!

  4. Programming in JAVA – I • Basic concepts in Java • Values • Variables • Complex data/objects • Methods • Expressions • Conditionals • Loops

  5. Programming in JAVA • English to JAVA translation: • Piece of data: value • Kind of data: type • Place to store a value: variable • Structured group of variables: object • Kind of object: class • Object of a particular class: classinstance • Variable belonging to an object: instancevariable

  6. Programming in JAVA • Values and Types: • All data values in Java have a type • The type of a value determines: • Allowed operations • Memory representation • Allowed operations • Conversion to other types • Allowed operations • Types are program invariants • Help to catch errors Default initial values (if omitted) are based on type

  7. Programming in JAVA • Primitive Types: • EIGHT basic pre-defined types: values on • which Java can operate directly. • byte, short, int, long, float, double, • boolean, char • Plus, special support for character strings- using java.lang.String class

  8. Programming in JAVA - II • Numeric Types in Java are characterized by their size (how much memory they occupy) • Integral types • Floating point types

  9. Strings • Built-in non-primitive type “String” • String s1 = “my string”; • Strings are sequence of characters • “UF” “Pugh Hall” string literals • + means concatenation • “Pugh” + “ ” + “Hall” => “Pugh Hall” • Automatic conversion of numbers to strings • “Go” + 2 + “UF” => Go2UF • Text in a String is immutable

  10. Variables • Variables store values • myScore = 100; • isRaining = true; • monthlySalary = 1656.89; • PI = 3.141592653589793; • myName = “Buzz Aldrin”; • All variables must be declared!

  11. Variables • Assignment statement “ = “ • int myScore = 100; • boolean isRaining = true; • float monthlySalary = 1656.89; • double PI = 3.141592653589793; • String myName = “Buzz Aldrin”; • Variables must store values of the correct type • myScore = true; • isRaining = 1; • Prevent nonsensical calculations

  12. Initializing Variables • Preferred usage: Combine declaration and • value initialization in one step: • String myCar = “Mustang”; • Boolean isRaining = true; • Default initial values (unless specified) are based • on type.

  13. Default type values • Default initial values (unless explicitly specified) • are based on type.

  14. Methods • A set of instructions referenced by a name that • can be called whenever required • public class Report{ • public static void main(String args[]){ • printHelloWorld(); • } • //simple print method • public static void printHelloWorld(){ • System.out.println(“Hello World!”); • } • } http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html

  15. Demystifying the main( ) method public staticvoidmain(String args[ ]) The keyword public indicates that the method can be called by any object. “The boss function!” : When you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main()method then calls all the other methods required to run your application. The keyword static indicates that the method is a class method, which can be called without the requirement to instantiate an object of the class. The keyword void indicates that the method doesn't return any value. String args[] - “an array of strings”: Mechanism through which the runtime system passes command line arguments to your application.

  16. “Parameters” vs “Arguments” • Parameters specify data that must be provided • in an invocation • public getName (String ufid, int year) { • … • searchUFID = ufid; • searchYear=year; • … • } • Arguments are the actual values supplied to a • method • String name = getName (“1234-5678”, 2010);

  17. Naming Conventions

  18. Java Expressions • Compute values of different types • Boolean values: 10>5 • Numeric values: 2+3 , 5%2 • String values: “Pugh”+” Hall” • Object values: new GradeBook(); http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html

  19. Control Flow Statements Use a condition to choose which action to take. • If, Else If, Else (Multi-way decisions, Nested decisions) • Switch-case • While, Do-While • For http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html

  20. Control Flow Statements • If, Else-If, Else (Multi-way decisions,Nested decisions) public String dominant() { String animal; if (isFrigid()) else animal = “cat"; return animal; } public String transport() { … if (isFrigid()) return "skis"; else if (isTropical()) return "surfboard"; else return "bicycle"; } { if (latitude > 0) animal = "polar bear"; else animal = "penguin"; }

  21. Get more info! (Online) Search keywords: • Java data types • Java basics • Floating point numbers • Java Class methods vs Instance methods • Java statements

More Related