Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Programming Language May 2015 Kyung Eun Park COSC236.237 Introduction to Computer Science II.

Similar presentations


Presentation on theme: "Introduction to Java Programming Language May 2015 Kyung Eun Park COSC236.237 Introduction to Computer Science II."— Presentation transcript:

1 Introduction to Java Programming Language May 2015 Kyung Eun Park COSC236.237 Introduction to Computer Science II

2 Contents 1.Primitive Data Types 2.Conditional (if) Statements 3.Loops 4.User Input/Output: Scanner and File classes 5.String 6.Arrays 7.Methods 8.Class as a Program 9.Class as an Object Class 10.Class as a Module Java key facts 2

3 1. Data Types Primitive data types and variable declarations – int int numb; int max = -1000; int min = 1000; int x, y, z; – double double average; double balance=0.0; – char char letter; char ch=‘q’; – boolean boolean isOdd; boolean isChar=true; 3

4 2. Conditional (if) Statements if/else if (num%2==0) System.out.println(num+” is even number”); else System.out.println(num+” is odd number”); if/else if/else if (grade>=90) { grade = ‘A’; } else if (grade>=80) { grade = ‘B’; } else { grade = ‘C’; } 4

5 3. Loops for loop for (int i=0; i<list.length; i++) { list[i]++; } while loop while (input.hasNext()) { name = input.next(); } do … while loop sum = 0; do { sum = sum + num % 10; num = num/10; } while (num>0); 5

6 4. User Input/Output (1) 6 File and Scanner import java.io.*; import java.util.*; … File file = new File(“mydata.txt"); Scanner input = new Scanner(file); or Scanner input = new Scanner(new File(“mydata.txt”)); Methods canRead(),delete(),exists(),getName(),length(),renameTo(file) Usage File f= new File(“example.txt”); if (f.exists() && f.length() > 1000) { f.delete(); } Exception Handling public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File(“data.txt”)); … }

7 4. User Input/Output (2) Scanner import java.util.*; … Scanner input = new Scanner(new File("data.txt")); if (input.hasNext()) { String name = input.next(); } Scanner line = new Scanner(string); Methods hasNext(), hasNextInt(), hasNextDouble(), hasNextLine() next(), nextInt(), nextDouble(), nextLine() 7

8 5. String 8 Variable declarations String first_name = “Kyung Eun”; String last_name = “Park”; String name = last_name + “, “ + first_name; Methods String s = “hello”; charAt(index) s.charAt(1) returns "e" contains(text) s.contains("hi") returns false endsWith(text) s.endsWith("llo") returns true equals(text) s.equals("hello") returns true equalsIgnoreCase(text) s.equalsIgnoreCase("Hello") returns true indexOf(text) s.indexOf("o") returns 4 length() s.length() returns 5 startsWith(text) s.startsWith("hi") returns false substring(start, stop) s.substring(1,3) returns "el" toLowerCase() s.toLowerCase() returns "hello" toUpperCase() s.toUpperCase() returns "HELLO" Useful conversion methods Integer.parseInt(numStr) returns int value, 324 converted from str like “324”. String numStr = “324”; int numInt = Integer.parseInt(numStr); // 324 double numDbl = Double.parseDouble(“1234.32”); // 1234.32

9 6. Arrays 9 Syntax import java.util.*; … type[] name = new type[length]; type[] name = {value 1, value 2, …, value n }; Accessing array name[index] name[index] = value; name.length Methods binarySearch(), copyOf(), equals(), fill(), sort(), toString() Usage int days = 31; int[] temps = new int[days]; Arrays.sort(temps);

10 7. Methods Static method – Part of a class, rather than part of an object. – Not copied into each object; shared by all objects of that class. – No implicit parameter – Syntax public static type name ( parameters ) { statements ; } Instance method (or object method) – exists inside each object of a class and gives behavior to each object – has implicit parameter, this. – Syntax public type name ( parameters ) { statements ; } 10

11 8. Class as a Program Has a main and other static methods. Does not usually declare any static fields. (except final ) Works as a driver (test) of object class(es) Example: – GuessingGame, PointMain, BankMain, COSCDepartment, etc. 11

12 9. Class as an Object Class Defines a new type of objects. Declares object fields, constructor(s), and methods. Might declare static fields or methods, but these are less of a focus. Should be encapsulated Example: – Point, BankAccount, Date, Student, etc. 12

13 10. Class as a Module A module is a partial program, not a complete program. – no main() Used as utility code implemented as static methods Example: Math Syntax class. method ( parameters ); Usage int factorsOf24 = Factors.countFactors(24); 13


Download ppt "Introduction to Java Programming Language May 2015 Kyung Eun Park COSC236.237 Introduction to Computer Science II."

Similar presentations


Ads by Google