java
-
[JAVA] 클래스 선언 - 클래스 변수, 클래스 메소드JAVA 2023. 7. 16. 15:02
EX1) Calculator 클래스 class Calculator { int left,right; public void setCalculator(int left,int right){ this.left = left; this.right = right; } public void sum(){ //더하기 System.out.println(left + right); } public void avg(){ //평균 System.out.println((left+right)/2); } } public class CalculatorDemo { public static void main(String[] args) throws Exception { Calculator c1 = new Calculator(); c1.setCal..
-
[JAVA] 클래스 선언 - 재고 관리 클래스JAVA 2023. 7. 16. 14:14
EX1) 재고 관리 클래스(Goods) 생성 public class Goods { String code; //상품 코드 int stockNum; //재고 void add(int amount){ //재고 더하기 stockNum += amount; } int del(int amount){ //재고 빼기 if (amount < 0) return 0; stockNum -= amount; return amount; } } 재고 관리 클래스(Goods)의 객체를 생성하여 사용하는 프로그램 /*상품 재고 클래스*/ public class Test { public static void main(String[] args) throws Exception { Goods obj; obj = new Goods(); obj.co..
-
[JAVA] 객체의 생성과 사용JAVA 2023. 7. 13. 23:32
객체 생성을 위해 new 연산자 사용 new StringBuffer("hi,hello") 클래스의 기능은 메소드로 구현됨 /*객체와 클래스*/ public class Test { public static void main(String[] args) throws Exception { StringBuffer obj; //변수 선언, 클래스 이름을 변수의 타입으로 사용 obj = new StringBuffer("hi,hello"); //문자열 조작 기능이 있는 클래스 obj.deleteCharAt(1); //문자하나 삭제하는 메소드 obj.insert(1, 'e'); obj.insert(2, 'y'); System.out.println(obj); } } ※ StringBuffer : JDK 라이브러리에 구비되..
-
[JAVA] try-catch 문JAVA 2023. 7. 13. 23:10
프로그램 실행 중에 발생하는 에러(익셉션: exception,예외)를 처리하는 방법 /* try문의 기본 형식 */ try try 블록 catch(익셉션타입 익셉션변수) catch 블록 finally finally 블록 /*try-catch*/ public class Test { public static void main(String[] args) throws Exception { int num1=2; int num2=3; int num3=0; int result; try{ result = num1+num2; System.out.println("result:" + result); }catch (java.lang.Exception e){ System.out.println("잘못된 연산입니다."); } //..
-
[JAVA] 메소드 호출문JAVA 2023. 7. 13. 22:47
/*메소드 호출 예시*/ public class Test { public static void main(String[] args) throws Exception { printCharacter('*',10); //메소드 호출 System.out.println("Hello"); printCharacter('-',10); //메소드 호출 } static void printCharacter(char ch,int num){ //메소드 생성 for(int i=0 ; i return값 넘겨주기 int sum; sum = num1 + num2; return sum; } } 15 15
-
[JAVA] while 반복문JAVA 2023. 7. 11. 21:51
while 반복문 /*while문 예시*/ public class Test { public static void main(String[] args) throws Exception { int num = 0; while (num < 10){ System.err.println(num); num += 1; } } } 0 1 2 3 4 5 6 7 8 9 do - while 반복문 /*while문 예시*/ public class Test { public static void main(String[] args) throws Exception { /*while문*/ int cnt1 = 0; while (cnt1 < 5){ System.err.println(cnt1); cnt1 += 1; } /*do-while문*/ i..
-
[JAVA] 조건문JAVA 2023. 7. 11. 21:22
/*if문 예시*/ public class Test { public static void main(String[] args) throws Exception { int num1 = 20; int num2 = 10; if (num1 > num2){ System.out.println("num1 값이 더 크다"); if(num2 > 5){ System.out.println("num2 값은 5보다 크다"); } } if(num1 < 10) System.out.println("num1 값은 10미만이다, num1:" + num1); else if(num1 < 20) System.out.println("num1 값은 20미만이다, num1:" + num1); else if(num1 < 30) System.out.pr..