생성자 : Constructor
목적 : 전역변수에게 자료를 처음으로 전달 (객체 초기화)
특징
이름은 클래스와 동일하고 ()가 존재
메서드의 일종이므로 인수와 매개변수의 타입과 갯수가 반드시 같아야함
...() => default constructor(디폴트 생성자)
package day10_Method2;
import java.text.DecimalFormat;
public class ScoreTest2 {
String name;
int kor,eng,math;
int tot;
double avg;
char hak;
public void out() {
DecimalFormat df = new DecimalFormat("#,#00.00");
System.out.println("이름 : "+name);
System.out.println("국어 : "+kor);
System.out.println("영어 : "+eng);
System.out.println("수학 : "+math);
System.out.println("총점 : "+tot);
System.out.println("평균 : "+df.format(avg));
System.out.println("학점 : "+hak(avg));
}
char hak(double avg) {
switch ((int)avg/10) {
case 10:
case 9:hak='A';break;
case 8:hak='B';break;
case 7:hak='C';break;
case 6:hak='D';break;
default:hak='F';break;
}
return hak;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getHak() {
return hak;
}
public void setHak(char hak) {
this.hak = hak;
}
}
package day10_Method2;
import java.util.Scanner;
public class Main_Test {
public static void main(String[] args) {
ScoreTest2 st = new ScoreTest2();
//1 직접입력
// st.setName("홍길동");
// st.setKor(80);
// st.setEng(99);
// st.setMath(75);
//2 키보드 입력
Scanner sc = new Scanner(System.in);
st.setName(name(sc));
st.setKor(kor(sc));
st.setEng(eng(sc));
st.setMath(math(sc));
st.setTot(st.getKor()+st.getEng()+st.getMath());
st.setAvg((double)st.getTot()/3);
st.setHak(st.hak(st.getAvg()));
st.out();
}
private static String name(Scanner sc) {
System.out.print("이름을 입력");
return sc.next();
}
private static int kor(Scanner sc) {
System.out.print("국어점수 입력");
return sc.nextInt();
}
private static int eng(Scanner sc) {
System.out.print("영어점수 입력");
return sc.nextInt();
}
private static int math(Scanner sc) {
System.out.print("수학점수 입력");
return sc.nextInt();
}
}
package day10_Method2;
import java.util.Scanner;
public class Input {
public String munja(String str,Scanner sc) {
System.out.print(str+"를 입력하시오");
return sc.next();
}
public int number(String str,Scanner sc) {
System.out.print(str+"를 입력하시오");
return sc.nextInt();
}
}
package day10_Method2;
import java.util.Scanner;
public class Main_Test {
public static void main(String[] args) {
ScoreTest2 st = new ScoreTest2();
Input in =new Input();
Scanner sc = new Scanner(System.in);
st.setName(in.munja("이름", sc));
st.setKor(in.number("국어", sc));
st.setEng(in.number("영어", sc));
st.setMath(in.number("수학", sc));
st.setTot(st.getKor()+st.getEng()+st.getMath());
st.setAvg((double)st.getTot()/3);
st.setHak(st.hak(st.getAvg()));
st.out();
}
}
package day10_Method2;
public class Process { //처리 클래스 값을 구하는 총점 평균 학점
//1총점
public int tot(int kor,int eng,int math) {//국어,영어,수학을 넘겨받
return (kor+eng+math);
}
//2평균
public double avg(int tot) {
return (double)tot/3;
}
//3학점
public char hak(double avg) {
char hak;
switch ((int)avg/10) {
case 10:
case 9:hak='A';break;
case 8:hak='B';break;
case 7:hak='C';break;
case 6:hak='D';break;
default:hak='F';break;
}
return hak;
}
}
package day10_Method2;
import java.util.Scanner;
public class Main_Test {
public static void main(String[] args) {
ScoreTest2 st = new ScoreTest2();
Input in =new Input();
Scanner sc = new Scanner(System.in);
Process pp = new Process();
st.setName(in.munja("이름", sc));
st.setKor(in.number("국어", sc));
st.setEng(in.number("영어", sc));
st.setMath(in.number("수학", sc));
st.setTot(pp.tot(st.getKor(),st.getEng(),st.getMath()));
st.setAvg(pp.avg(st.getTot()));
st.setHak(pp.hak(st.getAvg()));
st.out();
}
}
import java.text.DecimalFormat;
public class ScoreTest {
String name;
int kor,eng,math;
int tot;
double avg;
char grade;
public void out() {
DecimalFormat df = new DecimalFormat("#,#00.00");
System.out.println("이름 : "+name);
System.out.println("국어 : "+kor);
System.out.println("영어 : "+eng);
System.out.println("수학 : "+math);
System.out.println("총점 : "+tot);
System.out.println("평균 : "+df.format(avg));
System.out.println("학점 : "+grade(avg));
}
char grade(double avg) {
switch ((int)avg/10) {
case 10:
case 9:grade='A';break;
case 8:grade='B';break;
case 7:grade='C';break;
case 6:grade='D';break;
default:grade='F';break;
}
return grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
}
import java.util.Scanner;
public class Input {
public String munja(String str,Scanner sc) {
System.out.println(str+"을 입력하시오");
return sc.next();
}
public int number(String str,Scanner sc) {
System.out.println(str+"점수를 입력하시오");
return sc.nextInt();
}
}
public class Process {
public int tot(int kor,int eng,int math) {
return (kor+eng+math);
}
public double avg(int tot) {
return (double)tot/3;
}
public char grade(double avg) {
char grade;
switch ((int)avg/10) {
case 10:
case 9:grade='A';break;
case 8:grade='B';break;
case 7:grade='C';break;
case 6:grade='D';break;
default:grade='F';break;
}
return grade;
}
}
import java.util.Scanner;
public class MainTest {
public static void main(String[] args) {
ScoreTest st = new ScoreTest();
Input ip = new Input();
Scanner sc = new Scanner(System.in);
Process pc = new Process();
st.setName(ip.munja("이름", sc));
st.setKor(ip.number("국어", sc));
st.setEng(ip.number("영어", sc));
st.setMath(ip.number("수학", sc));
st.setTot(pc.tot(st.getKor(),st.getEng(),st.getMath()));
st.setAvg(pc.avg(st.getTot()));
st.setGrade(pc.grade(st.getAvg()));
st.out();
}
}
package day10_Sample;
public class Class1 {
//이름,전화번호,나이,주소를 입력해서 출력
String name,phone,add;
int age;
public void out() {
System.out.println("이름 : "+name);
System.out.println("전화번호 : "+phone);
System.out.println("나이 : "+age);
System.out.println("주소 : "+add);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package day10_Sample;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Class1 cc = new Class1();
Scanner sc = new Scanner(System.in);
Input in = new Input();
cc.setName(in.munja("이름", sc));
cc.setPhone(in.munja("전화번호", sc));
cc.setAge(in.number("나이", sc));
sc.nextLine();
cc.setAdd(in.munja("주소", sc));
cc.out();
}
}
package day10_Sample2;
public class Class1 {
//두수를 입력하여 합을 구하는 클래스 생성
int a,b;
public void out() {
System.out.println(a+"와 "+b+"의 합 : "+(a+b));
}
public Class1(int a, int b) {
super();
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
package day10_Sample2;
import java.util.Scanner;
public class Input {
public String munja(String str,Scanner sc) {
System.out.print(str+"를 입력");
return sc.nextLine();
}
public int number(String str,Scanner sc) {
System.out.print(str+"를 입력");
return sc.nextInt();
}
}
package day10_Sample2;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Class1 cl = new Class1(10, 50);
Input in = new Input();
Scanner sc = new Scanner(System.in);
cl.setA(in.number("숫자 A", sc));
cl.setB(in.number("숫자 B", sc));
cl.out();
}
}
package day10_Sample3;
import java.text.DecimalFormat;
public class Class2 {
//수험번호 이름 국어 영어 수학 과학->총점 평균 학점 결과
//결과는 국어,영어,수학,과학이 모두 40이상이면서 평균이 60이상이면 "합격" 아니면 "불합격"
String su;
String name;
int kor,eng,math,sc;
int tot;
double avg;
char grade;
String fi;
public void out() {
DecimalFormat df = new DecimalFormat("#,#00.00");
System.out.println("수험번호 : "+su);
System.out.println("이름 : "+name);
System.out.println("국어 : "+kor);
System.out.println("영어 : "+eng);
System.out.println("수학 : "+math);
System.out.println("과학 : "+sc);
System.out.println("총점 : "+tot);
System.out.println("평균 : "+df.format(avg));
System.out.println("학점 : "+grade);
System.out.println("결과 : "+fi);
}
public String getSu() {
return su;
}
public void setSu(String su) {
this.su = su;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getSc() {
return sc;
}
public void setSc(int sc) {
this.sc = sc;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
public String getFi() {
return fi;
}
public void setFi(String fi) {
this.fi = fi;
}
}
package day10_Sample3;
import java.util.Scanner;
public class Input {
public String munja(String str,Scanner sc) {
System.out.println(str+"를 입력");
return sc.nextLine();
}
public int number(String str,Scanner sc) {
System.out.println(str+"를 입력");
return sc.nextInt();
}
}
package day10_Sample3;
public class Process {
public int tot(int kor,int eng,int math,int sc) {
return (kor+eng+math+sc);
}
public double avg(int tot) {
return (double)tot/4;
}
public char grade(double avg) {
char grade;
switch ((int)avg/10) {
case 10:
case 9:grade='A';break;
case 8:grade='B';break;
case 7:grade='C';break;
case 6:grade='D';break;
default:grade='F';break;
}
return grade;
}
public String fi(int kor,int eng,int math,int sc,double avg) {
String fi;
if(kor>=40&&eng>=40&&math>=40&&sc>=40&&avg>=60) {
fi = "합격";
}
else
{
fi = "불합격";
}
return fi;
}
}
package day10_Sample3;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//수험번호 이름 국어 영어 수학 과학->총점 평균 학점 결과
//결과는 국어,영어,수학,과학이 모두 40이상이면서 평균이 60이상이면 "합격" 아니면 "불합격"
Class2 cc = new Class2();
Input in = new Input();
Scanner sc = new Scanner(System.in);
Process pr = new Process();
cc.setSu(in.munja("수험번호", sc));
cc.setName(in.munja("이름", sc));
cc.setKor(in.number("국어", sc));
cc.setEng(in.number("영어", sc));
cc.setMath(in.number("수학", sc));
cc.setSc(in.number("과학", sc));
cc.setTot(pr.tot(cc.getKor(), cc.getEng(), cc.getMath(), cc.getSc()));
cc.setAvg(pr.avg(cc.getTot()));
cc.setGrade(pr.grade(cc.getAvg()));
cc.setFi(pr.fi(cc.getKor(), cc.getEng(), cc.getMath(), cc.getSc(), cc.getAvg()));
cc.out();
}
}
'JAVA' 카테고리의 다른 글
221220_Interface (0) | 2022.12.20 |
---|---|
221219_Class (0) | 2022.12.19 |
221215_Class (0) | 2022.12.15 |
221214_Method,Class (0) | 2022.12.14 |
221213_Method (0) | 2022.12.13 |
댓글