📌240103 과제(표채우기) - Solution
- printf 이용해서 출력 및 정렬할 때,
직접 수를 입력하는 것보다 탭 입력해주는 게 좋다
- 평균 출력할 때 소수점 자리 나오게 하는 방법 잘 기억해두자
package ex06_3_method_intro3_return;
public class Homework_240103_Solution {
public static void main(String[] args) {
String name[] = { "강호동", "이승기", "유재석", "하하", "이광수" };
String sub_name[] = { "국어", "수학", "영어" };
int[][] score = { { 85, 60, 70 },
{ 90, 95, 80 },
{ 75, 80, 100 },
{ 80, 70, 95 },
{ 100, 65, 80 }
};
int[] subject = new int[3];
int[] student = new int[5];
System.out.println("=========== 학생별 / 과목별 총점구하기 ===========");
String[] R2 = { "국어", "수학", "영어", "총점", "평균" };
for (int i = 0; i < R2.length; i++) {
System.out.printf("%s\t", R2[i]);
}
System.out.println();
for (int i = 0; i < score.length; i++) {
for (int j = 0; j < score[i].length; j++)
student[i] += score[i][j];
}
for (int i = 0; i < name.length; i++) {
System.out.printf("%s\t", name[i]);
for (int j = 0; j < score[i].length; j++) {
System.out.printf("%s\t", score[i][j]);
}
System.out.printf("%s\t %.1f\t%n", student[i], (student[i] / 3f));
}
System.out.println("=================================================");
System.out.printf("%s\t", "총점");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
subject[i] += score[j][i];
}
}
for (int i = 0; i < subject.length; i++) {
System.out.printf("%s\t", subject[i]);
}
}
}
📌method overloading
package ex06_4_method_overloading;
public class Method_overloading {
public static void print() {
System.out.println("print()메서드 입니다.");
return;
}
public static void print(int num) {
System.out.println("print(int num)메서드 입니다.");
}
public static void print(int num, char data) {
System.out.println("print(int num, char data)메서드 입니다.");
}
public static void print(char num, int data) {
System.out.println("print(char num, int data)메서드 입니다.");
}
public static void main(String[] args) {
print();
print(10);
print(10, 'a');
}
}
📌pubilic static void main의 의미
public class MethodExample7 {
public static void main(String[] args) {
int num = 50;
print(num);
}
public static void print(int parameter) {
System.out.println("print() 메서드 입니다.");
System.out.println("main() 메서드에서 전달받은 데이터의 값은 " + parameter + "입니다.");
}
}
📌method 2개를 중첩해서 사용하는 문제
package ex06_3_method_intro3_return;
import java.util.Scanner;
public class MethodExample2_abs_input {
public static int input() {
Scanner sc = new Scanner(System.in);
System.out.print("정수를 입력 하세요?");
int value = sc.nextInt();
sc.close();
return value;
}
public static int abs(int data) {
if (data >= 0) {
} else {
data = (-(data));
}
return data;
}
public static void main(String[] args) {
int inputdata = input();
int result = abs(inputdata);
System.out.println("절대값 :" + result);
}
}
📌데이터 5개를 입력받고 최대값/최소값 구하는 문제
package ex06_3_method_intro3_return;
import java.util.Scanner;
public class MethodExample4_max_min_array2_input {
public static int[] input() {
Scanner sc = new Scanner(System.in);
System.out.print("5개의 정수를 입력 하세요?");
int v1 = sc.nextInt();
int v2 = sc.nextInt();
int v3 = sc.nextInt();
int v4 = sc.nextInt();
int v5 = sc.nextInt();
int[] input = { v1, v2, v3, v4, v5 };
sc.close();
return input;
}
public static int max(int[] data1) {
int max = data1[0];
for (int i = 1; i < data1.length; i++) {
if (max < data1[i])
max = data1[i];
}
return max;
}
public static int min(int[] data1) {
int min = data1[0];
for (int i = 1; i < data1.length; i++) {
if (min > data1[i])
min = data1[i];
}
return min;
}
public static void main(String[] args) {
int[] data = input();
int max2 = max(data);
int min2 = min(data);
System.out.println("max = " + max2);
System.out.println("min = " + min2);
}
}
📌constructor(생성자)
public class GoodsStock {
String goodsCode;
int stockNum;
GoodsStock(String goodsCode, int stockNum) {
this.goodsCode = goodsCode;
this.stockNum = stockNum;
}
void addStock(int num) {
stockNum += num;
}
int subtractStock(int num2) {
if(num2 > stockNum) {
return 0;
} else {
stockNum -= num2;
return num2;
}
}
}
- this() : 생성자에서 다른 생성자를 호출할 수 있다
public class SubscriberInfo {
String name;
String id;
String password;
String phoneNo;
String address;
SubscriberInfo(String name, String id, String password) {
this.name = name;
this.id = id;
this.password = password;
}
SubscriberInfo(String name,String id,String password,String phoneNo,String address){
this(name, id, password);
this.phoneNo = phoneNo;
this.address = address;
}
void changePassword(String password) {
this.password = password;
}
void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
void setAddress(String address) {
this.address = address;
}
}
📌반복되는 코드를 method를 이용해서 관리하기
- 반복되는 내용을 method로 정의해주면 관리/재사용이 편하다
public class GoodsStock_make {
public static void main(String args[]) {
GoodsStock obj = new GoodsStock("52135", 200);
print(obj);
obj.addStock(1000);
System.out.println("=====obj.addStock(1000); 후=====");
print(obj);
obj.subtractStock(500);
System.out.println("=====obj.subtractStock(500); 후=====");
print(obj);
}
private static void print (GoodsStock obj) {
System.out.println("상품코드:" + obj.goodsCode);
System.out.println("재고수량:" + obj.stockNum);
}
}
public static void main(String[] args) {
SubscriberInfo obj
= new SubscriberInfo("제임스 고슬링", "java",
"java", "010-111-2222", "나를 찾지 마세요~");
printSubscriberInfo(obj);
obj.setAddress("서울시 강남구 역삼동");
printSubscriberInfo(obj);
}
static void printSubscriberInfo(SubscriberInfo obj) {
System.out.println("이름:"+obj.name);
System.out.println("아이디:"+obj.id);
System.out.println("패스워드:"+obj.password);
System.out.println("전화번호:"+obj.phoneNo);
System.out.println("주소:"+obj.address);
System.out.println("============================");
}
📌240104 과제 - method를 활용하여 240103과제 해결하기
public class Student_Make {
public static void main(String[] args) {
Student a = new Student("강호동", 85, 60, 70);
Student b = new Student("이승기", 90, 95, 80);
Student c = new Student("유재석", 75, 80, 100);
Student d = new Student("하하", 80, 70, 95);
Student e = new Student("이광수", 100, 65, 80);
System.out.println("=========== 학생별 / 과목별 총점구하기 ===========");
String[] R2 = { "이름", "국어", "수학", "영어", "총점", "평균" };
for (int i = 0; i < R2.length; i++) {
System.out.printf("%s\t", R2[i]);
}
System.out.println();
print(a);
print(b);
print(c);
print(d);
print(e);
for (int i = 1; i < 48; i++) {
System.out.print("=");
}
int korhpa1 = korhap(a) + korhap(b) + korhap(c) + korhap(d) + korhap(e);
int enghap1 = enghap(a) + enghap(b) + enghap(c) + enghap(d) + enghap(e);
int mathhap1 = mathhap(a) + mathhap(b) + mathhap(c) + mathhap(d) + mathhap(e);
System.out.println();
System.out.print("총점" + "\t");
System.out.printf("%d\t%d\t%d\t", korhpa1, enghap1, mathhap1);
}
static void print(Student abs) {
System.out.printf(abs.name + "\t");
System.out.printf(abs.kor + "\t");
System.out.printf(abs.eng + "\t");
System.out.printf(abs.math + "\t");
System.out.printf(abs.getTotal() + "\t");
System.out.printf("%.1f\t", abs.getAverage());
System.out.println();
}
static int korhap(Student hap) {
return hap.kor;
}
static int enghap(Student hap) {
return hap.eng;
}
static int mathhap(Student hap) {
return hap.math;
}