본문 바로가기

개발일지/Java + Spring

(85)
클래스 이용해서 학점 계산하기_진짜_최종.java package Test; public class Grade{ int tot; double avg; String grade; void printS(String name, int k, int e, int m) { System.out.println("당신의 이름은 " + name + "입니다."); System.out.println("국어 성적 : " + k ); System.out.println("영어 성적 : " + e ); System.out.println("수학 성적 : " + m ); } void tot(int kor, int eng, int math) { tot = kor + eng + math; System.out.println("총점 : " + tot); } void avg() { avg = t..
예외처리(try-catch) Try 8 : 작업을 하다가 문제가 생기면 캐치문으로 가라 모든 수를 0으로 나누는 경우 .. 0이 나오면 자동으로 19번으로 간다. 9 : args 창에서 입력 받음 catch 13 : 배열에 관한 문제 16 : (인자)데이터타입 문제 19 : 0으로 나눈 문제가 생겼을 때 대소문자 제대로 써줘야함
인터페이스 클래스 원래 자바에서는 클래스 두 개 상속 받는 게 금기시 된다. 추상클래스도 안 됨 근데 인터페이스 클래스는 가능함 유일하게 가능하다. interface Interface1{ int interVar = 10; void interface1Method(); } class Interface1Impl implements Interface1{ @Override public void interface1Method() { System.out.println("interface1Method 구현"); } } public class InterfaceTest1 { public static void main(String[] args) { Interface1Impl in1Impl = new Interface1Impl(); Syste..
데이터베이스 자바에서 연결하기 import java.io.*; import java.sql.*; public class Main { public static void main(String[] args) { Connection conn; Statement stmt = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "Application?useSSL=false&serverTimezone=Asia/Seoul", "root", "1234"); //JDBC 연결 System.out.println("DB 연결 완료"); stmt = conn.createStatement();..
[Java] File 만들기/읽기 import java.io.*; import java.util.Scanner; public class PhoneWriterEx { public static void main(String[] args) { FileWriter fw = null; //초기값 File f = new File("c:\\temp\\phone.txt"); //저 장치 밑에 저장해 줌 try { fw = new FileWriter(f); Scanner sc = new Scanner(System.in); System.out.println("전화번호 입력 프로그램입니다."); while(true) { System.out.print("이름 전화번호 >> "); String line = sc.nextLine(); //한 줄을 읽는다. if(..
[Java] 거스름 돈 출력 - 예제 import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] coin = { 1000, 500, 100, 50, 10 }; int sum = 0; //지불금액 System.out.print("물건 값을 입력하세요 : "); int mulG = sc.nextInt(); System.out.print("지불한 돈의 액수를 입력하세요 : "); int inputM = sc.nextInt(); //출력 sum = inputM-mulG; if(inputM>mulG) { System.out.println("거스름 돈 : " + sum + "원"..
[Java] 추상클래스 abstract class AbstractClass{ //변수 선언 int age; //구현된 메소드 void generalMethod() { System.out.println("일반 메소드"); } //추상 메소드 abstract void abstractMethod(); //추상 메소드는 {} 실행부분이 있으면 안 됨 } class AbstractChildClass extends AbstractClass{ //상속 @Override void abstractMethod() { System.out.println("추상 메소드 구현"); } } public class AbstractTest1 { public static void main(String[] args) { //추상 클래스로 객체 생성 //Abst..
[Java] 다형성 정의 다형성 쉽게 상속의 반대 라고 생각을 할수도 있다. Person1 person = new Person1(); Student student = new Student(); 지금까지 객체생성을 할 때 클래스 타입과 동일한 클래스 타입을 참조했다. 하지만 이 방법만 있는 것이 아니다! 다형성을 이용하면 부모 클래스 타입의 레퍼런스 변수로 자식 클래스 객체를 참조할 수 있다! 자식이 부모에게 받으면 상속(extends) 부모가 자식에게 받으면 다형성! (물론 자식도 부모의 값을 다운 캐스팅으로 참조할 수 있다) 업 캐스팅(UpCasting) 부모 클래스 타입 레퍼런스 = 자식 클래스 객체의 참조 값 Person1 person = new Child(); 이렇게 참조하게 되면 자식 클래스 객체의 레퍼런스 값의 타입은..