private constructor

private constructor는 정적 메소드와 속성 (static method & property)만 있는 경우 사용함.

public class Counter
{
private Counter() { }
public static int currentCount;
public static int IncrementCount() { return ++currentCount; }
}
class TestCounter
{
public static void main(String[] args)
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// Counter aCounter = new Counter(); // Error
Counter.currentCount = 100;
Counter.IncrementCount();
System.out.println(“count=” + Counter.currentCount);
}

} // Output: New count: 101

OOP

OOP

BankAccount – instance vs static member field & method

CarSedan – public vs protected vs private

PersonStudent – method overriding

ShapePolymorphism – abstract class & method

 

 

Person Array

Person[] pArray = new Person[5];

// 만약 Person 객체를 하나만 생성한 후 for문에서 공유해 사용할 경우
// 마지막으로 입력된 데이터로 모든 데이터값이 치환됨
Person p = new Person();
for (int i = 0; i < 5; i++) {
System.out.print(“\n\nEnter Person name : “);
Scanner input = new Scanner(System.in);
p.setName(input.nextLine()); // 입력정보
System.out.print(“\n\nEnter Person age [int] : “);
p.setAge(input.nextInt()); // 입력정보
p.Print();
pArray[i] = p; // 리스트에 들어간 모든 원소는 동일한 p
}

System.out.println(“pArray : ” + Arrays.toString(pArray));

Person[] pArray = new Person[5];
// 아래와 같이 for문 안에 Person p = new Person()와같이
// 새로운 객체를 생성해야 각자 다르게 입력된 정보가 들어가게 됨
for (int i = 0; i < 5; i++) {

Person p = new Person();

System.out.print(“\n\nEnter Person name : “);
Scanner input = new Scanner(System.in);
p.setName(input.nextLine()); // 입력정보
System.out.print(“\n\nEnter Person age [int] : “);
p.setAge(input.nextInt()); // 입력정보
p.Print();

pArray[i] = p; // 이때 p는 새로운 Person객체

}

System.out.println(“pArray2 : ” + Arrays.toString(pArray2));

 

Basic

2.Basic
•ArithmeticOperator (+ – * /) method
•TemperatureConverter (F->C or C->F) enum, method, switch, if/else, for, while, do/while, break, continue, try/catch
•BreakContinueTest – break, continue
•FactorialTest (팩토리얼 연산) – recursive call
•BankAccount – class, static/instance method/field
•CheckUserInput (사용자 입력) – do/while, continue, try catch
•Divisor (약수) – for

getUserInputIntegerBetween

static public int getUserInputIntegerBetween(int min, int max) {
   int value = 0;
   Scanner scan = new Scanner(System.in);
   do {
      System.out.printf("Please enter value [%d-%d]: ", min, max);
      try {
         value = scan.nextInt();
      }
      catch (Exception e) {
         System.out.printf("Error! Please re-enter!\n");
         scan.next();
         continue;
      }
   } while (value < min || value > max);
   return value;
}