티스토리 뷰

학습정리-10-18.txt
0.00MB

1.메소드 오버로딩에 대하여 설명하시오.

 같은 함수(void) 이름에 파라미터의 데이터 타입이나 갯수를 달리 하는 것.

다만 return 타입을 달리 하는 것은 메소드 오버로딩이 적용되지 않는다.

 

 

 


2.메소드 오버로딩을 적용한 대표적인 함수는?

함수 오버 로딩의 가장 대표적인 예가 System.out.println(); 이다.

println(); 은 자바 문법적으로 파라미터 안에 들어가는 데이터 형 별로 함수가 생성되어 있다.

 

 

 


3.this 함수에 대하여 설명하시오.

this 생성자(함수)는 자신의 객체 안에서 자기 자신을 호출한다.

파라미터 변수명과 인스턴스 변수명이 같을 경우 this.변수명을 통해 해당 클래스 안 인스턴스 변수를 지칭할 수 있다.

자기 자신의 인스턴스 함수를 호출할 때도 this.을 사용할 수 있다.

ex) this.radius = radius;

     this.setRadius();

 

 

 

 


4.this 의 용도는?

this 메소드를 이용하면 중복되는 코드를 줄일 수 있다.

또한 파라미터 변수명과 인스턴스 변수명이 같을 경우 this.변수명을 통해 해당 클래스 안 인스턴스 변수를 지칭할 수 있다.

 

 

 

 


5.스트링 객체를 생성하는 2가지 방법은 무엇인가?

String str1 = new String("Simple String");

String str2 = "The Best String";

다만 2가지 방법에는 차이점이 있는데 아래의 6번 문제를 참고하자.

 

 

 

 


6. 아래의 결과를 예측하고,이유를 설명하시오.

public class ImmutableString {

	public static void main(String[] args) {
		
		String str1 = "Simple String";
		String str2 = "Simple String";
		
		String str3 = new String("Simple String");
		String str4 = new String("Simple String");
		
        
		//참조변수의 참조 값 비교		
		if(str1 == str2) {
			System.out.println("str1과 str2는 동일 인스턴스 참조");
		} // 출력
		else {
			System.out.println("str1과 str2는 다른 인스턴스 참조");
		}
		
        
		//참조변수의 참조 값 비교		
		if(str3 == str4) {
			System.out.println("str3과 str4는 동일 인스턴스 참조");
		}
		else {
			System.out.println("str3과 str4는 다른 인스턴스 참조");
		} // 출력
		
	
	}	
}

참조형에 사용한 == 비교 연산자 비교 기준은 메모리 주소! (자바에서)

"  "안에 들어가 있는 문자열이 같으면 동일한 메모리 주소를 사용한다. 

즉, 먼저 메모리에 올라간 str1의 주소를 str2가 가져다 쓴다. 

왜냐하면 메모리를 절약하려고 같은 것이 있는지 확인하고 중복으로 올리지 않는다.

 

""안에 있는 문자열을 직접 비교하고 싶을 때는 .equals를 사용한다. (자바에서)

    if(str1.equals(str2)) {
                System.out.println("같은 글자입니다.");
            }
            else {
                System.out.println("다른 글자입니다.");
            }
 		// 같은 글자입니다.

 

 

 

 


7.immutable 에 대하여 설명하시오.

String 인스턴스는 Immutable 인스턴스이다. (불변)→ 원본 보전

		String str5 = str1 + str2;
		System.out.println(str5);
		// Simple StringSimple String
        
		System.out.println(str5 == str1);  // false
		System.out.println(str5 == str2);  // false

String 끼리 더해도 절대 원본은 수정하지 않는다. 새로운 str5를 메모리에 생성.

 

 

 

 


8.사용자로부터 받은 문자열(영문으로)에서 자음과 모음 개수를 계산하는 프로그램을 작성
입력:abcd 
출력:
총글자수는 4개
자음:3 개
모음:1 개

import java.util.Scanner;

class Word{
	private String word;
	
	public Word(String word) {
		this.word = word;
	}
	
	public void resultCV() {
		String vowel = "aeiouAEIOU";
		int count1 = 0;
		
		for(int i = 0; i < word.length(); i++) {
			for(int j = 0; j < vowel.length(); j++) {
				if(vowel.charAt(j) == word.charAt(i)) {
					count1++;
				}
							
			}
					
		}
		
		int count2 = word.length() - count1;
		
		System.out.println("자음 : " + count2 + "개");
		System.out.println("모음 : " + count1 + "개");
			
	}
	
	
}	


public class PracTest3 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		String str = sc.next();
		
		Word word = new Word(str);
		
		System.out.println("총 글자수는 " + str.length() + "개");
		
		word.resultCV();
		
	}	
}

 

import java.util.Scanner;

class ConsVowCount{
	private String word;
	private int consonant;
	private int vowel;
	
	public ConsVowCount(String word) {
		this.word = word;
		consonant = 0;
		vowel = 0;
	}
	
	private void count(char ch) {
		switch (ch) {
		case 'a': case 'e': case 'i': case 'o': case 'u':
		case 'A': case 'E': case 'I': case 'O': case 'U':	
			vowel++;
			break;
			
		
		default:
			consonant++;
		}
		
	}
	
	
	public void countResult() {
		for(int i = 0; i < word.length(); i++) {
			char ch = word.charAt(i);
			
			count(ch);	
		}
		
		System.out.println("총 글자 수 : " + word.length());
		System.out.println("모음 : " + vowel);
		System.out.println("자음 : " + consonant);
		
	}
		

	
}



public class ConsVowTest {

	public static void main(String[] args) {
		
		while(true) {
			Scanner sc = new Scanner(System.in);
			
			String word = sc.next();
			
			ConsVowCount cvc = new ConsVowCount(word);
			
			cvc.countResult();
			
			System.out.println("계속  Yes  /  중단  No");
			String YesOrNo = sc.next();
			
			if(YesOrNo.equals("yes") || YesOrNo.equals("YES")) {
				continue;
			}
			else {
				break;
			}			
			
		}
		
		System.out.println("종료입니다.");
		
	}

}

※ default package에 들어가 있는 파일은 import로 불러올 수가 없다!

 

 

 



9.사용자에게 받은 문자열을 역순으로 화면에 출력하는 프로그램을 작성하시오.
입력:abcde
출력:edcba

import java.util.Scanner;


public class PracTest2 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		String str = sc.next();
		
		for(int i = str.length() - 1; i >= 0; i--) {
			System.out.print(str.charAt(i));
		}
		
		
	}	
}

 

 

 

 



10.사용자로부터 키를 입력 받아서 표준 체중을 계산한 후에 사용자의 체중과 비교하여 
저체중인지, 표준 인지, 과체중인지를 판단하는 프로그램을 작성하라. 
표준 체중 계산식은 다음을 사용하라.
표준체중(kg) = ( 키(cm) - 100 ) * 0.9

입력:
키(cm)를 입력하세요. : 193
체중(kg)을 입력하세요. : 25
출력:
표준 체중은 83.7입니다.
당신은 저체중 입니다. 

import java.util.Scanner;

class Standard{
	double tall, weight;
	
	public Standard(double tall, double weight) {
		this.tall = tall;
		this.weight = weight;
	}
	
	public double stanWeight() {
		
		return (tall - 100) * 0.9;
			
	}
	
	public void resultW() {
		if(weight < stanWeight()) {
			System.out.println("당신은 저체중입니다.");
		}
		else if(weight > stanWeight()) {
			System.out.println("당신은 과체중입니다.");
		}
		else{
			System.out.println("당신은 표준체중입니다.");
		}
				
	}
	
	
	
}


public class PracTest4 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("키(cm)를 입력하세요.");
		double tall = sc.nextDouble();
		
		System.out.println("체중(kg)을 입력하세요.");
		double weight = sc.nextDouble();
		
		Standard stan = new Standard(tall, weight);

		System.out.println("표준 체중은 " + stan.stanWeight() + "입니다.");
		stan.resultW();
		
	}	
}

 

import java.util.Scanner;

class BMICalculator{
	private double height;
	private double weight;
	
	public BMICalculator(double height, double weight) {
		this.height = height;
		this.weight = weight;
	}
	
	public void printResult() {
		double mWeight = (height - 100) * 0.9;
		
		System.out.println("표준 체중은 " + mWeight + "입니다.");
		
		if(weight > mWeight) {
			System.out.println("당신은 과체중입니다.");
		}
		else if(weight < mWeight) {
			System.out.println("당신은 저체중입니다.");
		}
		else {
			System.out.println("당신은 표준체중입니다.");
		}
		
	}
	
}
	
	


public class BMITest {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("키(cm)를 입력하세요.");
		int height = sc.nextInt();
		
		System.out.println("체중(kg)을 입력하세요.");
		int weight = sc.nextInt();
		
		BMICalculator bmi = new BMICalculator(height, weight);
		
		bmi.printResult();
		
		
	}

}

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함