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

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
CS 112 Introduction to Programming File as Input; Exceptions; while loops; Basic Arrays Yang (Richard) Yang Computer Science Department Yale University.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Using Java Class Library
Building Java Programs File Processing. 2 Input/output (I/O) import java.io.*; Create a File object to get info about a file on your drive. –(This doesn't.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Building Java Programs
Building Java Programs
Introduction to programming in java
Building Java Programs
Yanal Alahmad Java Workshop Yanal Alahmad
Building Java Programs
Building Java Programs
Building Java Programs
Computer Programming Methodology Input and While Loop
Chapter 5: Control Structures II
Building Java Programs
An Introduction to Java – Part I
Building Java Programs
Building Java Programs Chapter 6
Building Java Programs Chapter 6
Building Java Programs
Building Java Programs
Know for Quiz Everything through Last Week and Lab 7
Building Java Programs
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Computing Using Java
Building Java Programs
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Homework 9: Critters (cont.)
Building Java Programs
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4
Presentation transcript:

Introduction to Java Programming Language May 2015 Kyung Eun Park COSC Introduction to Computer Science II

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

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

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

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

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”)); … }

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

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(“ ”); //

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);

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

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

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

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