** 실습 1113 **

class Card
{
	String kind;	//인스턴스 변수
	int number;
	
	static int width = 100;		//클래스변수 -> 객체생성안해도 된다. Card. 으로 호출함
	static int height = 250;	//클래스변수
}

public class MySample1114 {

	public static void main(String[] args) {
		
		//클래스
		System.out.println("Card.width = " + Card.width);	//문법이 맞게 호출함
		System.out.println("Card.height = " + Card.height);
		
		Card c1 = new Card();	//ㅇ
		c1.kind = "Heart";
		c1.number = 7;
		
		Card c2 = new Card();
		c2.kind = "Spade";
		c2.number = 4;
		
		System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + c1.width + ", " + c1.height + ")");
		System.out.println("c2은 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + c2.width + ", " + c2.height + ")");
		c1.number = 12;
		c2.number = 8;
		
		c1.width = 50;
		c2.width = 80;
		
		System.out.println("변경 후============================");
		
		System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + c1.width + ", " + c1.height + ")");
		System.out.println("c2은 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + c2.width + ", " + c2.height + ")");
		
	}

}

** 실습 1114_2 **

import java.util.Scanner;

class Math2
{
	//인스턴스 메서드
	int add(int a, int b)
	{
		System.out.println("add메서드 시작 : a = " + a + ", b = " + b);
		a = a + 10;
		b = b + 10;
		
		int result = a + b;
		
		System.out.println("add메서드 끝 : a = " + a + ", b = " + b);
		return result;
	}
}

public class MySample1114_2 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		
		//계산기 클래스
		System.out.print("정수 2개를 입력하세요.>");
		int a = scn.nextInt();
		int b = scn.nextInt();
		
		System.out.println("main 메서드 호출 전 : a = " + a + ", b = " + b);
		
		Math2 m = new Math2();
		int c = m.add(a, b);
		
		System.out.println("main 메서드 호출 후 : a = " + a + ",b = " + b + ", c = " + c);
        
	}
}

** 실습 1114_2 **

/* 1
 문제)두 정수를 입력받아 더하기, 빼기, 곱하기, 나누기 연산결과 출력.
 입력예)두 정수를 입력하세요.>5 3
 출력예)add(a, b) = 8
    subtract(a, b) = 2
    multiply(a, b) = 15
    divide(a, b) = 1.66666666666667(소수점은 실수로 표련 - double)
 단, 클래스는 Math3로 한다.
 */

import java.util.Scanner;

class Math3
{
	int add(int a, int b)
	{
		return a + b;
	}
	
	int subtract(int a, int b)
	{
		//항상 큰수에서 작은수를 뺄수있게 처리
		if(a > b)
			return a - b;
		else		//return을 하려면 모든경우가 들어가야한다 -> esle 안쓰면 오류난다.
			return b - a;	
	}
	
	int multiply(int a, int b)
	{
		return a * b;
	}
	
	double divide(int a, int b)
	{
		return (double)a / b;
	}
}

public class MySample1114_2 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
        
        System.out.print("두 정수를 입력하세요.>");
		int a = scn.nextInt();
		int b = scn.nextInt();
		
		Math3 m3 = new Math3();
		
		int i = m3.add(a, b);
		int j = m3.subtract(a, b);
		int k = m3.multiply(a, b);
		double l = m3.divide(a, b);
		
		System.out.println("add(a, b) = " + i);
		System.out.println("subtract(a, b) = " + j);
		System.out.println("multiply(a, b) = " + k);
		System.out.println("divide(a, b) = " + l);
		
		//변수 선언 안하고 바로 프린트
		System.out.println("add(a, b) = " + m3.add(a, b));
		System.out.println("subtract(a, b) = " + m3.subtract(a, b));
		System.out.println("multiply(a, b) = " + m3.multiply(a, b));
		System.out.println("divide(a, b) = " + m3.divide(a, b));   
	}
}

** 실습 1114_2 **

//위 문제를 인스턴스변수를 이용하여 더하기, 빼기, 곱하기, 나누기 연산결과 출력.

import java.util.Scanner;

class Math3
{
	int a, b;
	
	int add()
	{
		return a + b;
	}
	
	int subtract()
	{
		//항상 큰수에서 작은수를 뺄수있게 처리
		if(a > b)
			return a - b;
		else		//return을 하려면 모든경우가 들어가야한다 -> esle 안쓰면 오류난다.
			return b - a;	
	}
	
	int multiply()
	{
		return a * b;
	}
	
	double divide()
	{
		return (double)a / b;
	}
}

public class MySample1114_2 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
        
        	Math3 m3 = new Math3();
		
		System.out.print("두 정수를 입력하세요.>");
		
		int a = scn.nextInt();
		int b = scn.nextInt();
		m3.a = a;
		m3.b = b;
		
		// int a = scn.nextInt(); / m3.a = a; 합쳐서 사용
		m3.a = scn.nextInt();
		m3.b = scn.nextInt();
        
        	System.out.println("add(a, b) = " + m3.add());
		System.out.println("subtract(a, b) = " + m3.subtract());
		System.out.println("multiply(a, b) = " + m3.multiply());
		System.out.println("divide(a, b) = " + m3.divide());
	}
}

** 실습 1114_2 **

//위와 동일한 결과로 클래스 변수를 이용한 계산기
//Math3 클래스에대한 객체인스턴스 m1, m2에 대해서 정의한 후
//m1과 m2에서 각각 같은 변수를 공유한다는 의미로 출력
//클래스 변수병은 기존 소스를 수정하여 a, b 그대로 사용

import java.util.Scanner;

class Math3
{
	static int a, b;
	
	int add()
	{
		return a + b;
	}
	
	int subtract()
	{
		//항상 큰수에서 작은수를 뺄수있게 처리
		if(a > b)
			return a - b;
		else		//return을 하려면 모든경우가 들어가야한다 -> esle 안쓰면 오류난다.
			return b - a;	
	}
	
	int multiply()
	{
		return a * b;
	}
	
	double divide()
	{
		return (double)a / b;
	}
}

public class MySample1114_2 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		
		System.out.print("두 정수를 입력하세요.>");
		
		Math3.a = scn.nextInt();
		Math3.b = scn.nextInt();
		
		Math3 m1 = new Math3();
		Math3 m2 = new Math3();
		
		System.out.println("add(a, b) = " + m1.add());
		System.out.println("subtract(a, b) = " + m1.subtract());
		System.out.println("multiply(a, b) = " + m1.multiply());
		System.out.println("divide(a, b) = " + m1.divide());
		
		System.out.println("add(a, b) = " + m2.add());
		System.out.println("subtract(a, b) = " + m2.subtract());
		System.out.println("multiply(a, b) = " + m2.multiply());
		System.out.println("divide(a, b) = " + m2.divide());   
	}
}



 

** 스택  & 큐 **

 

* 스택

데이터를 일시적으로 저장하기위해 사용하는 자료구조로, 데이터이 입력과 출력순서는 후입선루 (LIFO.Last In First Out)

가장 나중에 넣은 데이터를 가장 먼저 꺼내는 구조.

 


** 실습 1114_3 **

public class MySample1114_3 {

	static void firstMethod()
	{
		System.out.println("firstMethod() start...");		//2
		secondMathod();
		System.out.println("firstMethod() end...");			//7
	}
	
	static void secondMathod()
	{
		System.out.println("secondMethod() start...");		//3
		thiredMathod();
		System.out.println("secondMethod() end...");		//6
	}
	
	static void thiredMathod()
	{
		System.out.println("thiredMathod start...");		//4
		System.out.println("thiredMathod end...");			//5
	}
	
	public static void main(String[] args) {
		
		//메서드 호출 순서 - 스택
		System.out.println("main start...");				//1
		firstMethod();
		System.out.println("main end...");					//8

	}

}

** 실습1114_3 **

/* 1
 문제)정수 2개를 입력받아 메서드 호출전과 후에 대한 결과를 출력하는 프로그램 작성
 입력예)정수 2개를 입력하세요.>5 10
 출력예)호출 전 a : 5, b : 10 (main메서드에서 출력)
    add 메서드 a : 10, b : 20 (add메서드에서 출력)
    결과 : 30 (main메서드에서 출력)
    호출 후 a : 5, b : 10 (main메서드에서 출력)
 */

import java.util.Scanner;

class Math4
{
	int add(int a, int b)
	{
		a = a * 2;
		b = b * 2;
		int result = a + b;
		System.out.println("add 메서드 a : " + a + ", b : " + b);
		return result;
	}
}

public class MySample1114_3 {
	public static void main(String[] args) {
		
		Scanner scn = new Scanner(System.in);
        
        	System.out.print("정수 2개를 입력하세요.>");
		int a = scn.nextInt();
		int b = scn.nextInt();
		
		System.out.println("호출 전 a : " + a + ", b : " + b);
		
		Math4 x  = new Math4();
		
//		x.add(a, b);
		System.out.println("결과 : " + x.add(a, b));
		System.out.println("호출 후 a : " + a + ", b : " + b);
		
	}

}

//위와 동일한 결과로 지역변수가 아닌 인스턴스 변수로 이용시.

import java.util.Scanner;

class Math4
{
	//인스턴스 변수 적용시
	int a, b;
	
	int add(int a, int b)
	{
		a = a * 2;
		b = b * 2;
		int result = a + b;
		System.out.println("add 메서드 a : " + a + ", b : " + b);
		return result;
	}
}
public class MySample1114_3 {
	public static void main(String[] args) {
		
		Scanner scn = new Scanner(System.in);
        
        		//위와 동일한 결과로 지역변수가 아닌 인스턴스 변수로 이용시.
		
		Math4 m = new Math4();		//인스턴스 변수쓰려고 객체생성 
		
		System.out.print("정수 2개를 입력하세요.>");
		m.a = scn.nextInt();
		m.b = scn.nextInt();
		
		System.out.println("호출 전 a : " +m. a + ", b : " + m.b);
		System.out.println("결과 : " + m.add(m.a, m.b));
		System.out.println("호출 후 a : " + m.a + ", b : " + m.b);

	}

}

//위와 동일할 결과로 지역변수나 인스턴스 변수가 아닌 클래스변수로 이용시.

import java.util.Scanner;

class Math4
{
	//클래스 변수만 이용하여 출력.
	
    static int a, b;
		
		int add()
		{
			a = a * 2;
			b = b * 2;
			int result = a + b;
			System.out.println("add 메서드 a : " + a + ", b : " + b);
			return result;
		}
}

public class MySample1114_3 {
	public static void main(String[] args) {
		
		Scanner scn = new Scanner(System.in);
        
        	System.out.print("정수 2개를 입력하세요.>");
		Math4.a = scn.nextInt();
		Math4.b = scn.nextInt();
		
		Math4 m = new Math4();
		
		System.out.println("호출 전 a : " + Math4. a + ", b : " + Math4.b);
		System.out.println("결과 : " + m.add(Math4.a, Math4.b));		
        	//m.add(m.a, m.b) 하면 버그남(m에 노란줄 뜬다.)
        	//m.add(Math4.a, Math4.b)에는 (Math4.a, Math4.b)가 아닌 값만 전달된다.
		System.out.println("호출 후 a : " + Math4.a + ", b : " + Math4.b);

	}

}

 

 

 

 

 

 

 

 

 

 

 

+ Recent posts