Book
package com.mywork.ex;
public class Book {
String title;
String writer;
int price;
int salesVolume;
boolean isBestSeller;
Book(){ }
Book(String _s, int _p){
title = _s;
writer = "작가미상";
price = _p;
}
Book(String _s, String _w, int _p){
title = _s;
writer = _w;
price = _p;
}
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 {
int x;
int y;
double radius;
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;
}
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 {
String name;
int age;
String sn;
boolean isKorean;
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;
}
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 {
int width;
int height;
boolean isSquare;
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;
}
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) {
Rect nemo1 = new Rect();
Rect nemo2 = new Rect(3);
Rect nemo3 = new Rect(3, 5);
nemo1.output();
nemo2.output();
nemo3.output();
}
}
Student
package com.mywork.ex;
import java.util.Scanner;
public class Student {
String name;
String dept;
String score1;
String score2;
double average;
boolean isPass;
Student(String _name, String _dept) {
name = _name;
dept = _dept;
}
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 {
int width;
int height;
Triangle(){
width = 1;
height = 1;
}
Triangle(int w, int h){
width = w;
height = h;
}
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();
}
}