오보에블로그
week6:class 본문
Problem 1:School
1.define 'Class Student'
2.Define 'five student' using "Constructor' in Student[]
Name | x | y | Score | Sex |
G-Drangon | 23 | 22 | 85 | Boy |
Naruto | 66 | 35 | 75 | Boy |
Dva | 33 | 46 | 80 | Girl |
Teemo | 14 | 55 | 95 | Boy |
Sona | 87 | 75 | 100 | Girl |
3.Define 'respones() method'.which print name + 'Yes'
Ex1)Dva Yes
1.Define ‘callStudents(Student[], value) method’
2.can call students conditionally. (It should use ‘response()’ in student class)
//Ex)(95 →the student`s score >= 95) callStudent(students, 95) //print) Teemo Yes Sona Yes |
Problem 1:School(using static)
1.Define ‘numStudents()’ in student class
//Ex)Student Genji = new Student(“Genji”, 32, 15, 80, “Boy”); Student Sombra = new Student(“Sombra”,15, 90, 85, “Girl”); Genji.numStudents()Sombra.numStudents() //Print) 2 2 |
Problem 2: shortestDistanceStudent()
1.Input N
2.N size of students array, so there are N students
- x,y,Score → random value
Name : Student1 ~ StudentN
x : 0 ~ 100 // double, using Math.random()
y : 0 ~ 100 // double, using Math.random()
Score : 0 ~ 100 // double, using Math.random()
Sex : Boy or Girl // 50%, using Math.random()
3.Define printStudents(students[]) : print all informatin of students
(hint: use System.out.printf(“%.2f”,x))
4.Define shortestDistanceStudents(students[]) :
print the two students closest to each other
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import java.util.Scanner; class Student{ String name; double x; double y; double score; String sex; static int NumStudents; Student(String myname, double myx, double myy, double myscore, String mysex ){ name = myname; x = myx; y = myy; score = myscore; sex = mysex; NumStudents +=1; } void response() { System.out.println(name + " Yes"); } void numStudents() { System.out.println(NumStudents); } } public class School { static void callStudent(Student mystudent[] ,int checkscore) { for(int i=0; i<5; i++) { if(mystudent[i].score>=checkscore) { mystudent[i].response(); } } } static void printStudents(Student mystudent[]) { for(int i=0;i<mystudent.length;i++) { System.out.print(mystudent[i].name+" "); System.out.printf("%.2f ",mystudent[i].x); System.out.printf("%.2f ",mystudent[i].y); System.out.printf("%.2f ",mystudent[i].score); System.out.println(mystudent[i].sex); } } static void shortestDistanceStudent(Student mystudent[]){ double maxdistance = 1000000000; int maxi=0, maxj=0; for(int i=0; i<mystudent.length; i++) { for(int j=0; j<mystudent.length;j++) { if(i == j) { continue; } double distance = (mystudent[i].x-mystudent[j].x) * (mystudent[i].x-mystudent[j].x) + (mystudent[i].y-mystudent[j].y)*(mystudent[i].y - mystudent[j].y); if(distance < maxdistance){ maxdistance = (mystudent[i].x-mystudent[j].x) * (mystudent[i].x-mystudent[j].x) + (mystudent[i].y-mystudent[j].y)*(mystudent[i].y - mystudent[j].y); maxi= i; maxj=j; } } } System.out.println(mystudent[maxi].name+" and "+mystudent[maxj].name); } public static void main(String args[]) { Student student[] = new Student[5]; student[0] = new Student("G-Dragon", 23, 22, 85, "Boy"); student[1] = new Student("Naruto", 66, 35, 75, "Boy"); student[2] = new Student("Dva", 33, 46, 80, "Girl"); student[3] = new Student("Teemo", 14, 55, 95, "Boy"); student[4] = new Student("Sona", 87, 75, 100, "Girl"); Scanner InputNum = new Scanner(System.in); int N = InputNum.nextInt(); Student lovelystudent[] = new Student[N]; for(int i=0;i<N;i++) { lovelystudent[i] = new Student("Student"+(i+1),Math.random()*100,Math.random()*100,Math.random()*100,Math.random()<0.5?"Girl":"Boy"); } printStudents(lovelystudent); shortestDistanceStudent(lovelystudent); } } | cs |
'STEADYSTUDY > Etc' 카테고리의 다른 글
2447_별찍기 - 10 (0) | 2018.02.06 |
---|---|
자바실습 Week10 - FILE IO (0) | 2017.11.22 |
Collection & Generics (0) | 2017.11.16 |
추상클래스 (abstract class) 사용 (0) | 2017.11.08 |
클래스 상속과 객체 (0) | 2017.11.01 |