Java_Week_2

신태원·2020년 9월 8일
1

Java

목록 보기
2/7
post-thumbnail

Encapsulation - Information hiding이 굉장히 중요함.

package Figure;

public class Rectangle{
   private int x,y,h,w;
   
   public Rectangle(int x1, y1, h1, w1) //c++ 과 마찬가지고, 생성자
   {x = x1; y = y1; h = h1; w = w1;}
   
   public void moveTo(int x1, y1) 
   { x = x1; y =y1; }
   
   public void display() 
   {..//drawRectangle(x,y,h,w);}
   
};

윈도우 만들기

public class Window extends Rectangle //window 라는 클래스는 rectangle 클래스를 상복한다라는 뜻
{
   public void resize(int h1, w1)
   {
      h=h1; w=w1; display();
   }
}

Instance Variable

  • 객체가 가지고 있는 변수
  • Instance Variable은 자신만의 객체가 있음.(두 객체가 공유하는 변수가 아님)
  • 가능한한 private으로 설정하는 것이 좋음(encapsulated 때문에)

Encapsulation: get, set Methods

  • Method 앞에 붙여서 구분하기 쉽게 해주는것이 좋음.

Constructors

  • new라는 키워드와 함께 호출하고, 생성자를 선언할 때는 리턴 타입을 따로 설정하지 않음. 생성자를 설정하지 않아도 메소드 선언이 가능하고, parameter만 달리 해주면 여러개의 생성자 선언이 가능하다.

Primitive Type and Reference Types

  • Reference Type은 객체를 저장함.
  • Primitive Type은 boolean,byte, char, short, int ,long ,float, double 총 8개가 있음.
  • Reference Type은 다른곳에 있는 객체를 가리키는 포인터를 내부적으로 갖고있음. Primitive Type은 값을 직접 지정해줘야함.
  • 하지만 가끔 Reference Type Primitive Type을 객체로 사용해야하는 경우가 있기 때문에 wrapper class가 있음.

Java application에는 한개 이상의 main() 이 있어야 함.

  • 모든 함수는 어느 class안에 있어야 함.

Account class 정의하기

  • 저축할 금액을 외부에서 입력받고 잔고를 관리하며, 그 처리과정을 출력해주는 프로그램을 작성해보자.
  • 잔고 관리는 Account 객체를 이용한다.
  • module 이나 객체를 구동해서 뭔가 원하는 것을 testing 해주는 것을 driver라고 함.
public class Account
{
   private double balance;
   
   public Account( double initialBalance )
   {
      if( initialBalance > 0.0 )
         balance = initialBalance;
   }
   
   public void credit ( double amount )
   {
   balance = balance + amount;
   
   }
   
   public double getBalance()
   {
      return balance;
   }
}
   
package java1;
import java.util.Scanner;


public class AccountTest {

   public static void main(String[] args) {
	//System.out.print("Hello World");
      Account account1 = new Account (50.00);
      Account account2 = new Account (-7.53);
      
      System.out.printf("account1 balance: $%.2f\n",
      		account1.getBalance() );
      System.out.printf("account2 balance: $%.2f\n",
      		account2.getBalance() );
            
      Scanner input = new Scanner (System.in);
      double depositAmount;
      
      System.out.print("\nEnter deposit amount for account1: ");
      depositAmount = input.nextDouble();
      System.out.printf("\nsubtracting %.2f from account1 balance\n",
				depositAmount );
      account1.credit(depositAmount) ;
		
      System.out.printf("account1 balance: $%.2f\n",
				account1.getBalance() );
                
      System.out.printf("account2 balance: $%.2f\n",
				account2.getBalance() );
      System.out.print("\nEnter deposit amount for account2: ");
      depositAmount = input.nextDouble();
      System.out.printf("\nsubtracting %.2f from account2 balance\n",
				depositAmount );
      account2.credit(depositAmount) ;
      
      System.out.printf("account1 balance: $%.2f\n",
		account1.getBalance() );
		
      System.out.printf("account2 balance: $%.2f\n",
		account2.getBalance() );
	}

}

위 코드는 실습시간에 구현했던, 간단한 기능을 갖고 있는 ATM이다. 현재 텀프로젝트로
좀 더 복잡한 ATM을 만들고 있으며 추후에 추가할 예정이다.

만약 같은 폴더에 여러 파일이 있으면, 그것을 정리해주는 키워드가 package인 것같음. 이것에 대해서도 추후에 자세하게 공부를 하고 올리겠다.

Scanner method: nextLine(), next()

  • nextInt(): int 한게 읽을 때
  • nexDobule(): double 하나 읽을 때
  • nextLine(): 문장 하나 읽을 때
  • next(): 공백으로 분리되어 있는 단어 한개를 읽어올 때
  • %.2f 소수점 두번째까지 출력
profile
일단 배우는거만 정리해보자 차근차근,,

0개의 댓글