접근제어
public : 클래스 외부에서 접근이 가능하다.
private : 클래스 내부에서만 접근이 가능하다.
default : 동일 패키지에 있는 다른 클래스에서 접근 가능하다.
protected : 동일 패키지의 다른 클래스와 다른 패키지의 하위클래스에서도 접근 가능하다.
데이터 캡슐화
모든 데이터 멤버를 private으로 만들고 필요한 경우에 public한 get/set 메서드를 제공한다.
이렇게 하면 객체가 제공해주는 메서드를 통하지 않고서는 객체 내부의 데이터에 접근할 수가 없다.
이것은 data encapsulation혹은 information hiding이라고 부른다.
앞서 나왔던 프로젝트에 데이터필드와 메서드를 알맞은 지정자를 지정한 후
getter setter를 이용해 보았다.
package section2;
public class Term3 {
private int coef;//계수
private int exp;//차수
public Term3(int c, int e) {
coef = c;
exp = e;
}
public int calTerm(int x) {
return (int)(coef * Math.pow(x, exp));
}
public void printTerm() {
System.out.print(coef + "x^" +exp);
}
public int getCoef() {
return coef;
}
public void setCoef(int coef) {
this.coef = coef;
}
public int getExp() {
return exp;
}
}
package section2;
public class Polynomial3 {
private char name; //다항식의 이름
private int nTerms = 0; // 항의 개수
private Term3[] terms; //항들의 배열
public Polynomial3() {
nTerms=0;
terms = new Term3[100];
}
public Polynomial3(char name) {
this.name = name;
nTerms=0;
terms = new Term3[100];
}
public char getName() {
return name;
}
public int calcPolynomial(int x) {
int result = 0;
for (int i =0; i< nTerms; i++) {
result += (terms[i].calTerm(x));
}
return result;
}
public void printPolynomial() {
for(int i =0; i<nTerms; i++) {
terms[i].printTerm();
System.out.print("+");
}
System.out.println();
}
public void addTerm(int c, int e) {
int index = findTerm(e);
if(index != -1) {
terms[index].setCoef(terms[index].getCoef() + c);
}
else {
int i = nTerms-1;
while(i >= 0 && terms[i].getExp() < e) {
terms[i+1] = terms[i];
i--;
}
terms[i+1] = new Term3(c,e);
nTerms++;
}
}
private int findTerm(int e) {
for(int i=0;i<nTerms && terms[i].getExp() >= e;i++) {
if(terms[i].getExp() ==e) {
return i;
}
}
return -1;
}
}
package section2;
import java.util.Scanner;
public class code11{
Polynomial3 polys[] = new Polynomial3 [100];
int n = 0;
public void procesCommand() {
Scanner kb = new Scanner(System.in);
while(true) {
System.out.print("$ ");
String command = kb.next();
if(command.equals("create")) {
char name = kb.next().charAt(0);
polys[n] = new Polynomial3(name);
n++;
}
else if(command.equals("add")) {
char name = kb.next().charAt(0);
int index = find(name);
if(index==-1) {
System.out.println("No such Polynomial exist");
}
else {
int c = kb.nextInt();
int e = kb.nextInt();
polys[index].addTerm(c,e);
}
}
else if(command.equals("calc")) {
char name = kb.next().charAt(0);
int index = find(name);
if(index==-1) {
System.out.println("No such Polynomial exist");
}
else {
int x = kb.nextInt();
int result = polys[index].calcPolynomial(x);
System.out.println(result);
}
}
else if(command.equals("print")) {
char name = kb.next().charAt(0);
int index = find(name);
if(index==-1) {
System.out.println("No such Polynomial exist");
}
else {
polys[index].printPolynomial();
}
}
else if(command.equals("exit")) {
break;
}
}
kb.close();
}
public static void main(String[] args) {
code11 app = new code11();
app.procesCommand();
}
private int find(char name) {
for(int i =0;i<n;i++) {
if(polys[i].getName() == name) {
return i;
}
}
return -1;
}
}
'알고리즘 with 자바 > 자료구조' 카테고리의 다른 글
상속 2 (0) | 2021.06.29 |
---|---|
상속 1 (0) | 2021.06.29 |
static 그리고 public 1 (0) | 2021.06.28 |
메서드와 생성자 3 (0) | 2021.06.24 |
메서드와 생성자 2 (0) | 2021.06.24 |