140 likes | 284 Views
COSC236.237 Introduction to Computer Science II. Introduction to Java Programming Language. May 2014 Kyung Eun Park. Contents. Java key facts. Primitive Data T ypes Conditional (if) Statements Loops User Input/Output: Scanner and File classes String Arrays Methods
E N D
COSC236.237 Introduction to Computer Science II Introduction to Java Programming Language May 2014 Kyung Eun Park
Contents Java key facts • Primitive Data Types • Conditional (if) Statements • Loops • User Input/Output: Scanner and File classes • String • Arrays • Methods • Class as a Program • Class as an Object Class • Class as a Module
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 booleanisOdd; boolean isChar=true;
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’; }
3. Loops • for loop for (int i=0; i<list.length; i++) { list[i]++; } • while loop while (input.hasNext()) { name = input.next(); } • do … whileloop sum = 0; do { sum = sum + num % 10; num = num/10; } while (num>0);
4. User Input/Output(1) • File and Scanner 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”)); … }
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()
5. String • 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") returnsfalse 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 strlike “324”. String numStr= “324”; int numInt = Integer.parseInt(numStr); // 324 double numDbl = Double.parseDouble(“1234.32”); // 1234.32
6. Arrays • Syntax import java.util.*; … type[] name = new type[length]; type[] name = {value1, value2, … , valuen}; • 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);
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 typename(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 typename(parameters) { statements; }
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.
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.
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);