객체지향언어-20230602

DONGDONG_JU·2023년 6월 2일
0

자바

목록 보기
9/16

과제1-년,월,일 입력받아 요일 구하기

		Scanner sc = new Scanner(System.in);
		
		int year,month,day,result,j;
		String [] weeks= {"일","월","화","수","목","금","토"};
		int [] monthLastDays= {31,28,31,30,31,30,31,31,30,31,30,31};
		
		System.out.printf("년도와 월과 일 입력하세요: ");
		year = sc.nextInt();
		month= sc.nextInt();
		day = sc.nextInt();
		
		
		if(year%4==0 && year % 100 !=0 || year %400 ==0) {
			monthLastDays[2]=29;
		}
		
		
		
		 result =   (year-1)*365     // 2023년 기준 전년도*365     = 738,030
	                +(year-1)/4      //+전년도 /4      =  505
	                -(year-1)/100    //-전년도 /100   =  20
	                +(year-1)/400;   //+전년도/400     = 5  1년1월1일부터~ 전년도 12월 31일까지의 날수   = 738520
	
		 for(int i=0;i<month-1;i++)     //올해 저번달까지의 날짜  0~5배열
	        {
	            result+=monthLastDays[i];    // [0]=1월  [1]=2월    //전년도 12월 31일까지+저번달까지의 날수계산 끝  31+28+31+30+31  = 151+738520
	        }
		
		
		result += day;    //   결과값+오늘날짜  === 총 날수    738,671+2   = 738673 
		
		j = result%7;  //날수%7      738673%7 ==5
		
		System.out.printf("입력하신 %d년의 %d월의 %d일은 %s요일 입니다.",year,month,day,weeks[j]);

인터넷 보고 했는데 작년까지의 날수구하는 공식이 이해가 안간다..

<다른거>

슬랙에서 보기 chap5-test1,2,3


객체지향언어

▶객체지향은-현실세계 시뮬레이션?

▶클래스와 객체의 정의와 용도
-클래스 정의: 객체를 정의해 놓은것
-클래스 용도: 객체를 생성하는데 사용

-객체의 정의: 실제로 존재하는 것.사물 또는 개념
-객체의 용도: 객체의 속성과 기능에 따라 다름

▶객체추상화

object-현실세계
객체 추상화한 결과로 class를 만들고
memory world에서 객체와 비스무리한거로 만드는데 이게 인스턴스
인스턴스는 현실세계 존재 x, 오직 memory world에서만 존재하는 객체 비스무리한거.

object 와 인스턴스는 비슷하다

▶클래스이름은 식별자
-보통은 대문자로 시작함
클래스는 현실세계의 오브젝트를 표현하는 설계도?
필드=멤버변수=인스턴스변수

▶인스턴스화
-클래스로부터 인스턴스를 생성하는 것.

▶객체는 속성과 기능으로 이루어져있다.
-객체는 속성과 기능의 집합이며, 속성과 기능을 객체의 멤버(member,구성요소)라고 한다..

▶속성은 변수로, 기능은 메서드로 정의한다.
-클래스를 정의할 때 객체의 속성은 변수(필드)로, 기능은 메서드로 정의한다.

객체지향프로그래밍 3단계
1.클래스 정의
2.클래스로부터 인스턴스 생성
3.래퍼런스 사용(참조변수 사용)

▶인스턴스의 생성방법
클래스명 참조변수명; --객체를 다루기 위한 참조변수 선언
참조변수명 = new 클래스명(); -- 객체생성 후 , 생성된 객체의 주소를 참조변수에 저장

Tv t;
t = new Tv();

Tv t = new Tv();

예시1

-class Tv-

class Tv {
	String color;
	boolean Ispower;
	int channel;
	
	void power() {
		Ispower = !Ispower;
	}
	
	void channelUp() {
		channel++;
	}
	
	void channelDown() {
		channel--;
	}
	
}

-class TvTest-

public class TvTest {

	public static void main(String[] args) {
		
		Tv t = new Tv();
		
		System.out.println("color: "+t.color);
		System.out.println("Ispower: "+t.Ispower);
		System.out.println("channel: "+t.channel);
		
		t.power();
		t.color = "은색";
		t.channelUp();
		t.channelUp();
		t.channelUp();
		
		System.out.println("color: "+t.color);
		System.out.println("Ispower: "+t.Ispower);
		System.out.println("channel: "+t.channel);

	}

}

-class TvTest2-

Tv t1,t2;
		t1 = new Tv();
		t2 = new Tv();
		
		System.out.println("Tv1: color: "+t1.color);
		System.out.println("Tv1: Ispower: "+t1.Ispower);
		System.out.println("Tv1: channel: "+t1.channel);
		
		
		System.out.println("Tv2: color: "+t2.color);
		System.out.println("Tv2: Ispower: "+t2.Ispower);
		System.out.println("Tv2: channel: "+t2.channel);
		
		System.out.println("T1: "+t1+ ", T2: "+t2);  
		//객체아이디값 나옴 패키지명.클래스명@ 각각의 인스턴스(id)값
		
		System.out.println("---------------------------");
		
		t2 = t1;
		
		t1.power();
		t2.color = "은색";
		t1.channelUp();
		t2.channelUp();
		t1.channelUp();
		
		
		System.out.println("Tv1: color: "+t1.color);
		System.out.println("Tv1: Ispower: "+t1.Ispower);
		System.out.println("Tv1: channel: "+t1.channel);
		
		
		System.out.println("Tv2: color: "+t2.color);
		System.out.println("Tv2: Ispower: "+t2.Ispower);
		System.out.println("Tv2: channel: "+t2.channel);
		
		System.out.println("T1: "+t1+ ", T2: "+t2); 


예시2

-Circle-

	class Circle {
	
	double pi = 3.141592;
	int radius;
	
	void setRadius(int rad) {
		radius = rad;
	}
	
	double getArea() {
		double area;
		area = radius * radius * pi;
		
		return area;
	}

}

-CircleTest-

	public class CircleTest {

	public static void main(String[] args) {
		
		Circle circle;
		circle = new Circle();
		
		circle.setRadius(5);
		
		System.out.printf("반지름인 5인 원의 넓이는 %.2f 입니다. \n",circle.getArea());
		
		
		circle.setRadius(7);
		
		System.out.printf("반지름인 7인 원의 넓이는 %.2f 입니다. \n",circle.getArea());


	}

}


클래스의 또다른 정의

▶변수- 하나의 데이터를 저장할 수 있는 공간
▶배열 - 같은 타입의 여러 데이터를 저장할 수 있는 공간
▶구조체-타입에 관계없이 서로 관련된 데이터들을 저장할 수 있는 공간
▶클래스 - 데이터와 함수의 결합(구조체+함수)

▶클래스-사용자 정의 타입 (User-defined type)
-프로그래머가 직접 새로운 타입을 정의할 수 있다.
-서로 관련된 값을 묶어서 하나의 타입으로 정의한다.


예시1

-class Time-

public class Time {

	int hour;
	int minute;
	int second;  //3개의 필드
	
	void setHour(int h) {
		hour = h;
	}
	void setMinute(int m) {
		minute = m;
	}
	void setSecond(int s) {
		second = s;
	}
	
	String currentTime() {
		return hour + "시" + minute + "분" + second + "초";
	}
	
}

-class TimeTest-

public class TimeTest {

	public static void main(String[] args) {
		
		Time t1,t2,t3;
		t1 = new Time();
		t2 = new Time();
		t3 = new Time();
		
		t1.setHour(14);
		t1.setMinute(30);
		t1.setSecond(25);
		
		t2.setHour(17);
		t2.setMinute(42);
		t2.setSecond(21);
		
		t3.setHour(10);
		t3.setMinute(12);
		t3.setSecond(43);
		
		System.out.println("time1: "+t1.currentTime());
		System.out.println("time2: "+t2.currentTime());
		System.out.println("time3: "+t3.currentTime());
		
		

	}

}

예시1-배열로

		Time [] timeArr;
		timeArr = new Time[3];
		
		timeArr[0] = new Time();
		timeArr[1] = new Time();
		timeArr[2] = new Time();
		
		timeArr[0].setHour(14);
		timeArr[0].setMinute(30);
		timeArr[0].setSecond(25);
		
		timeArr[1].setHour(14);
		timeArr[1].setMinute(30);
		timeArr[1].setSecond(25);
		
		timeArr[2].setHour(14);
		timeArr[2].setMinute(30);
		timeArr[2].setSecond(25);
		
		for(int i = 0; i<timeArr.length; i++) {
			System.out.printf("time%d %s \n",i+1,timeArr[i].currentTime());
		}

복잡하면서 쉬운듯.... 집가서 다시해봐야지!


참고문헌- 자바의정석 (남궁성)

profile
웹개발자로 취업하고싶어 공부중인 HDJ 입니다

0개의 댓글