1. 나눗셈 하기 - 예외 처리 함수(1)
import java.util.Scanner;
public class 실습2 {
static void prn(int i, int j) throws Exception {
System.out.println(i/j);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("첫 번째 수 : ");
int i = scan.nextInt();
System.out.println("두 번째 수 : ");
int j = scan.nextInt();
try {
prn(i,j);
}
catch(Exception e) {
System.out.println("0으로 나누기 오류 발생");
}
}
}
2. 나눗셈 하기 - 예외처리 함수(2)
import java.util.Scanner;
public class 실습2 {
static void prn(int i, int j) throws Exception {
System.out.println(i/j);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("첫 번째 수 : ");
int i = scan.nextInt();
System.out.println("두 번째 수 : ");
int j = scan.nextInt();
try {
prn(i,j);
}
catch(Exception e) {
System.out.println("0으로 나누기 오류 발생");
}
}
}
3. 채널, 볼륨 출력 - 객체 활용(인스턴스 변수)
public class 실습4 {
int channel;
int volume;
boolean onOff;
public static void main(String[] args) {
// TODO Auto-generated method stub
실습4 tv = new 실습4();
tv.channel = 16;
tv.volume = 9;
tv.onOff = true;
System.out.println("채널 : "+tv.channel);
System.out.println("볼륨 : "+tv.volume);
}
}
4. 면적 출력 - 객체 활용(인스턴스 변수 / 인스턴스 메소드)
public class Circle {
String name;
int radius;
public Circle() {
radius = 1;
}
public Circle(int r, String n) {
radius = r;
name = n;
}
public double getArea() {
return 3.14*radius*radius;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle pizza;
pizza = new Circle(10,"피자");
pizza.name = "피자";
pizza.radius = 10;
double area = pizza.getArea();
System.out.println(pizza.name + "의 면적 : "+area);
Circle donut = new Circle();
donut.name = "도넛";
donut.radius = 2;
area = donut.getArea();
System.out.println(donut.name + "의 면적 : "+ +area);
}
}
5. 사각형의 면적 출력하기 - 객체 활용(인스턴스 변수 / 인스턴스 메소드)
import java.util.Scanner;
public class Rectangle {
int width;
int height;
public Rectangle() {
width = 0;
height = 0;
}
public double getArea() {
return width*height;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle rect = new Rectangle();
Scanner scan = new Scanner(System.in);
System.out.println("너비 입력 : ");
rect.width = scan.nextInt();
System.out.println("높이 입력 : ");
rect.height = scan.nextInt();
double area = rect.getArea();
System.out.println("사각형의 면적 : "+ area);
}
}
6. 문장 변형하기 - 객체 활용(인스턴스 메소드)
import java.util.Scanner;
public class Rectangle {
int width;
int height;
public Rectangle() {
width = 0;
height = 0;
}
public double getArea() {
return width*height;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle rect = new Rectangle();
Scanner scan = new Scanner(System.in);
System.out.println("너비 입력 : ");
rect.width = scan.nextInt();
System.out.println("높이 입력 : ");
rect.height = scan.nextInt();
double area = rect.getArea();
System.out.println("사각형의 면적 : "+ area);
}
}
7. 사각형 넓이 출력하기 - 객체 활용(매개변수 자료형에 따른 인스턴스 메소드 호출)
public class Mymath {
int square(int i) {
return i*i;
}
double square(double i) {
return i*i;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
public class MymathTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1 = 10;
double num2 = 3.14;
Mymath obj = new Mymath();
System.out.println(obj.square(num1));
System.out.println(obj.square(num2));
}
}
'CS Study > Java' 카테고리의 다른 글
7. 상속 - 메소드 오버라이딩 (0) | 2022.04.07 |
---|---|
6. 패키지 / 접근 제한자 / 상속 (0) | 2022.04.07 |
4. 배열 / 예외 처리 (0) | 2022.04.07 |
3. if / switch / for / while / do-while / random 함수 (0) | 2022.04.07 |
2. 자료형 2 / Scanner (0) | 2022.04.07 |