스마트 인재개발원/Java

2021-04-22 자바 클래스 객체 실습

package day22_2;

import java.util.Scanner;

public class CalculatorMain {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.print("1번째 정수를 입력하세요: ");
		int x = sc.nextInt();
		
		System.out.print("2번째 정수를 입력하세요: ");
		int y = sc.nextInt();
		
		
		Calculator cal = new Calculator(x,y);
		cal.setNum1(x);
		cal.setNum2(y);
		
		System.out.println(cal.sum());
		
		cal.setNum1(75);
		cal.setNum2(5);
		
		System.out.println(cal.sub());
		System.out.println(cal.mul());
		System.out.println(cal.div());
	}

}
package day22_2;

public class Calculator {

	private int num1;
	private int num2;
	
	
	public Calculator(int x, int y) {
		// TODO Auto-generated constructor stub
	}

	//메소드 작성
	public  void Calculator(int num1, int num2) {
		
	}
	
	public  void setNum1(int num1) {
		this.num1 = num1;
		
		
	}
	public  void setNum2(int num2) {
		this.num2 = num2;
	}
	public  int sum() {
		return (this.num1 +this.num2);
	}
	public  int sub() {
		return (this.num1 -this.num2);
	}
	public  int mul() {
		return (this.num1 *this.num2);
	}
	
	public double div() {
		return (this.num1 /this.num2);
	}

	
	
}
package day22_1;

public class Pockemon {
	
	//스스로 동작 X 설계만 진행하는 클래스 -> main메소드 사용 x
	//포켓몬 도감 설계도 -> 포켓몬 정보 저장 , 기능, 
	//포켓몬 정보 : 타입 - String, 이름 - String, 방어력-int, 공격력-int, 체력-int, 
	//생성자 메스드 필요
	public Pockemon(String name, String type, int shield, int attack, int hp) {
		
		this.name = name;
		this.type = type;
		this.shield = shield;
		this.attack = attack;
		this.hp = hp;
		
		
	}
	
	public Pockemon(String name, String type) {
		
		this.name = name;
		this.type = type;

		
		
	}
	//포켓몬 속성정의 
	//접근지정자 제한주기 -> 다른사용자에 의해서 정보가 수정되지 않도로 하기 위함.
	
	private String type;
	private String name;
	private int shield;
	private int attack;
	private int hp;
	 
	 //메서드 포켓몬의 속성을 조회할 수 있는 get 메서드 생성
	public  String getName() {
		return name; 
	}
	
	public String getType() {
		
		return type; 
	}
	
	public int getShield() {
		return shield;
	}

	public int getAttack() {
		return attack;
	}
	
	public int getHp() {
		return hp;
	}
	

}
package day22_1;

import java.util.ArrayList;

public class PockeMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.pockemon 클래스를 통하여 mon1 객체 생성!
		
//		Pockemon mon1 = new Pockemon();
//		
//		mon1.name = "파이리";
//		mon1.type = "불";
//		mon1.shield = 20;
//		mon1.attack = 50;
//		mon1.hp = 80;

		Pockemon mon1 = new Pockemon("파이리", "불", 20, 50, 80);
		Pockemon mon2 = new Pockemon("피카츄", "전기", 60, 80, 100);
		Pockemon mon3 = new Pockemon("꼬부기", "물", 50, 50, 50);
		Pockemon mon4 = new Pockemon("꼬부기", "물");
		
		 System.out.println("mon2의 이름 : "+mon2.getName() +" 타입 : "+mon2.getType());
		 System.out.println("mon3의 이름 : "+mon3.getName() +" 타입 : "+mon3.getType());
		 System.out.println("mon1의 이름 : "+mon1.getName() +" 타입 : "+mon1.getType());
		 
//		 mon2.type = "물";
		 
		 System.out.println();
		 System.out.println("mon2의 이름 : "+mon2.getName() +" 타입 : "+mon2.getType());

		 System.out.println();
		 //포켓몬을 담을 수 있는 가방 만들기 -) 배열사용
		 Pockemon[] pocke = new Pockemon[3];
		 pocke[0] = mon1;
		 pocke[1] = mon2;
		 pocke[2] = mon3;

		 
		 //반복문을 활용해서 한번에 포켓몬정보 가져오기?
		 for(int i = 0; i<pocke.length; i++) {
			 System.out.println("mon"+ (i+1) +"의 정보 : " + pocke[i].getName()  
					 			+ "," + pocke[i].getShield()
					 			+ "," + pocke[i].getAttack()
					 			+ "," + pocke[i].getHp() );
			 
		
		// 가변 리스트를 활용하여 사용자가 원하는 타입으로 생성하기
 
			 
		 }
		 
		// 가변 리스트를 활용하여 사용자가 원하는 타입으로 생성하기
		// import 작업 필요하다. 
		// 어떤타입으로 만들지가 중요하다. 타입 지정
		// 
		
		 ArrayList<Pockemon> pocke_list = new ArrayList<Pockemon>();
		 
		 //ArrayList .add()
		 

		 pocke_list.add(mon1);
		 pocke_list.add(mon2);
		 pocke_list.add(mon3);
		 
		 //ArrayList에 담긴 내용 확인하기 -> .get()
		 System.out.println();
		 for(int i=0; i<pocke_list.size(); i++) {
			 
			 System.out.println(pocke_list.get(i).getName());
			 
		 }
		 
		 
		
	}

}
package day22;

public class PersonMain {

	public static void main(String[] args) {
		Person person1 = new Person();
		Person person2 = new Person("최영재", 43);
		
		person1.setName("강다현");
		person1.setAge(20);
		
		System.out.println("Person1의 이름 : " + person1.getName() +" 나이 : " + person1.getAge() );
		System.out.println("Person2의 이름 : " + person2.getName() +" 나이 : " + person2.getAge());
		
	
	
	}

}
package day22;

public class Person {
	//사람의 이름과 나이, 행동들을 정리할 수 있는 클래스를 생성하자
	//클래스의 구조 -) 필드(속성)(데이터)와 메소드 (행동,로직)
	
	
	// 1. 필드 정의
	private String name;
	private int age;
	
	
	//생성자 메소드의 특징
	//1.리턴타입이 존재하지 않는다
	//생성자 메소드의 이름은 클래스의 이름과 동일하다.
	//default 생성자 메소드 
	//2.생성자 메소드의 오버로딩이 가능하다.
	//
	
	public  Person() {		
		
		
	}
	
	//Person 생성자 메소드 오버로딩 (중복정의)
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
		
		
	}
	
	public String getName() {
		//설계도에 저장되어 있는 name 리턴하는 기능
		int name2 = 33;
		return name;
	}

	public void setName(String name) {
		//메소드 호출시 작정되는 매개변수 데이터를 설계도에 저장하는 기능 
		this.name = name;
	}

	public int getAge() {
		//설계도에 저장되어 있는 age 리턴하는 기능
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
	
	
	
}