본문 바로가기

Java28

[Java]람다식(Lambda) 알고리즘 코드를 보다보면 람다식을 자주 사용하는 것을 볼 수 있다. 제대로 이해하고 가는 것이 나을 거 같아서 정리하는 글! 람다 함수란? 프로그래밍 언어에서 사용되는 개념으로 익명 함수(Anonymous functions)를 지칭하는 용어로 보다 단순하게 표현하는 방법 //기존 방식 public String Method(매개변수){ return "Hello"; } //람다식 (매개변수) -> "Hello"; 람다식 특징 · 람다식 내에서 사용되는 지역변수는 final이 붙지 않아도 상수로 간주 · 람다식으로 선언된 변수명은 다른 변수명과 중복될 수 없음 람다식 장점 · 코드를 간결하게 만들어 줌 · 개발자의 의도가 명확히 드러나 가독성이 높아짐 · 함수를 만드는 과정이 없어 생산성이 높아짐 · 멀티쓰레.. 2022. 7. 14.
[Java] 파일 읽고/쓰기 파일 쓰기 public static void main(String[] args) { File file = new File("d:\\myfile\\writeData.txt"); try { // 쓰기 /* FileWriter fw = new FileWriter(file); fw.write("안녕하세요"); fw.write("hi hello"); fw.close(); // FileWriter에서는 close 필수! */ /* //추가 쓰기 FileWriter fw = new FileWriter(file, true); // 매개변수 2개 fw.write("반갑습니다"); fw.close(); */ // 문장쓰기 FileWriter fw = new FileWriter(file); BufferedWriter bw .. 2022. 5. 20.
[Java] Abstract(추상클래스) 추상 클래스 - 공통적으로 포함되는 필드와 메소드를 사용하여 실체 클래스에서 필드와 메소드의 이름을 통일하여 유지보수성을 높이고 통일성을 유지 ˚ 추상 메소드를 하나 이상 포함하고 있는 클래스 ˚ 일반 메소드를 포함 ˚ 멤버 변수 선언 가능 추상 메소드 ˚ 내용(처리)은 없음 ˚ Prototype(매개변수, 리턴값)만 선언되어 있는 메소드 추상 클래스 public abstract class Animal{ public String kind;// 필드 public void breath(){// 일반 메소드 System.out.println("숨"); } public abstract void sound();// 추상 메소드 } 추상 메소드를 상속받은 실체 클래스는 재정의(오버라이딩)해야함 실체 클래스 publ.. 2022. 5. 18.
[Java] Inheritance(상속)3 MainClass import cls.ChildClass3; import cls.ParentClass3; public class 과제9_상속3 { public static void main(String[] args) { ParentClass3 e = new ParentClass3("스파이더맨", 7, 5); e.display(); System.out.println("***********************************"); // 매개변수 4개인 생성자로 객체 생성 ChildClass3 e1 = new ChildClass3("매트릭스", 9, 10, 10, 8, 9); e1.display(); } } =====================================================.. 2022. 5. 17.
[Java] Constructor(생성자) public class Student { String name; int ban, no, kor, eng, math; int total; int getTotal(int kor, int eng, int math) { this.kor = kor; this.eng = eng; this.math = math; return (kor + eng + math); } double getAverage() { return (kor + eng + math) / 3; } } Main public static void main(String[] args) { Student s = new Student(); s.name = "홍길동"; s.ban = 1; s.no = 1; s.kor = 100; s.eng = 60; s.math =.. 2022. 5. 17.
[Java] 학생들의 최고 점수/최저 점수 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String student[][] = {// 국어 영어 수학 { "홍길동", "1999-01-23", "100", "90", "75" }, { "성춘향", "2010-07-04", "90", "100", "95" }, { "일지매", "2001-03-05", "85", "95", "100" }, }; // 각 학생들의 국어, 영어, 수학을 합친 총점은? 개개인의 총점 int sum[] = new int[student.length]; int temp;// 1명분에 대한 성적의 합계 for (int j = 0; j < student.length; j++) { temp =.. 2022. 5. 17.
[Java] 숫자 정렬 int number[] = null; int updown[] = new int[1];// 1을 넣는 이유 - 오름차순인지 내림차순인지 하나만 넣어서 구별 number = input( updown ); // 정렬 sotring(number, updown[0]); // 결과출력 result(numbr, updown[0]); Static int[] input(int[] updown){ Scanner sc = new Scanner(System.in); System.out.print("정렬할 숫자의 개수 = "); int count = sc.nextInt(); int number[] = new int[count]; for(int i = 0; i < number.length; i++ ){ System.out.pri.. 2022. 5. 17.
[Java] 학생들의 평균 점수를 구하라 Scanner scan = new Scanner(System.in); System.out.print("학생 수를 입력하세요 : "); int n = scan.nextInt(); int score[] = new int[n]; for(int i=0; i < n ; i++ ){ System.out.print("점수를 입력하세요 : "); score[i] = scan.nextInt(); if(i == n){ break; } } int total = 0; for(int i = 0 ; i < n ; i++ ){ total += score[i]; } System.out.println("총점 : " + total); 2022. 5. 16.
[Java] Interface2 NameCard_main import cls.NameCard; import cls.PrintNameCard; import cls.PrintNamePhoneCard; public class NameCard_main { public static void main(String[] args) { NameCard namecard = new NameCard("홍길동", "123-4567", "asdf@naver.com"); //PrintNameCard pnc = new PrintNameCard(); //namecard.setPrintNameCard(pnc);// NameCard 18번째 줄로 정보 감 namecard.setPrintNameCard(new PrintNameCard());// 12,13줄을 요약한것 n.. 2022. 5. 16.