Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

Test .oop 본문

JAVA/chapter06_oop

Test .oop

GAWON 2023. 5. 26. 17:43
Q1. 
클래스 Circle
- 필드 : radius, PI, name
- 메소드 : info
클래스 CircleMain
- 메소드 : main
값을 대입해서 
반지름, 이름, 크기(PI*R*R), 둘레(2*PI*R) 값 출력

Q2.
클래스 Rect
- 필드 : width, height
- 메소드 : init(너비, 높이 입력), info(너비, 높이, 크기(calcArea) 출력), 
calcArea(w*h, 넓이계산(크기) 출력)
클래스 RectMain
- 메소드 : main
값을 입력 받아서 (Scanner) 확인

Q3. 
클래스 Triangle 
- 필드 : width, height
- 메소드 : init(너비, 높이 입력), info(너비, 높이, 크기 출력), calcArea(w*h/2 넓이계산 후 리턴)
클래스 TriangleMain
- 메소드  : main
값을 입력 받아서 (Scanner) 확인 
파일을 하나만 사용


Q4. 
클래스 Student
- 필드 :
String name, String dept, String score1, String score2
double average
boolean isPass(합격 유무) : 80점 이상 true, 80점 미만 false
- 메소드 : 
input : name, dept, score1, score2 콘솔입력, 평균 및 패스 유무 확인
output : name, dept, average, isPass ("합격","불합격")
클래스 StudentMain
- 메소드 : main
package org.joonzis.test;

/*클래스 Circle
- 필드 : radius, PI, name
- 메소드 : info
클래스 CircleMain
- 메소드 : main
값을 대입해서 
반지름, 이름, 크기(PI*R*R), 둘레(2*PI*R) 값 출력*/

public class Circle {
	double radius;
	final double PI = 3.14;
	String name;
	
	void info() {
		System.out.println("반지름 : " +radius);
		System.out.println("이름 : " + name);
		System.out.println("크기 : " + (PI * radius * radius));
		System.out.println("둘레 : " + (PI * 2 * radius));
	}
}
package org.joonzis.test;

public class CircleMain {
	public static void main(String[] args) {
		
		Circle myCircle = new Circle();
		
		myCircle.radius = 12.3;
		myCircle.name = "지구";
		
		myCircle.info();
		
		
	}
}
package org.joonzis.test;

import java.util.Scanner;

/*Q2.
클래스 Rect
- 필드 : width, height
- 메소드 : init(너비, 높이 입력), info(너비, 높이, 크기(calcArea) 출력), 
calcArea(w*h, 넓이계산(크기) 출력)
클래스 RectMain
- 메소드 : main
값을 입력 받아서 (Scanner) 확인*/
public class Rect {
	// 필드
	int width;
	int height;
	Scanner sc;
	
	// 메소드
	void init() {
		sc = new Scanner(System.in);
		System.out.print("너비 입력 >> ");
		width = sc.nextInt();
		System.out.print("높이 입력 >> ");
		height = sc.nextInt();
	}
	void info() {
		System.out.println("너비 : " + width);
		System.out.println("높이 : " + height);
		calcArea();
	}
	void calcArea() {
		System.out.println("크기 : " + (width * height));
	}
	

}
package org.joonzis.test;

public class RectMain {
	public static void main(String[] args) {
		
		Rect r1 = new Rect();
	
		r1.init();
		r1.info();
	}
}
package org.joonzis.test;

import java.util.Scanner;

//클래스 Student
//- 필드 :
//String name, String dept, String score1, String score2
//double average
//boolean isPass(합격 유무) : 평균 점수가 80점 이상 true, 80점 미만 false
//- 메소드 : 
//input : name, dept, score1, score2 콘솔입력, 평균 및 패스 유무 확인
//output : name, dept, average, isPass ("합격","불합격")
public class Student {
	// 필드
	String name, dept, score1, score2;
	double average;
	boolean isPass;
	
	// 메소드
	void input(Scanner sc) {
		System.out.print("이름 입력 >> ");
		name = sc.next();
		System.out.print("학과 입력 >> ");
		dept = sc.next();
		System.out.print("중간 점수 입력 >> ");
		score1 = sc.next();
		System.out.print("기말 점수 입력 >> ");
		score2 = sc.next();
		average = (Double.parseDouble(score1) + Double.parseDouble(score2)) / 2.0;
		isPass = average >= 80 ? true : false;
	}
	void output() {
		System.out.println("이름 : " + name);
		System.out.println("학과 : " + dept);
		System.out.println("평균 : " + average);
		System.out.println(isPass ? "합격" : "불합격");
	}
	
	
	
}
package org.joonzis.test;

import java.util.Scanner;

public class StudentMain {
	public static void main(String[] args) {
		Student stu = new Student();
		
		// 1.
		Scanner sc = new Scanner(System.in);
		stu.input(sc);
		
		// 2.
		//stu.input(new Scanner(System.in));
		
		stu.output();
	}
}
package org.joonzis.test;

import java.util.Scanner;

//Q3. 
//클래스 Triangle 
//- 필드 : width, height
//- 메소드 : init(너비, 높이 입력), info(너비, 높이, 크기 출력), calcArea(w*h/2 넓이계산 후 리턴)
//클래스 TriangleMain
//- 메소드  : main
//값을 입력 받아서 (Scanner) 확인 
//파일을 하나만 사용
class Triangle{
	int width, height;
	
	void init(Scanner sc) {
		System.out.print("너비 입력 >> ");
		width = sc.nextInt();
		System.out.print("높이 입력 >> ");
		height = sc.nextInt();
	}
	void info() {
		System.out.println("너비 : " + width);
		System.out.println("높이 : " + height);
		System.out.println("크기 : " + calcArea() );
	}
	double calcArea() {
		return (width * height) / 2.0;
	}
}
public class TriangleMain {
	public static void main(String[] args) {
		Triangle t1 = new Triangle();
		Scanner sc = new Scanner(System.in);
		t1.init(sc);
		t1.info();
		sc.close();
	}
}

'JAVA > chapter06_oop' 카테고리의 다른 글

chapter02 : PersonMain  (0) 2023.05.26
chapter01 : book / book_main  (0) 2023.05.26