Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter06 : OOP 본문

JAVA

chapter06 : OOP

GAWON 2023. 5. 16. 17:39
1.1 객체지향 언어란(OOP:Object Oriented Programming)?
 - 부품에 해당하는 객체들을 먼저 만들고, 이것들을 하나씩 조립해서 완성된 프로그램을 만드는 기법
 - 특징으로는 캡슐화(Encapsulation), 상속(Inheritance), 다형성(Polymorphism)이 있다.

1.2 객체란(Object)?
 - 자신의 속성을 가지고 있는 다른 것과 식별 가능한 것
 - 자바에서는 이 속성들을 각각 필드(Field)와 메소드(Method)라고 부른다.
 - 클래스로부터 만들어진 객체를 해당 클래스의 인스턴스(instance)라고 한다.

1.3 객체 생성(인스턴스 생성)
 - 정의된 클래스를 이용하여 '객체'를 생성한다.
 - 생성 방법
	클래스명 객체명 = new 클래스명();
	클래스명 인스턴스명 = new 클래스명();
 - 객체(인스턴스) 사용방법
 	객체명.필드명
 	객체명.메소드명()

1.4 클래스(class)란?
 - 객체지향언어에서 '객체'를 만드는 도구이자 설계도 이다.
 - 객체(object), 인스턴스(instance)를 만드는 설계도이다.
 ex)   클래스		   객체
        사람		철수, 영희
     붕어빵기계		붕어빵
 - 구성
 ex) 	변수 > 필드(Field)
        함수 > 메소드(Method)

1.5 클래스 선언 규칙
 1) 하나 이상의 문자로 이루어져야 한다.
 	ex) Car, SportCar  (첫 글자 대문자로만 이루어져있다.)
 2) 첫 번째 글자는 숫자가 올 수 없다.
 	ex) Car(O), 3Car(X)
 3) '$', '_'외의 특수 문자는 사용할 수 없다.
 	ex) $Car(O), _Car(O), @Car(X)
 4) 자바 키워드는 사용할 수 없다.(예약어)
 	ex) int(x), for(x)
 5) 첫 번째 글자는 대문자로 사용하는 것이 원칙이다.
 	- 생성 및 실행에는 영향x

1.6 메인 클래스*
 - 메인 메소드를 가지고 있는 클래스이다.(중요)
 - 프로그램이 시작되는 클래스이다.
 - 프로그램의 이름이 되는 클래스이다.

1.7 기타*
 - 하나의 자바 파일에 클래스가 2개 이상인 경우 public 키워드는 파일명과 일치하는 클래스에만 명시하고,
 하나의 클래스만 public 키워드를 명시해야 한다.
Q1.
클래스 Circle
- 필드 : radius, PI, name
- 메소드 : info
클래스 CircleMain
- 메소드 : main
값을 대입해서
반지름, 이름, 크기(PI*R*R), 둘레(2*PI*R) 값 출력
package org.joonzis.test;



public class Circle {
	double radius;
	final double PI = 3.14;
	String name;
	
	void info() {
		System.out.println("반지름 : " +radius);
		System.out.println("이름 : " + name);
		System.out.println("크기 : " + (PI * radius * radius));
		System.out.println("둘레 : " + (PI * 2 * radius));
	}
}
--------------------------------------------------
package org.joonzis.test;

public class CircleMain {
	public static void main(String[] args) {
		
		Circle myCircle = new Circle();
		
		myCircle.radius = 12.3;
		myCircle.name = "지구";
		
		myCircle.info();
		
		
	}
}
Q2.
클래스 Rect
- 필드 : width, height
- 메소드 : init(너비, 높이 입력), info(너비, 높이, 크기(calcArea) 출력),
calcArea(w*h, 넓이계산(크기) 출력)
클래스 RectMain
- 메소드 : main
값을 입력 받아서 (Scanner) 확인
package org.joonzis.test;

import java.util.Scanner;


public class Rect {
	// 필드
	int width;
	int height;
	Scanner sc;
	
	// 메소드
	void init() {
		sc = new Scanner(System.in);
		System.out.print("너비 입력 >> ");
		width = sc.nextInt();
		System.out.print("높이 입력 >> ");
		height = sc.nextInt();
	}
	void info() {
		System.out.println("너비 : " + width);
		System.out.println("높이 : " + height);
		calcArea();
	}
	void calcArea() {
		System.out.println("크기 : " + (width * height));
	}
}
--------------------------------------------------
package org.joonzis.test;

public class RectMain {
	public static void main(String[] args) {
		
		Rect r1 = new Rect();
	
		r1.init();
		r1.info();
	}
}
Q3.
클래스 Triangle
- 필드 : width, height
- 메소드 : init(너비, 높이 입력), info(너비, 높이, 크기 출력), calcArea(w*h/2 넓이계산 후 리턴)
클래스 TriangleMain
- 메소드  : main
값을 입력 받아서 (Scanner) 확인
파일을 하나만 사용

package org.joonzis.test;

import java.util.Scanner;

//클래스 Student
//- 필드 :
//String name, String dept, String score1, String score2
//double average
//boolean isPass(합격 유무) : 평균 점수가 80점 이상 true, 80점 미만 false
//- 메소드 : 
//input : name, dept, score1, score2 콘솔입력, 평균 및 패스 유무 확인
//output : name, dept, average, isPass ("합격","불합격")
public class Student {
	// 필드
	String name, dept, score1, score2;
	double average;
	boolean isPass;
	
	// 메소드
	void input(Scanner sc) {
		System.out.print("이름 입력 >> ");
		name = sc.next();
		System.out.print("학과 입력 >> ");
		dept = sc.next();
		System.out.print("중간 점수 입력 >> ");
		score1 = sc.next();
		System.out.print("기말 점수 입력 >> ");
		score2 = sc.next();
		average = (Double.parseDouble(score1) + Double.parseDouble(score2)) / 2.0;
		isPass = average >= 80 ? true : false;
	}
	void output() {
		System.out.println("이름 : " + name);
		System.out.println("학과 : " + dept);
		System.out.println("평균 : " + average);
		System.out.println(isPass ? "합격" : "불합격");
	}
}
------------------------------------------------------
package org.joonzis.test;

import java.util.Scanner;

public class StudentMain {
	public static void main(String[] args) {
		Student stu = new Student();
		
		// 1.
		Scanner sc = new Scanner(System.in);
		stu.input(sc);
		
		// 2.
		//stu.input(new Scanner(System.in));
		
		stu.output();
	}
}
Q4.
클래스 Student
- 필드 :
String name, String dept, String score1, String score2
double average
boolean isPass(합격 유무) : 80점 이상 true, 80점 미만 false
- 메소드 :
input : name, dept, score1, score2 콘솔입력, 평균 및 패스 유무 확인
output : name, dept, average, isPass ("합격","불합격")
클래스 StudentMain
- 메소드 : main

'JAVA' 카테고리의 다른 글

chapter08 : constructor  (0) 2023.05.16
chapter07 : method  (1) 2023.05.16
chapter05 : memory  (0) 2023.05.16
chapter05 : array  (1) 2023.05.16
chapter04 : control_statement  (0) 2023.05.16