package Week02.day0715;
class Programmer{
private int age;
private Project now;
private Project[] history; //name 하나 더 넣어도 됨.
private String sname;
private int count;
Programmer(int age, String sname, Project now, Project[] history) {
this.age = age;
this.now = now;
this.history = history;
this.sname = sname;
}
void joinProject(Project p) { // 현재 진행중인 project를 설정 now
this.now = p;
}
void addProjectHistory(Project p) throws Exception { //project 경력을 추가 hitory에 추가
this.history[5] = p;
if(count > 4) {
throw new Exception("Project Hisoty는 5개까지 추가가능합니다.");
}
}
void getNowProject() { // 현재 Project를 리턴한다.
System.out.println(sname + "님의 현재 진행중인 프로젝트는 : " + now.getName() + " 입니다.");
}
void printProjectHostory() { //Project 경력을 출력한다 (print로 출력.)
System.out.println(sname + "님의 현재 경력은 : ");
for(int i = 0; i<history.length; i++) {
System.out.println("프로젝트명 : " + history[i].getName() + ", 프로젝트기간 : " + history[i].getMonth() + "개월");
}
}
void getTotalHistory() { //모든 Project의 총 경력의 합을 리턴한다(x년 x월)
int sum=0;
for(int i = 0; i<history.length; i++) {
sum+=history[i].getMonth();
}
if(sum/12==0)
System.out.println(sname + "님의 총 경력기간은 : " + sum/12 + "년 0개월 입니다.");
else
System.out.println(sname + "님의 총 경력기간은 : " + sum/12 + "년 " + sum%12 + "개월 입니다.");
}
}
class Project{
private String name;
private int month;
private String company;
Project(String name, int month, String company){
this.name = name;
this.month = month;
this.company = company;
}
public Project() {
}
String getName() { //Project명 리턴
return name;
}
void setName(String name) { //Project명 설정
this.name = name;
}
int getMonth() { // Project 수행기간을 월로 리턴 // month를 sum해서 월로 리턴.
return month;
}
void setMonth(int month) { // Project 수행기간
this.month = month;
}
}
public class 실습5일차 {
public static void main(String[] args) throws Exception {
Project[] p = new Project[6];
p[0] = new Project("마케팅", 5, "마케팅컴퍼니");
p[1] = new Project("빅데이터", 3, "빅데이터컴퍼니");
p[2] = new Project("소셜커머스", 6, "소셜커머스컴퍼니");
p[3] = new Project("인공지능", 8, "인공지능컴퍼니");
p[4] = new Project("AI", 10, "AI컴퍼니");
Project p1 = new Project();
p1 = new Project("배민뽀개기", 24, "딜리버리히어로코리아");
Programmer pg = new Programmer(32, "박홍준", p1, p);
pg.getNowProject();
pg.joinProject(p1);
pg.addProjectHistory(p1);
pg.printProjectHostory();
pg.getTotalHistory();
}
}
--------------------------------------------------------------------------
package Week02.day0715;
interface Campus{
void meal();
void goToSchool();
void takeClass();
}
class Teacher implements Campus{
private int id;
private String name;
private String major;
Teacher(int id, String name, String major){
this.id = id;
this.name = name;
this.major = major;
}
@Override
public void meal() {
System.out.println(name + " 교수님이 밥을 먹는다.");
}
@Override
public void goToSchool() {
System.out.println(name + " 교수님이 출근한다.");
}
@Override
public void takeClass() {
System.out.println(name + " 교수님이 강의를 한다.");
}
public void print() {
System.out.println("이름 : " + name + ", 담당전공 : " + major + ", 사번 : " + id);
}
}
class UniversityStudent implements Campus{
private int id;
private String name;
private String major;
UniversityStudent(int id, String name, String major){
this.id = id;
this.name = name;
this.major = major;
}
@Override
public void meal() {
System.out.println(name + "가 밥을 먹는다.");
}
@Override
public void goToSchool() {
System.out.println(name + "가 학교를 간다.");
}
@Override
public void takeClass() {
System.out.println(name + "가 수업을 듣는다.");
}
public void doHomework() {
System.out.println(name + "가 과제를 한다.");
}
public void partTime() {
System.out.println(name + "가 알바를 한다.");
}
public void print() {
System.out.println("이름 : " + name + ", 학과 : " + major + ", 학번 : " + id);
}
}
public class CampusLife {
public static void main(String[] args) {
Teacher tc = new Teacher(123456, "홍길동", "컴퓨터공학");
System.out.println("=======교수의 생활========");
tc.meal();
tc.goToSchool();
tc.takeClass();
System.out.println("=======교수의 정보========");
tc.print();
System.out.println();
System.out.println("=======학생의 생활========");
UniversityStudent[] st = new UniversityStudent[3];
st[0] = new UniversityStudent(1234567, "홍자동", "경영학과");
st[1] = new UniversityStudent(2234567, "홍수동", "행정학과");
st[2] = new UniversityStudent(3234567, "홍영동", "식품영양학과");
st[0].meal();
st[1].goToSchool();
st[2].takeClass();
System.out.println("=======학생의 정보========");
st[0].print();
st[1].print();
st[2].print();
}
}
--------------------------------------------------------------------------
/* 자료구조 */
1. 여러 개의 원소를 한 묶음으로 묶어 줄 수 있는 객체
2. Collection Framework
- interface : 각 collection에 있어야하는 연산의 정의
- implements : 각 collection이 실제 어떤 자료구조를 이용하여 표현했는가에 따라 collection 의 종류가 달라짐
- Algorithm : 각 collection 마다 유용하게 사용할 수 있는 메소드
package Week02.day0715;
import java.util.Random;
public class RandomEx2 {
public static void main(String[] args) {
Random rand = new Random();
int[] number = new int[100];
int[] counter = new int[10];
for(int i = 0; i<number.length; i++) {
// System.out.println(number[i] = (int)(Math.random()*10));
// 0<=x<10 범위의 정수 x를 반환한다.
System.out.println(number[i] = rand.nextInt(10));
}
System.out.println();
for(int i=0; i<number.length; i++) {
counter[number[i]]++;
}
for(int i=0; i<counter.length; i++) {
System.out.println(i + "의 개수 : " + printGraph('#', counter[i]) + " " + counter[i]);
}
}
public static String printGraph(char ch, int value) {
char[] bar = new char[value];
for(int i=0; i<bar.length; i++) {
bar[i] = ch;
}
return new String(bar);
}
}
--------------------------------------------------------------------------
package Week02.day0715;
public class RandomEx4 {
final static int RECORD_NUM = 10; //생설할 레코드의 수를 정한다.
final static String TABLE_NAME = "TEST_TABLE";
final static String[] CODE1 = {"010", "011", "017", "018", "019"};
final static String[] CODE2 = {"남자", "여자"};
final static String[] CODE3 = {"10대", "20대", "30대", "40대","50대"};
public static void main(String[] args) {
for(int i =0; i<RECORD_NUM; i++) {
System.out.println("INSERT INTO " + TABLE_NAME
+ " VALUES("
+ " '" + getRandArr(CODE1) + "'"
+ ", " + getRandArr(CODE2) + "'"
+ ", " + getRandArr(CODE3) + "'"
+ ", " + getRand(100, 200) //100~200 사이의 값을 얻는다.
+ ");" );
}
}
public static String getRandArr(String[] arr) {
return arr[getRand(arr.length-1)]; //배열에 저장된 값 중 하나를 반환한다.
}
public static int getRand(int n) {
return getRand(0, n);
}
public static int getRand(int from, int to) { //100~200 사이의 값을 얻는 로직.
return (int)(Math.random()*(Math.abs(to-from)+1)) + Math.min(from, to);
}
}
--------------------------------------------------------------------------
package Week02.day0715;
public class RandomWrapperTest {
public static void main(String[] args) {
//Random메소드
//Math.random()도 내부적으로 Random클래스의 인스턴스 생성해서 사용
//아래의 두문장은 동일
//double randNum = Math.random();
//double randNum = new Random().nextDouble();
/* 1. Random()
* 현재시간을 seed값으로 이용하는 Random 생성
*
* 2. Random(long see)
* 매개변수 seed를 종자값으로 하는 Random 생성
*
* 3. boolean nextBoolean()
* boolean type의 난수 발생
*
* 4. void nextBytes(byte[] bytes)
* Bytes배열에 byte타입의 난수를 채워서 반환
*
* 5. double nextDouyble()
* double타입의 난수를 반환, 0.0<=x<1.0
*
* 6. float nextFloat()
* float타입의 난수를 반환, 0.0<=x<1.0
*
* 7. int nextInt()
* Boolean타입의 난수를 반환
*
* 8. int nextInt(int n)
* int타입의 난수를 반환
*
* 9.long nextLang()
* Lang타입의 난수를 반환
*
* 10. void setSeed(long seed)
* seed변경
*/
//Wrapper
//변수를 객체로 만들 필요가 있을때 사용.
//사용시기
// 1. 매개변수로 객체가 요구 될때.
// 2. 기본형 값이 아닌 객체로 저장해야 할 때.
// 3. 객체간의 비교가 필요할 때. 등등
/*1.문자열->기본형
* byte b = Byte.parseByte("100");
* short s = Short.parseShort("100");
* int i = Int.parseInt("100");
* long l = Long.parseLong("100");
* float f = Float.parseFloat("100");
* double d = Double.parseDouble("3.14");
*
* 2. 문자열 -> Wrapper Class
* Byte b = Byte.valueOf("100");
* Short s = Short.valueOf("100");
* Int i = Int.valueOf("100");
* Long l = Long.valueOf("100");
* Float f = Float.valueOf("100");
* Double d = Double.valueOf("3.14");
*/
Integer i = new Integer(100);
Integer i2 = new Integer(100);
System.out.println("i==i2 ? " + (i==i2)); // false
System.out.println("i.equals(i2) ? " + i.equals(i2)); //true
System.out.println("i.compareTo(i2)=" + i.compareTo(i2)); //0
System.out.println("i.toString()=" + i.toString()); //100
System.out.println("MAX_VALUE=" + Integer.MAX_VALUE); //2147483647
System.out.println("MIN_VALUE=" + Integer.MIN_VALUE); //-2147483648
System.out.println("SIZE=" + Integer.SIZE+" bits"); // 32
System.out.println("BYTES=" + Integer.BYTES + " bytes"); // 4
System.out.println("TYPE=" + Integer.TYPE); //int
int i3 = 10;
//기본형을 참조형으로 형변환(형변환 생략가능)
Integer intg = (Integer)i3; // Integer intg = Integer.valueOf(i3);
Object obj = (Object)i3; // Object obj = (Object)Integer.valueOf(i3);
Long lng = 100L; // Long lng = new Long(100L);
int i4 = intg + 10; //참조형과 기본형간의 연산 가능
long l = intg + lng; // 참조형 간의 덧셈도 가능
Integer intg2 = new Integer(20);
int i5 = (int)intg2; //참조형을 기본형으로 형변환도 가능(형변환 생략가능)
Integer intg3 = intg2 + i5;
System.out.println("i3 = " + i3); //10
System.out.println("intg = " + intg); //10
System.out.println("obj = " + obj); //10
System.out.println("lng = " + lng); //100
System.out.println("intg = " + intg); //10
System.out.println("intg + 10 = " + i4); // 20
System.out.println("intg + lng = " + l); // 110
System.out.println("intg2 = " + intg2); // 20
System.out.println("i5 =" + i5); // 20
System.out.println("intg2 + i5 = " + intg3); // 40
}
}
--------------------------------------------------------------------------
package Week02.day0715;
import java.util.Scanner;
class Add{
private int a, b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a+b;
}
}
class Mul{
private int a, b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a*b;
}
}
class Sub{
private int a, b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a-b;
}
}
class Div{
private int a, b;
public void setValue(int a, int b) {
this.a = a;
this.b = b;
}
public int calculate() {
return a/b;
}
}
public class 계산기예제 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("두 정수와 연산자를 입력하시오>>");
int a = sc.nextInt();
int b = sc.nextInt();
char operator = sc.next().charAt(0); //연산자를 문자로 변환
switch(operator) {
case '+':
Add add = new Add();
add.setValue(a, b);
System.out.println(add.calculate());
break;
case '-':
Sub sub = new Sub();
sub.setValue(a, b);
System.out.println(sub.calculate());
break;
case '*':
Mul mul = new Mul();
mul.setValue(a, b);
System.out.println(mul.calculate());
break;
case '/':
Div div = new Div();
div.setValue(a, b);
System.out.println(div.calculate());
break;
default:
System.out.println("잘못된 연산자입니다.");
}
}
}
--------------------------------------------------------------------------
package Week02.day0715;
public class 피라미드예제 {
public static void main(String[] args) {
new 피라미드예제();
}
public 피라미드예제() {
int max = 50;
for(int i = 0; i <max; i++) {
String print = "";
for(int j = max; j>0; j--) {
if(i>=j) {
print+="*";
} else {
print+=" ";
}
}
for(int j = 0; j<max; j++) {
if(i<=j) {
print+=" ";
} else {
print+="*";
}
}
System.out.println(print);
}
}
}