Part07 - constructor

uglyduck.dev·2020년 9월 29일
0
post-thumbnail

Book

package com.mywork.ex;

public class Book {
	// Field
	String title;
	String writer;
	int price;
	int salesVolume;       // 판매량
	boolean isBestSeller;  // 판매량이 1000 이상이면 true, 아니면 false
	
	// Constructor
	Book(){	}	// Deafult 
	Book(String _s, int _p){
		title = _s;
		writer = "작가미상";
		price = _p;
	}
	Book(String _s, String _w, int _p){
		title = _s;
		writer = _w;
		price = _p;
	}
	
	// Method
	void setSaleVolume(int sales) {
		salesVolume = sales;
		isBestSeller = sales >= 1000 ? true : false;
	}
	void output() {
		System.out.println("제목 : " + title);
		System.out.println("저자 : " + writer);
		System.out.println("가격 : " + price);
		System.out.println("판매량 : " + salesVolume);
		System.out.println(isBestSeller ? "베스트셀러" : "일반서적");
	}

}

BookMain

package com.mywork.ex;

public class BookMain {
	public static void main(String[] args) {
		Book book1 = new Book();
		Book book2 = new Book("콩쥐팥쥐", 10000);
		Book book3 = new Book("도전 자바 200문제", "김자바" ,25000);
		
		book1.setSaleVolume(0);
		book2.setSaleVolume(500);
		book3.setSaleVolume(1500);
		
		book1.output();		System.out.println("-----");
		book2.output();		System.out.println("-----");
		book3.output();
	}
}

Circle

package com.mywork.ex;

public class Circle {

	// Field
	int x;
	int y;
	double radius;
	
	// Constructor
	Circle() {
		x = 0;
		y = 0;
		radius = 1;
	}
	Circle(double r) {
		x = 0;
		y = 0;
		radius = r;
	}
	Circle(int _x, int _y, double r) {
		x = _x;
		y = _y;
		radius = r;
	}
	
	// Method
	double calcArea() {
		return radius * radius * Math.PI;
	}
	double calcCircum() {
		return 2 * Math.PI * radius;
	}
	void output() {
		System.out.println("중심좌표 : [" + x + ", " + y + "]");
		System.out.println("반지름 : " + radius);
		System.out.println("크기 : " + calcArea());
		System.out.println("둘레 : " + calcCircum());		
	}
}

CircleMain

package com.mywork.ex;

public class CircleMain {
	public static void main(String[] args) {
		Circle circle1 = new Circle();
		Circle circle2 = new Circle(1.5);
		Circle circle3 = new Circle(3, 3, 2.5);
		
		circle1.output();
		circle2.output();
		circle3.output();
	}
}

Local

package com.mywork.ex;
public class Local {

	// Field
	String name;
	int age;
	String sn;          // 주민등록번호(식별번호가 1~4는 한국인, 5~8은 외국인, 주민등록번호가 없어도 외국인)
	boolean isKorean;   // 한국인: true, 외국인: false
	
	// Constructor
	Local(String _name, int _age) {
		name = _name;
		age = _age;
	}
	Local(String _name, int _age, String _sn) {
		name = _name;
		age = _age;
		sn = _sn;
		isKorean = sn.charAt(7) <= '4' ? true : false;
	}
	// Method
	void output() {
		System.out.println("이름 : " + name);
		System.out.println("나이 : " + age);
		System.out.println("주민등록번호 : " + (sn == null ? "없음" : sn));
		System.out.println(isKorean ? "한국인" : "외국인");
	}
	
}

LocalMain

package com.mywork.ex;

public class LocalMain {
	public static void main(String[] args) {

		Local person1 = new Local("홍길동", 20, "901215-1234567");
		Local person2 = new Local("응우엔티엔", 21, "911215-6789123");
		Local person3 = new Local("james", 22);
		
		person1.output();	System.out.println("-----");
		person2.output();	System.out.println("-----");
		person3.output();
		
	}
}

Rect

package com.mywork.ex;

public class Rect {
	// 1. Fields
	int width;
	int height;
	boolean isSquare;
	
	// Constructor
	// 1. 생성자이다.
	// 2. 객체(인스턴스)를 생성할 때 호출되는 메소드이다.
	// 3. 객체 초기화 용도로 사용한다.
	// 4. 리턴이 없다.
	// 5. 클래스 명과 같다
	Rect(){
		width = 1;
		height = 1;
		isSquare = true;
	}
	
	Rect(int side){
		width = side;
		height = side;
		isSquare = true;
	}
	
	Rect(int w, int h){
		width = w;
		height = h;
		isSquare = (w == h) ? true : false;
	}
	
	// Method
	void output(){
		System.out.println("너비 : " + width);
		System.out.println("높이 : " + height);
		System.out.println("크기 : " + calcArea());
	}
	int calcArea() {
		return width + height;
	}
}

RectMain

package com.mywork.ex;

public class RectMain {
	public static void main(String[] args) {
		// 1. 객체(인스턴스) 생서
		// ==>>> 생성자가 호출된다!
		Rect nemo1 = new Rect();    // Rect(){} [Default(기본) 생성자] 
		                            //   -> 프로그래머가 만들지 않아도 자바가 자동으로 만들어준다.
		Rect nemo2 = new Rect(3);
		Rect nemo3 = new Rect(3, 5);
		
		// 2. 객체 메소드 호출
		nemo1.output();
		nemo2.output();
		nemo3.output();
	}

}

Student

package com.mywork.ex;
import java.util.Scanner;

public class Student {
	
	// Field
	String name;
	String dept;
	String score1;
	String score2;
	double average;
	boolean isPass;
	
	// Constructor
	Student(String _name, String _dept) {
		name = _name;
		dept = _dept;
	}
	
	// Method
	void input(Scanner scanner) {
		System.out.print("중간 >> ");
		score1 = scanner.nextLine();
		System.out.print("기말 >> ");
		score2 = scanner.nextLine();
		average = (Double.parseDouble(score1) + Double.parseDouble(score2)) / 2;
		isPass = average >= 80 ? true : false;
	}
	void output() {
		System.out.println("이름 : " + name);
		System.out.println("학과 : " + dept);
		System.out.println("평균 : " + average);
		System.out.println("결과 : " + (isPass ? "합격" : "불합격"));
	}
	
}

StudentMain

package com.mywork.ex;
import java.util.Scanner;

public class StudentMain {
	public static void main(String[] args) {

		Student student = new Student("홍길동", "컴퓨터공학과");
		student.input(new Scanner(System.in));
		student.output();
		
	}
}

Triangle

package com.mywork.ex;

public class Triangle {
	// Field
	int width;
	int height;
	
	// Constructor
	Triangle(){
		width = 1;
		height = 1;
	}
	Triangle(int w, int h){
		width = w;
		height = h;
	}
	// Method
	double calcArea() {
		return width * height / 2.0;
	}
	void output() {
		System.out.println("넓이 : " + width);
		System.out.println("높이 : " + height);
		System.out.println("크기 : " + calcArea());
	}
}

TriangleMain

package com.mywork.ex;

public class TriangleMain {
	public static void main(String[] args) {
		Triangle semo1 = new Triangle();
		Triangle semo2 = new Triangle(1, 3);
		semo1.output();
		semo2.output();
	}
}
profile
시행착오, 문제해결 그 어디 즈음에.

0개의 댓글