JAVA코딩
조회수 789회
안녕하세요,자바 코딩이필요해서 그런데, 소스코딩해봤는데 수정 좀해주세요 ㅜㅡㅜ 속성만 가지는 클래스가 주어졌을 때 프로그램을 완성하는 문제에요, main이 있는 class 이름은 A01이고,
class Box1 { int a; int b; } 이 주어진다.
class Box1을 이용하여 두개의 객체를 생성하라. 각 객체의 객체속성변수에 각각 임의의 정수값을 저장하라. 두객체의 a값을 더하여 출력하고, 두객체의 b값을 더하여 출력하는 프로그램을 작성하세요. 빠른답 부탁 드릴게요~~~~~~~~~~ㅜㅡㅜ class Box1 { int a; int b; } public class A01 { public static void main(String args[]) { Box1 mybox1 = new Box1(); Box1 mybox2 = new Box1(); int vol1, vol2;
Box1.a = 78;
Box1.b = 145;
Box2.a = 48;
Box2.b = 45;
vol1 = mybox1.width * Box1.a + Box1.b;
System.out.println("첫 번째 박스의 합은 " + vol1 + "입니다");
vol2 = mybox2.width * Box2.a + Box2.b;;
System.out.println("두 번째 박스의 합은 " + vol2 + "입니다");
} }
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
class A01{ static class Box1{ public Box1(int a, int b){ this.a = a; this.b = b; } private int a; private int b; public int getA(){ return this.a; } public int getB(){ return this.b; } } public static void main(String[] args){ Box1 box1 = new Box1(10, 20); Box1 box2 = new Box1(20, 30); System.out.println("box1.a + box2.a = " + sum(box1.getA(), box2.getA())); System.out.println("box1.b + box2.b = " + sum(box1.getB(), box2.getB())); } private static int sum(int a, int b){ return a+b; } }
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력