휴먼교육센터 개발자과정 25일차

조하영·2022년 9월 13일
0

자바는 객체지향 언어. 객체를 부품으로이해할 수 있다.
예) 자동차의 부품: 타이어, 엔진, 시동을 키는 부품등
자동차를 움직이려면 시작이 되는 부품이 있다.(키박스)
자바로 비유하면 부품을 객체로 이해할 수 있고 이 객체는 클래스가 정의한다.
부품은 설계도를 기반으로 만든다. 이런 설계도를 의미하는 것이 클래스.
프로그램이 구동되면서 램에 만들어놓고 사용한다.
객체를 만드는 명령어 = new
new MemberADM(); // 생성자: 클래스 이름과 동일한 메써드

객체 정의
클래스
1.전역변수 vs 지역변수
전역변수: 클래스 영역 전체에서 사용하는 변수
지역변수: 메서드 내에서 사용하는 변수, for문에서 사용하는 변수, 특정블럭에서 사용

2.메서드 정의하고 사용하기
예전에 함수는 미리 정해놓은 기능. 호출(매개변수 전달)

예제

프로젝트 명: HumanShop
패키지명: human
클래스명 : Main- 메인클래스
PointADM - 포인트 관리 클래스

PointADM
전역변수 : temp 배열: 사이즈3, 정수형
메서드명: public void prt()- temp배열의 모든 내용 출력

위와 같이 프로젝트를 생성하고 프로그램이 실행되면 PointADM생성자가 호출되고 생성자가 다시 prt()메서드를 호출하도록 하시오.

package human;

public class Main {

public static void main(String[] args) {
	new PointADM();
}

}

package human;

import java.util.Scanner;

public class PointADM {// 클래스는 전역변수와 메서드로 정의
String[] temp = new String[10];// 전역변수 선언

PointADM() {// 메서드 생성자
	menu();
//		tempInput(); 
//		prt(); // prt 메서드 호출
}

public void menu() {
	for (;;) {
		System.out.println("1. 입력 ");
		System.out.println("2. 전체출력 ");
		System.out.println("3. 프로그램 종료 ");
		Scanner in = new Scanner(System.in);
		System.out.println("메뉴 선택");
		int a = in.nextInt();
		in.nextLine();
		if (a == 1) {
			tempInput();
		} else if (a == 2) {
			prt();
		} else if (a == 3) {
			System.out.println("프로그램 종료 ");
			break;
		}
	}
}

public void prt() {// 출력하는 기능을 수행하는 메서드. 호출이 없으면 메서드는 실행되지 않음.
	for (int i = 0; i < temp.length; i++) {
		System.out.println(temp[i]);
	}
}

public void tempInput() {
	Scanner in = new Scanner(System.in);// ctrl + shift + o
	System.out.println("단어를 입력하세요.");
	String newWord = in.nextLine();
	//int a = in.nextInt();
	in.nextLine();

	for (int i = 0; i < temp.length; i++) {
		if(temp[i]==null) {
			temp[i] = newWord;
			break;
		}
	}
}

}

기본자료형 : 변수에 저장된 값이 실제 사용할 데이터
Int, double, char, boolean

참조자료형 : 변수에 저장된 값이 실제 데이터가 있는 곳의 주소를 참조
String, 객체정의 클래스
->여러개의 변수가 하나의 객체를 공유하는 의미
->메모리의 용량을 아낄 수 있다.
->기타 컴퓨터의 자원을 아낄 수 있다.
->초기값으로 NULL을 가짐.

오후 실습

프로젝트명: ZooZoo
패키지명: loveZoo
클래스 :Main
ZooADM

  1. 동물의 이름을 저장하는 1차원 배열 선언
  2. 다음 메서드 구현
    1. 메뉴
    2. 입력
    3. 출력
    4. 삭제
    5. 수정
    6. 이름검색

메인클래스

package loveZoo;

public class Main {

public static void main(String[] args) {
	System.out.println("동물관리 프로그램 시작");
	new ZooADM();
	System.out.println("동물관리 프로그램 종료");
}

}

ZooADM 클래스

package loveZoo;

import java.util.Scanner;

public class ZooADM {
String[] temp = new String[10];// 전역변수 선언

ZooADM() {// 메서드 생성자
	menu();

// tempInput();
// prt(); // prt 메서드 호출
}

public void menu() {
	for (;;) {
		System.out.println("----메뉴----");
		System.out.println("1. 입력 ");
		System.out.println("2. 출력 ");
		System.out.println("3. 삭제 ");
		System.out.println("4. 수정 ");
		System.out.println("5. 이름검색 ");
		System.out.println("6. 종료 ");
		System.out.println("1~6번까지 원하는 메뉴의 숫자를 입력하세요. ");
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		in.nextLine();
		if (a == 1) {
			tempInput();
		} else if (a == 2) {
			prt();
		} else if (a == 3) {
			del();
		} else if (a == 4) {
			update();
		} else if (a == 5) {
			search();
		} else if (a == 6) {
			break;
		}
	}
}

public void prt() {// 출력하는 기능을 수행하는 메서드. 호출이 없으면 메서드는 실행되지 않음.
	for (int i = 0; i < temp.length; i++) {
		if (temp[i] != null) {
			System.out.println(i + ":" + temp[i]);
		}
	}
}

public void tempInput() {
	Scanner in = new Scanner(System.in);// ctrl + shift + o
	System.out.println("동물이름을 입력하세요.");
	String newAni = in.nextLine();
	// int a = in.nextInt();

	for (int i = 0; i < temp.length; i++) {
		if (temp[i] == null) {
			temp[i] = newAni;
			break;
		}
	}
}

public void del() {
	Scanner in = new Scanner(System.in);
	System.out.println("삭제할 동물의 번호를 입력하세요.");
	int a = in.nextInt();
	in.nextLine();
	temp[a] = null;
}

public void update() {
	Scanner in = new Scanner(System.in);
	System.out.println("수정할 동물의 번호를 입력하세요.");
	int a = in.nextInt();
	in.nextLine();
	System.out.println("새로운 동물이름을 입력하세요.");
	String updateAni = in.nextLine();
	temp[a] = updateAni;
}

public void search() {

	Scanner in = new Scanner(System.in);
	System.out.println("검색할 동물이름을 입력하세요.");
	String searchAni = in.nextLine();
	int cnt=0;
	for (int i = 0; i < temp.length; i++) {
		if (searchAni.equals(temp[i])) {
			System.out.println(i + ":" + searchAni);
			cnt++;
			break;
		}
	}
	if(cnt==0) {
		System.out.println(searchAni+"(이/가) 없습니다.");
	}
}

}

profile
공부하는 개발자

0개의 댓글