본문 바로가기
JAVA

221215_Class

by 경 훈 2022. 12. 15.

메소드 오버로딩 : 동명이인

메소드명이 동일하면서 인수의 타입과 개수가 다른 경우

오버라이딩(Overriding) : 재정의, 기능을 다시 부여

 

package day9_ClassTest2;

public class ClassTest3 {
	//두수를 입력해서 합을 구하는
	public void hap(int a,int b)
	{
		System.out.println("합 : "+(a+b));
	}
	public void hap(double a,int b)
	{
		System.out.println("합 : "+(a+b));
	}
	public void hap(int a,double b)
	{
		System.out.println("합 : "+(a+b));
	}
	public void hap(double a,double b)
	{
		System.out.println("합 : "+(a+b));
	}
}

 

package day9_ClassTest2;

public class MainClass {

	public static void main(String[] args) {
		ClassTest3 ct = new ClassTest3();
		ct.hap(12,45);
		ct.hap(12.34,66);
		ct.hap(33,45.7);
		ct.hap(12.3,34.78);
	}
}

 

package day9_ClassTest2;

public class Method_Overloading {
	public void out(String mm) {
		System.out.println("1넘겨받은 자료는 "+mm);
	}
	public void out(int mm) {
		System.out.println("2넘겨받은 자료는 "+mm);
	}
	public void out(double mm) {
		System.out.println("3넘겨받은 자료는 "+mm);
	}
	public void out(char mm) {
		System.out.println("4넘겨받은 자료는 "+mm);
	}
	public void out(Boolean mm) {
		System.out.println("5넘겨받은 자료는 "+mm);
	}
}

 

package day9_ClassTest2;

public class MainClass {

	public static void main(String[] args) {
		Method_Overloading mo =new Method_Overloading();
		mo.out("샘플");
		mo.out(12);
		mo.out(12.54);
		mo.out('A');
		mo.out(12<5);
	}
}

 

package day9_ClassTest2;

public class Animal {
	// 동물이름,음식,날개,나이
	String name;
	String food;
	boolean fly;
	int age;
	//
	public void out() {
		String nal = wing(fly);
		System.out.println("이름 : "+name);
		System.out.println("식성 : "+food);
		System.out.println("날개 : "+nal);
		System.out.println("나이 : "+age);
	}
	private String wing(boolean fly) {
		String nal;
		if(fly==true)
		{
			nal="O 날수있다";
		}
		else
		{
			nal="X 날수없다";
		}
		return nal;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFood() {
		return food;
	}
	public void setFood(String food) {
		this.food = food;
	}
	public boolean isFly() {
		return fly;
	}
	public void setFly(boolean fly) {
		this.fly = fly;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

package day9_ClassTest2;

public class MainClass {

	public static void main(String[] args) {
		Animal ani = new Animal();
		ani.setName("독수리");
		ani.setFood("육류");
		ani.setFly(true);
		ani.setAge(3);
		ani.out();
	}
}

 

package day9_ClassTest2;

import java.text.DecimalFormat;

public class Score {
	//이름,국,영,수..총점..평균..학점
	String name;
	int kor,eng,math;
	
	public void out() {
		DecimalFormat df = new DecimalFormat("#,##0.00");
		System.out.println("이름 : "+name);
		System.out.println("국어 : "+kor);
		System.out.println("영어 : "+eng);
		System.out.println("수학 : "+math);
		int tot = tot(kor,eng,math);
		System.out.println("총점 : "+tot);
		double avg = avg(tot);
		System.out.println("평균 : "+df.format(avg));
		String hak = hak(avg);
		System.out.println("학점 : "+hak);
	}
	private String hak(double avg) {
		String hak;
		switch ((int)avg/10) {
		case 10:
		case 9: hak="A";break;
		case 8: hak="B";break;
		case 7: hak="C";break;
		case 6: hak="D";break;
		default: hak="F";break;
		}
		return hak;
	}
	private double avg(int tot) {
		return (double)tot/3;
	}
	private int tot(int kor2, int eng2, int math2) {
		return kor2+eng2+math2;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
}

 

package day9_ClassTest2;

public class MainClass {

	public static void main(String[] args) {
		Score sc = new Score();
		sc.setName("홍길동");
		sc.setKor(80);
		sc.setEng(90);
		sc.setMath(87);
		sc.out();
	}
}

 

package day9_ClassTest2;

import java.text.DecimalFormat;

public class Score {
	//이름,국,영,수..총점..평균..학점
	String name;
	int kor,eng,math;
	
	int tot;
	double avg;
	String hak;
	
	public void out() {
		DecimalFormat df = new DecimalFormat("#,##0.00");
		System.out.println("이름 : "+name);
		System.out.println("국어 : "+kor);
		System.out.println("영어 : "+eng);
		System.out.println("수학 : "+math);
		System.out.println("총점 : "+tot);
		System.out.println("평균 : "+df.format(avg));
		System.out.println("학점 : "+hak);
	}
	String hak(double avg) {
		String hak;
		switch ((int)avg/10) {
		case 10:
		case 9: hak="A";break;
		case 8: hak="B";break;
		case 7: hak="C";break;
		case 6: hak="D";break;
		default: hak="F";break;
		}
		return hak;
	}
	private double avg(int tot) {
		return (double)tot/3;
	}
	private int tot(int kor2, int eng2, int math2) {
		return kor2+eng2+math2;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
	public int getTot() {
		return tot;
	}
	public void setTot(int tot) {
		this.tot = tot;
	}
	public double getAvg() {
		return avg;
	}
	public void setAvg(double avg) {
		this.avg = avg;
	}
	public String getHak() {
		return hak;
	}
	public void setHak(String hak) {
		this.hak = hak;
	}
}

 

package day9_ClassTest2;

public class MainClass {

	public static void main(String[] args) {
		Score sc = new Score();
		sc.setName("홍길동");
		sc.setKor(80);
		sc.setEng(90);
		sc.setMath(89);
		//총점
		sc.setTot(sc.getKor()+sc.getEng()+sc.getMath());
		//평균
		sc.setAvg((double)sc.getTot()/3);
		//학점
		sc.setHak(sc.hak(sc.getAvg()));
		sc.out();
	}
}

 

package day9_ClassTest2;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class MainClass {

	public static void main(String[] args) {
		Date dd = new Date();
		System.out.println("일시 : "+dd);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy년MM월dd일 a HH:mm:ss");
		System.out.println("날짜 : "+sdf.format(dd));
		System.out.println("============================");
		LocalDate day = LocalDate.now();
		String aa = day.format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일"));
		System.out.println("날짜 : "+aa);
		LocalTime lt = LocalTime.now();
		String bb = lt.format(DateTimeFormatter.ofPattern("a HH시 mm분"));
		System.out.println(bb);
		LocalDateTime ldt = LocalDateTime.now();
		String cc = ldt.format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 a HH시 mm분"));
		System.out.println(cc);
	}
}

 

 

package day9_ClassTest2;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class MainClass {

	//구입날짜 상품명 수량 가격 금액
	//예 : 날짜 : 12월 15일 상품명 : 사과 수량: 5개 가격:1000원 등급:A -> 우수 B->보통 C->저급
	String name;
	int count;
	int cost;
	int money;
	String grade;
	public void out() {
		DecimalFormat df =new DecimalFormat("#,#00");
		String day=date();
		System.out.println("날짜 : "+day);
		System.out.println("이름 : "+name);
		System.out.println("수량 : "+df.format(count)+"개");
		System.out.println("가격 : "+df.format(cost)+"원");
		System.out.println("판매금액 : "+df.format(money)+"원");
		String good=good(grade);
		System.out.println("등급 : "+good);
	}
	private String date() {
		LocalDate date = LocalDate.now();
		String day=date.format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일"));
		return day;
	}
	private String good(String grade) {
		String good;
		if(grade.equals("A")||grade.equals("a"))
		{
			good="우수";
		}
		else if(grade.equals("B")||grade.equals("b"))
		{
			good="보통";
		}
		else if(grade.equals("C")||grade.equals("c"))
		{
			good="저급";
		}
		else
		{
			good="등급보류";
		}
		return good;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getCost() {
		return cost;
	}
	public void setCost(int cost) {
		this.cost = cost;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
}

 

package day9_ClassTest2;

public class main {

	public static void main(String[] args) {
		MainClass mc = new MainClass();
		mc.setName("전기장판");
		mc.setCount(24);
		mc.setCost(155000);
		mc.setMoney(mc.getCount()*mc.getCost());
		mc.setGrade("A");
		mc.out();
	}
}

 

package day9_ClassTest2;

public class Sample {

	public static void main(String[] args) {
		// && 논리연산자 and || or
		// 정보처리기사 필기시험 합격,불합격 판별
		//edps,db,sw,ic,cc  5과목에서 과락이 35점미만 없으면서 평균 60이상 
		int edps=80,db=89,sw=60,ic=68,cc=78;
		double avg = (edps+db+sw+ic+cc)/5;
		if(edps>=40 && db>=40 && sw>=40 && ic>=40 && cc>=40 && avg>=60)
		{
			System.out.println("합격");
		}
		else
		{
			System.out.println("불합격");
		}
		// &  비트연산자 and |  or  --> 2진법

	}

}

 

package day9_ClassTest2;

public class Sample {

	public static void main(String[] args) {
		//진법변환
		int a=35;
		System.out.println(Integer.toBinaryString(a));
		System.out.println(Integer.toOctalString(a));
		System.out.println(Integer.toHexString(a));
	}
}

 

package day9_ClassTest2;

public class Sample {
	String name;//전역변수
	int age;
	boolean married;
	int child;
	
	public void out() {
		System.out.println("이름 : "+name);
		System.out.println("나이 : "+age);
		String marry = marry(married);
		System.out.println("결혼여부 : "+marry);
		System.out.println("자녀 : "+child);
	}
	private String marry(boolean married) {
		String marry;
		if(married == true)
		{
			marry="기혼";
		}
		{
			marry="미혼";
		}
		return marry;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {//지역변수
		this.name = name; //this 가 붙으면 무조건 전역변수
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isMarried() {
		return married;
	}
	public void setMarried(boolean married) {
		this.married = married;
	}
	public int getChild() {
		return child;
	}
	public void setChild(int child) {
		this.child = child;
	}
}

 

package day9_ClassTest2;

public class main {

	public static void main(String[] args) {
		Sample ex = new Sample();
		ex.setName("홍길동");
		ex.setAge(30);
		ex.setMarried(false);
		ex.setChild(0);
		ex.out();
	}
}

'JAVA' 카테고리의 다른 글

221219_Class  (0) 2022.12.19
221216_Class  (0) 2022.12.16
221214_Method,Class  (0) 2022.12.14
221213_Method  (0) 2022.12.13
221212_배열  (0) 2022.12.12

댓글