//PersonStreamTest – text file read using BinaryStream, Reader
public class PersonStreamTest {
public static void load1(String filename) {
FileInputStream fis;
try {
fis = new FileInputStream(filename);
System.out.println("load1 file import: " + filename);
int c = 0;
while ((c = fis.read()) != -1) {
System.out.println((char)c);
}
fis.close();
System.out.println("load1 successfully");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void load2(String filename) {
FileReader fr;
try {
fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
System.out.println("load2 file import: " + filename);
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
fr.close();
System.out.println("load2 successfully");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void load3(String filename) {
FileReader fr;
try {
fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
System.out.println("load3 file import: " + filename);
String delimiter = ",";
String line = "";
while ((line = br.readLine()) != null) {
try {
// use comma as separator
String[] items = line.split(delimiter);
String name = items[0];
int age = Integer.parseInt(items[1]);
double weight = Double.parseDouble(items[2]);
double height = Double.parseDouble(items[3]);
Gender gender = items[4].equals("FEMALE") ? Gender.FEMALE : Gender.MALE;
// add Person into person array
Person p = new Person(name, age, weight, height, gender);
System.out.println(p);
}
catch (NumberFormatException e) {
System.out.println("Error(parseInt or parseDouble)!");
}
}
br.close();
fr.close();
System.out.println("load3 successfully");
} catch (FileNotFoundException e) {
System.out.println("Error! FileNotFoundException");
} catch (IOException e) {
System.out.println("Error! IOException");
}
}
public static void main(String[] args) {
String filename = "PersonList.csv";
// command line arguments
if (args.length >= 1) {
filename = args[0];
System.out.println("args0=" + filename);
}
load1(filename); // using binary stream
load2(filename); // using text file reader & buffered reader
load3(filename); // using text file reader & buffered reader & parse
}
}
ImageTest
// ImageTest – try/catch, throw
public class ImageTest {
// method
static void print(String filename) {
// read image & find image info
BufferedImage image = null;
try {
image = ImageIO.read(new File(filename));
int width = image.getWidth();
int height = image.getHeight();
String ext = filename.substring(filename.lastIndexOf('.') + 1);
System.out.println("filename=" + filename);
System.out.println("width=" + width);
System.out.println("height=" + height);
System.out.println("format=" + ext);
} catch (IOException e) {
System.out.println(filename + " not found");
e.printStackTrace();
}
}
// method
static void print2(String filename) throws IOException {
// read image & find image info
BufferedImage image = null;
image = ImageIO.read(new File(filename));
int width = image.getWidth();
int height = image.getHeight();
String ext = filename.substring(filename.lastIndexOf('.') + 1);
System.out.println("filename=" + filename);
System.out.println("width=" + width);
System.out.println("height=" + height);
System.out.println("format=" + ext);
}
public static void main(String[] args) {
String filename = "dog1.jpg";
// command line arguments
if (args.length >= 1) {
filename = args[0];
System.out.println("args0=" + filename);
}
print(filename);
try {
print2(filename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exceptions
ArrayIndexOutOfBoundsException 배열의 범위를 벗어난 접근할 때 발생
ArithmeticException 산술 연산 오류 에 의해 발생 (예를 들어, 0으로 정수를 나누는 경우)
NullPointerException null 객체를 사용하려고 시도할 때 발생
ClassCastException 변환할 수 없는 클래스로 객체 변환을 시도할 때 발생
NumberFormatException String을 숫자(number)로 변환할 수 없을 때 발생 (예를 들어, “1.23”을 Integer 변환을 시도할 때)
IllegalArgumentException 메소드 인자(argument) 유형을 잘못 사용할 경우
InputMismatchException (Scanner 클래스) 잘못된 입력일 때 발생
IOException 입출력(IO) 오류에 의해 발생
FileNotFoundException 지정된 경로에서 파일을 찾지 못할 때 발생
lecture2
lecture2
java2-lecture2
recursive call
재귀호출 (recursive call) 예제 https://introcs.cs.princeton.edu/java/23recursion/
lab2
lab2 due by (9/17 23:59) submitted to e-learning (보고서 포함)
java2-lab2
static variable in java
Why are the static variable considered evil?
https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil%22
Global variables are bad
Polymorphism
Polymorphism (via overriding) vs Method Overriding vs Method Overloading
http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading
Abstract Class vs Interface
abstract class vs interface
(Image from http://alecture.blogspot.com/2011/05/abstract-class-interface.html)
abstract class (일반변수+일반메소드+추상메소드 형태) – 추상클래스는 공통부분(abstract)을 상속해서 구현하게 함.
interface (상수+추상메소드 형태) – 기능(abstract) 구현(implements)을 강제함으로써, 구현한 객체들에서 동일한 동작을 보장할 수 있음.
An abstract class is generally used as a building basis for similar classes. Implementation that is common for the classes can be in the abstract class.
An interface is generally used to specify an ability for classes, where the classes doesn’t have to be very similar.
https://beginnersbook.com/2013/05/abstract-class-vs-interface-in-java/
https://dzone.com/articles/java-interface-vs-abstract-class
https://www.guru99.com/interface-vs-abstract-class-java.html
Object-Oriented Programming (OOP)
Object − objects have states and behaviors.
Class – defines the grouping of data and code, the “type” of an object
Instance – a specific allocation of a class
Message – sent to objects to make them act
Method – a “function” that an object knows how to perform
Local Variables − variables defined inside methods, constructors or blocks
Instance Variables – variables within a class but outside any method (a specific piece of data belonging to an object)
Class Variables − variables declared within a class, outside any method, with the static keyword
Encapsulation – keep implementation private and seperate from interface
Polymorphism – different objects, same interface
Inheritance – hierarchical organization, share code, customize or extend behaviors