A sentimental robot

Object-oriented style score management 본문

Java

Object-oriented style score management

GOD03219 2017. 12. 28. 13:50

import java.util.Scanner;

public class Hello2 {

 int[] score;
 float avg;
 String name;

 public Hello2() {    // 생성자에서 필드 초기화
  score = new int[4];

 }

 public void setName(String a) {
  name = a;
 }

 public void setKor(int k) {
  score[0] = k;

 }

 public void setEng(int e) {
  score[1] = e;

 }

 public void setMat(int m) {
  score[2] = m;
 }

 public String getName() {
  return name;
 }

 public int getKor() {
  return score[0];

 }

 public int getEng() {
  return score[1];
 }

 public int getMat() {
  return score[2];
 }

 public int getTotal() {
  return score[3] = score[0] + score[1] + score[2];
 }

 public float getAvg() {
  return avg = score[3] / 3.f;
 }

 public static void main(String[] ar) {
  Scanner sc = new Scanner(System.in);
  int student;
  

  System.out.print("The number of student : ");
  student = sc.nextInt();


  Hello2[] ss = new Hello2[student];  // 학생 수만큼 객체배열 참조변수 만들기


  for (int i = 0; i < ss.length ; i++)      // 객체만들기

   ss[i] = new Hello2();


  for (int i = 0; i < ss.length ; i++) {


   System.out.print("name input : ");
   ss[i].setName(sc.next());
   System.out.print("kor input : ");
   ss[i].setKor(sc.nextInt());
   System.out.print("eng input : ");
   ss[i].setEng(sc.nextInt());
   System.out.print("mat input : ");
   ss[i].setMat(sc.nextInt());


  }


  // print ALL
  
for (int i = 0; i < ss.length ; i++) {
   System.out.print(ss[i].getName() + " \t");
   System.out.print(ss[i].getKor() + " \t");
   System.out.print(ss[i].getEng() + " \t");
   System.out.print(ss[i].getMat() + " \t");
   System.out.print(ss[i].getTotal() + " \t");
   System.out.println(ss[i].getAvg() + " \t");
  }
 }

}

'Java' 카테고리의 다른 글

This  (0) 2017.12.28
Object oriented style exercise  (0) 2017.12.28
The elements of "Class"  (0) 2017.12.28
Score Management[Three dimensional]  (0) 2017.12.28
Score Management[Two dimensional]  (0) 2017.12.28