public class Car {
// 필드 정의
String modelName; // 모델명을 담을 수 있는 그릇이 객체 안에 만들어지게 함
String maker;
int price;
int makeYear;
}
public class CarApp {
public static void main(String[] args) {
// Car 설계도로 객체 생성하기
// new Car();
// Car 설계도로 객체 생성하고 생성된 객체의 주소값을 참조 변수(리모콘)에 대입하기
// Car c1 = new Car();
Car c1 = new Car();
Car c2 = new Car();
System.out.println(c1); // 해시코드(c1의 주민등록번호)
System.out.println(c2); // c1의 해시코드 != c2의 해시코드
// 생성된 객체의 속성(필드)에 값 담기
c1.modelName = "제네시스";
c1.maker = "현대자동차";
c1.price = 70000000;
c1.makeYear = 2019;
c2.modelName = "XC40";
c2.maker = "볼보자동차";
c2.price = 54000000;
c2.makeYear = 2018;
// 생성된 객체의 속성 출력에 사용하기
System.out.println(c1.modelName);
System.out.println(c1.maker);
System.out.println(c2.modelName);
System.out.println(c2.maker);
}
}
public class Item {
String name;
int price;
int amount;
}
import java.util.Scanner;
public class ItemApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Item[] cart=new Item[3];
for (;;) {
System.out.println("----------------------------");
System.out.println(" 1. 입력 | 2. 출력 | 0. 종료");
System.out.println("----------------------------");
System.out.print("선택> ");
int selectNo = scanner.nextInt();
if (selectNo == 1) {
for (int i=0; i<3; i++) {
System.out.print("상품명 입력> ");
String productName = scanner.next();
System.out.print("수량 입력> ");
int productAmount = scanner.nextInt();
System.out.print("상품 가격 입력> ");
int productPrice = scanner.nextInt();
Item item = new Item(); // 상품 정보 저장용 Item 객체 생성
item.name = productName;
item.amount = productAmount;
item.price = productPrice;
cart[i] = item; // 배열에 item 객체를 담기
}
} else if (selectNo == 2) {
for (Item x : cart) {
System.out.println(x.name + "\t" + x.amount + "\t" + x.price);
}
} else if (selectNo == 0) {
System.out.println("프로그램 종료");
break;
}
}
}
}
public class Student {
// 학생의 성적 정보 : 번호, 이름, 국어, 영어, 수학 점수 정보를 담는 필드를 정의
int identity;
String name;
int kor;
int eng;
int math;
}
import java.util.Scanner;
public class StudentApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int studentNumbers = 0; // 학생 수를 담을 변수
Student[] students = null; // 여러 명의 학생 정보를 담는 배열을 생성해서 담을 변수
for (;;) {
System.out.println("--------------------------------------------------------------------------------");
System.out.println(" 1. 학생 수 입력 | 2. 학생 정보 입력 | 3. 전체 출력 | 4. 한 학생 출력 | 0. 종료 ");
System.out.println("--------------------------------------------------------------------------------");
System.out.println("메뉴 선택> ");
int selectNo = scanner.nextInt();
if (selectNo == 1) {
System.out.print("학생 수 입력> ");
studentNumbers = scanner.nextInt();
students = new Student[studentNumbers];
} else if (selectNo == 2) {
for (int i=0; i<studentNumbers; i++) {
System.out.print("학번 입력> ");
int studentidentity = scanner.nextInt();
System.out.print("이름 입력> ");
String studentname = scanner.next();
System.out.print("국어 점수 입력> ");
int studentkor = scanner.nextInt();
System.out.print("영어 점수 입력> ");
int studenteng = scanner.nextInt();
System.out.print("수학 점수 입력> ");
int studentmath = scanner.nextInt();
Student student = new Student();
student.identity = studentidentity;
student.name = studentname;
student.kor = studentkor;
student.eng = studenteng;
student.math = studentmath;
students[i] = student;
}
} else if (selectNo == 3) {
for (Student x : students) {
System.out.println(x.identity + "\t" + x.name + "\t" + x.kor + "\t" + x.eng + "\t" + x.math);
}
} else if (selectNo == 4) {
System.out.print("조회할 학생의 학번 입력> ");
int a = scanner.nextInt();
Student student = null;
for (Student x:students) {
if (a == x.identity) {
student = x;
}
}
if(student != null) {
System.out.println(student.identity + "\t" + student.name + "\t" + student.kor + "\t"+ student.eng + "\t" + student.math);
} else {
System.out.println("학생 정보 없음");
}
} else if (selectNo == 0) {
System.out.println("프로그램 종료");
break;
}
}
}
}
public class Account {
// 계좌 정보: 통장 이름, 계좌 번호, 비밀 번호(숫자), 예금주, 잔액
String name;
int num;
int pw;
String owner;
int money;
}
import java.util.Scanner;
public class AccountApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int accountNumbers = 0;
Account[] accounts = null;
/* 입금, 출금할 때 계좌 번호, 비밀 번호, 입금액, 혹은 출금액을 입력받는다.
입력된 계좌 번호와 일치하는 계좌 정보를 찾고
입력된 비밀 번호와 찾아진 계좌의 비밀 번호가 일치하는 경우에만 입금, 출금이 된다. */
for (;;) {
System.out.println("1. 계좌 수 입력 2. 계좌 정보 입력 3. 계좌 정보 출력 4. 입금 5. 출금 0. 종료");
int selectNo = scanner.nextInt();
if (selectNo == 1) {
System.out.println("계좌 수> ");
accountNumbers = scanner.nextInt();
accounts = new Account[accountNumbers];
} else if (selectNo == 2) {
for (int i=0; i<accountNumbers; i++) {
Account account = new Account();
System.out.println("통장 이름> ");
account.name = scanner.next();
System.out.println("계좌 번호> ");
account.num = scanner.nextInt();
System.out.println("비밀 번호(숫자)> ");
account.pw = scanner.nextInt();
System.out.println("예금주> ");
account.owner= scanner.next();
System.out.println("잔액> ");
account.money = scanner.nextInt();
accounts[i] = account;
}
} else if (selectNo == 3) {
for (Account x : accounts) {
System.out.println("계좌명: " + x.name + "\t" + "계좌 번호: " + x.num + "\t" + "예금주: " + x.owner + "\t" + "잔액: " + x.money);
}
} else if (selectNo == 4) {
System.out.println("계좌 번호> ");
int a = scanner.nextInt();
System.out.println("비밀 번호> ");
int b = scanner.nextInt();
System.out.println("입금액> ");
int c = scanner.nextInt();
boolean isSuccess = false;
for (Account x : accounts) {
if (x.num == a && x.pw == b) {
x.money += c;
isSuccess = true;
break;
}
}
if (isSuccess) {
System.out.println("입금 처리 완료");
} else {
System.out.println("계좌 번호 혹은 비밀번호가 올바르지 않음");
}
} else if (selectNo == 5) {
System.out.println("계좌 번호> ");
int a = scanner.nextInt();
System.out.println("비밀 번호> ");
int b = scanner.nextInt();
System.out.println("출금액> ");
int c = scanner.nextInt();
boolean isSuccess = false;
for (Account x : accounts) {
if (x.num == a && x.pw == b) {
x.money -= c;
isSuccess = true;
break;
}
}
if (isSuccess) {
System.out.println("출금 처리 완료");
} else {
System.out.println("계좌 번호 혹은 비밀번호가 올바르지 않음");
}
} else if (selectNo == 0) {
System.out.println("종료");
break;
}
}
}
}
public class Hello {
// 메소드 정의
// 입력/출력 없음, 동작 있음
void morning() {
System.out.println("좋은 아침입니다.");
System.out.println("좋은 하루 보내세요.");
}
// 메소드 정의
// 입력값 있음/반환값 없음, 동작 있음
void greeting(String x ) {
System.out.println( x + "님 반갑습니다.");
}
// 메소드 정의
// 입력값있음/반환값 없음, 동작 있음
void promise(String name, int hour) {
System.out.println(name + "님" + hour + "시에 만납시다.");
}
}
public class HelloApp {
public static void main(String[] args) {
Hello hello = new Hello();
// 메소드 실행(호출)
hello.morning();
hello.greeting("이순신");
hello.greeting("강감찬");
hello.promise("천성현", 5);
hello.promise("강희경", 7);
String username = "김다윤";
hello.greeting(username);
}
}
public class Calculator {
// 메소드 정의
// 입력값 있음, 반환값 있음, 동작 있음
int plus(int x, int y) {
int z = x+y;
return z; // return 키워드는 메소드를 호출한 곳에 z에 보관된 값을 제공(반환)한다.
// 메소드 정의
// 입력값 있음, 반환값 있음, 동작 있음
}
double area(int r) {
double result = 3.14 * r * r;
return result;
}
}
public class CalculatorApp {
public static void main(String[] args) {
Calculator c = new Calculator();
int r = c.plus(10, 6);
System.out.println(r);
double r1 = c.area(3);
double r2 = c.area(5);
System.out.println("원의 넓이 " + r1);
System.out.println("원의 넓이 " + r2);
}
}
public class ArrayUtils {
// 정수로 이루어진 배열을 전달받아서 출력하는 기능
void printArray(int[] items) {
for ( int x : items ) {
System.out.println(x+ "\t");
}
}
// 정수로 이루어진 배열을 전달받아서 가장 큰 값을 제공하는 기능
int max(int[] items) {
int result = 0;
for (int x : items) {
if ( x > result) {
result = x;
}
}
return result;
}
// 정수로 이루어진 배열을 전달받아서 가장 작은 값을 제공하는 기능
int min(int[] items) {
int result = 100;
for (int x : items) {
if ( x < result) {
result = x;
}
}
return result;
}
// 정수로 이루어진 배열을 전달받아서 그 값의 2배로 이루어진 배열을 제공하는 기능
int[] twice(int[] items) {
int[] result = new int[items.length];
for (int i=0; i<items.length; i++) {
result[i]= items[i]*2;
}
return result;
}
}
public class ArrayUtilsApp {
public static void main(String[] args) {
ArrayUtils util = new ArrayUtils();
int[] numbers = {1, 10, 76, 39};
util.printArray(numbers);
int maxNumber = util.max(numbers);
int minNumber = util.min(numbers);
int[] twiceNumber = util.twice(numbers);
System.out.println("최고 숫자: " + maxNumber);
System.out.println("최저 숫자: " + minNumber);
util.printArray(twiceNumber);
System.out.println();
System.out.println();
int[] scores = {60, 70, 30, 46, 98, 100};
util.printArray(scores);
int maxScore = util.max(scores);
int minScore = util.min(scores);
int[] twiceScore = util.twice(scores);
System.out.println("최고 점수 : " + maxScore);
System.out.println("최저 숫자: " + minScore);
util.printArray(twiceScore);
}
}
'자바 > oop1' 카테고리의 다른 글
Book, BookUtils, BookPrinter, BookApp(2), MethodOverloading(App).java (0) | 2019.06.07 |
---|