자바 프로그래밍 여섯 번째 수업

김형우·2022년 10월 31일
0

Java

목록 보기
6/22
post-thumbnail

1.객체란 무엇인가?

변수와 메소드로 정의된 클래스로 생성되어 메모리 상에
존재하는 클래스의 인스턴스를 뜻한다.

2. 아래의 클래스에 대하여, 메모리 그림을 그리시오.

Rectangle rec = new Rectangle();
==========================
public class Rectangle {
int height;
int width;

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getArea() {
return width * height;
}

}

임의의 번지에 메모리가 할당되며, 변수나 메소드에 상관없이 4byte씩 할당된다.

3.클래스와 객체의 차이는 무엇인가?

클래스는 객체를 위한 설계도, 객체는 클래스로 생성된 인스턴스이다.

4.아래의 프로그램을 작성하시오.

1 부터 num 까지 합을 구하는 class 를 작성하도록 하시오.

class Sum{
	
    int sum = 0;
    
    public int getSum(int num){
    	for(i=1; i<=num; i++){
        	sum+=i;
        }
        return sum;
    }
}

5.아래의 클래스를 작성하시오.

StraPrint strPrint = new StarPrint();

strPrint.printTriangle(3);
System.out.println();
strPrint.printReverseTriangle(3);

Class StarPrint{

	int num1;

public PrintStar(int num1) {
	this.num1 = num1;
}

public void printTriangle(int number) {
	for (int i = 1; i <= number; i++) {
		for (int j = 1; j <= i; j++) {
			System.out.print("*");
		}
		System.out.println();
	}
}

public void printReverseTriangle(int number) {
	for (int i = number; i >= 1; i--) {
		for (int j = 1; j <= i; j++) {
			System.out.print("*");
		}
		System.out.println();
	}
  }
}

출력 결과

*
**
***

***
**
*

6.생성자란 무엇인가?

클래스의 이름과 같은 함수이며, 
객체를 생성할때 원하는 값으로 초깃값을 설정해주는 함수를
생성자라고 한다.

7.디폴트 생성자란 무엇인가?

객체가 생성될때, 사용자가 초깃값을 설정해주지 않으면 컴파일러가
자동으로 생성해주는 생성자를 뜻한다.
디폴트 생성자는 사용자로부터 인수를 전달받지 않으므로, 매개변수를 가지지 않는다.

8.생성자의 용도에 대하여 설명하시오.

객체의 생성과 함께 초깃값을 설정해주기 위함이다.

9.null 에 대하여 설명하시오.

null은 참조형 타입의 기본 value이다.
모든 기본형 타입이 초깃값을 가지듯이, 참조형 타입은
기본적으로 null(비어있는)값을 가지는 것이다.

10.자바의 명명 규칙에 대하여 설명하시오.

  • 클래스
  • 항상 대문자로 시작하고 단어의 시작문자마다 대문자를 붙여준다
    ex) class ForExample
  • 메소드와 변수
  • lowerCamelCase를 사용한다.
  • 상수
  • 모든 문자를 대문자로 작성한다
    ex) final int RAINBOW_COLOR : 7;
  • camel case
  • 낙타의 등을 닮았다고 해서 명명되었으며,
    종류에는 lowerCamelCase와 UpperCamelCase가 있다.
    lowerCamelCase는 연결된 단어중 첫 단어만 모두 소문자로 한 뒤 다음 단어부턴 첫 문자에 대문자를 삽입하며,
    UpperCamelCase는 연결된 단어 모두 첫 문자에 대문자를 삽입하는 표기법을 말한다.
  • snake case
  • 언더바(_)가 뱀을 닮았다고 해서 명명되었으며,
    camel case와 같이 lower case, upper case 둘다 사용가능하다.
    사용 방법은 연결된 단어에 단어의 사이마다 언더바를 붙여주는 것이다.
    ex) java_programming_day_6

    11. 아래를 프로그래밍 하시오.

    노래 한 곡을 나타내는 Song 클래스를 작성하라.
    Song은 다음 필드로 구성된다.

    • 노래의 제목을 나타내는 title
    • 가수를 나타내는 artist
    • 노래가 발표된 연도를 나타내는 year
    • 국적을 나타내는 country

    또한 Song 클래스에 다음 생성자와 메소드를 작성하라.

    • 생성자 2개: 기본 생성자와 매개변수로 모든 필드를 초기화하는 생성자
    • 노래 정보를 출력하는 show() 메소드
    • main() 메소드에서는 1978년, 스웨덴 국적의 ABBA가 부른 "Dancing Queen"을

    song 객체로 생성하고 show()를 이용하여 노래의 정보를 다음과 같이 출력하라.
    1978년 스웨덴국적의 ABBA가 부른 Dancing Queen

    소스코드 Song.java

    public class Song {
    
    String title;
    String artist;
    int year;
    String country;
    
    	Song() {}
    
    	Song(String title, String artist, int year, String country) {
    
    	this.title = title;
    	this.artist = artist;
    	this.year = year;
    	this.country = country;
    }
    
    public void show() {
    	System.out.println(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
    	}
    }

    12.아래가 프로그래밍이 되도록 Grade 를 완성하시오..

      Grade me = new Grade(90, 70, 100);
      System.out.println("평균은 "+me.average());
     
      Class Grade {
      	int score1, score2, score3;
        double average;
        
      	double average() {
        	return (score1 + score2 + score3)/3;
        }
      }
      

    13.자바 클래스를 작성하는 연습을 해보자.

    다음 main() 메소드를 실행하였을 때 예시와 같이 출력되도록 TV 클래스를 작성하라.

    public static void main(String[] args) {
    TV myTV = new TV("LG", 2017, 32); //LG에서 만든 2017년 32인치
    myTV.show();
    }

    출력 결과

    LG에서 만든 2017년형 32인치 TV

    소스코드

    class TV{
    	String brand_name;
    	int year;
    	int inch;
    
    	TV(String brand_name, int year, int inch){
        this.brand_name = brand_name;
        this.year = year;
        this.inch = inch;
    }
    
    public void show() {
    
    	System.out.println(brand_name + "에서 만든 "+year+"년형 "+inch+"인치 TV");
    
    }

    14. 아래의 BankAccount 객체에 대하여 그림을 그리시오.

    BankAccount ref1 = new BankAccount();
    BankAccount ref2 = ref1;

    15.아래의 프로그래밍을 작성하시오.

    Gugudan gugudan = new Gugudan();
    gugudan.printGugu(10); //1단부터 10단까지 출력
    gugudan.printGugu(20); //1단부터 20단까지 출력

    소스코드

    public class Gugudan {
    
    public static void main(String[] args) {
    	PrintGugudan gugudan = new PrintGugudan();
    
    	gugudan.printGugu(10);
    	gugudan.printGugu(20);
     }
    }
    
    class PrintGugudan {
    
    int number1;
    
    PrintGugudan() {
    }
    
    PrintGugudan(int number1) {
    	this.number1 = number1;
    }
    
    public void printGugu(int dan) {
    
    	for (int i = 1; i <= dan; i++) {
    		for (int j = 1; j <= 9; j++) {
    			System.out.println(i + "*" + j + "=" + (i * j));
    		}
    	}
     }
    }

    출력결과

    1*1=1
    1*2=2
    1*3=3 ......
    
    20*9=180
    profile
    개발자 지망생

    0개의 댓글

    관련 채용 정보