[문제]
과일 판매량 구하기
월별 매출합계도 구하시오
[클래스]
Fruit
[필드]
pum, jan, feb, mar, tot
sumJan, sumFeb, sumMar
[메소드]
생성자(품명, 1월, 2월, 3월)
calcTot()
display()
public static void output()
[클래스]
FruitMain
[실행결과]
---------------------------------------
PUM JAN FEB MAR TOT
---------------------------------------
사과 100 80 75 255
포도 30 25 10 xxx
딸기 25 30 90 xxx
---------------------------------------
xxx xxx xxx output()로 처리
package class__;
public class Fruit {
private String pum;
private int jan, feb, mar, tot;
private static int sumJan, sumFeb, sumMar;
public Fruit(String pum, int jan, int feb, int mar) {
this.pum = pum;
this.jan = jan;
this.feb = feb;
this.mar = mar;
sumJan += this.jan;
sumFeb += this.feb;
sumMar += this.mar;
}
public void calcTot() {
tot = jan + feb + mar;
}
public void display() {
System.out.println(this.pum + "\t"
+ this.jan + "\t"
+ this.feb + "\t"
+ this.mar + "\t"
+ this.tot);
}
public static void output() {
System.out.println("\t" + sumJan + "\t" + sumFeb + "\t" + sumMar);
}
}
package class__;
public class FruitMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fruit[] ar = new Fruit[3];
ar[0] = new Fruit("사과", 100, 80, 75);
ar[1] = new Fruit("포도", 30, 25, 10);
ar[2] = new Fruit("딸기", 25, 30, 90);
System.out.println("---------------------------------------");
System.out.println("PUM\tJAN\tFEB\tMAR\tTOT");
System.out.println("---------------------------------------");
for(int i=0; i<ar.length; i++) {
ar[i].calcTot();
ar[i].display();
}
System.out.println("---------------------------------------");
Fruit.output();
}
}
---------------------------------------
PUM JAN FEB MAR TOT
---------------------------------------
사과 100 80 75 255
포도 30 25 10 65
딸기 25 30 90 145
---------------------------------------
155 135 175
'HOMEWORK' 카테고리의 다른 글
DAY 6 - HOMEWORK - 성적 처리 프로그램 / 성적계산 (2024.07.10) (0) | 2024.08.13 |
---|---|
DAY 8 - HOMEWORK 치환 / 사지선다형 (2024.07.12) (0) | 2024.08.12 |
DAY 10 - HOMEWORK - 달력 (2024.07.16) (0) | 2024.08.12 |
DAY 11 - HOMEWORK - SungJukDTO / SungJuk (2024.07.17) (0) | 2024.08.12 |
DAY 29 - CSS HOMEWORK (2024.08.12) (0) | 2024.08.12 |