리턴에 관해서 질문..있어요
미투리
안녕하세요.. 저 멀리 외국에서 자바를 처음 배우는 학생인데요..
학교에서 선생님이 직접 가르쳐 주시는게 아니라
혼자 공부하는거라 그런지.. 너무 어렵네요..
조금 도와주세요
메쏘드를 이용해서 사각형의 넓이 를 구하는건데요..
리턴을 이용하지 않고서도.. 가능한것 같기도 하고..
음냥..
일단 제가 짠 프로그램이 맞나 봐주실 수 잇나요?
Write a class named Rectangle to represent rectangles. The class should describe a rectangle’s height and width. The Rectangle class will have one method to calculate area. ?xml:namespace prefix = o ns = urn:schemas-microsoft-com:office:office /?xml:namespace prefix = o /
Write a tester class to test your Rectangle class. Your tester class should create two Rectangle objects and assign different widths and heights for each of the two instances. Invoke the method to calculate the area of the rectangle for each instance. Output the area for each rectangle instance.
이건 문젠데요 렉탱글이라는 클래스는 사각형의 너비와 높이에대해 언급해야하고.. 넓이를 구하라는 메쏘드를 포함해야하구요
테스터 클래스에는 2개의 렉탱글 오브젝트를 만들어야하고 각 각 다른 숫자들을 지정해줘야해요.
class Rectangle
{
//method
int multiply(int width, int height)
{
int result = width * height;
return result;
}
}
class RectangleTester
{
//execution of RectangleTester begins with main{} method
public static void main (String[] args)
{
Rectangle R1 = new Rectangle();
int result1 = R1.multiply(6,7);
Rectangle R2 = new Rectangle();
int result2 = R2.multiply(4, 10);
System.out.println(The area of rectangle 1 is + result1);
System.out.println(The area of rectangle 2 is + result2);
}
}
이게 맞는지 봐주세요..
이렇게 말고 void를 사용 하는 법으로도 만들어서
잘 작동했는데...흠..어떤걸 써야하는건지..
class Rectangle
{
// instance variables
int width;
int height;
int area;
// methods
void area()
{
area = width * height;
System.out.println (The area of rectangle is + area):
}
}
class RectangleTester
{
public static void main (String[] args)
{
Rectangle R1 = new Rectangle();
R1.width = 3;
R1.height = 7;
R1.area();
Rectangle R2 = new Rectangle();
R2.width = 5;
R2.height = 10;
R2.area();
} //end of main() method
} //end of RabbitTester class
두개 다 맞는건가요..?
-
매1혻적
네엡 감사합니다~
-
소유
그리고 void는 리턴형이지 접근 지정자가 아닙니다
-
첫빛
첫번째는 리턴방식으로 메인메소드에서 multiply 메소드에 값을 너어준후 그 결과값을
리턴받아서 메인메소드 에서 처리해주는 것이고
두번쨰는 메인메소드에서 Rectangle클래스에 변수값들을 세팅한후 area메소드를 호출하는
방식이네요...세터 없어도 될꺼 같네여 -
가랑비
그럼.. 두가지 다 맞는거긴.. 한거네요?
음.. 셋터는 뭔가요;;
어렵네요;;ㅠ -
곰탱이
void로 할경우엔 셋터가 있어야 할거 같은데요 바로 접근이 불가능 할꺼 같은데요..
-
새밝
return 방식은 게산후 값을 리턴받아서 다시 처리해주는 방식이고
void 방식은 리턴값없이 void 메소드에서 바로 처리해주는 방식이네요..
방법에 차이일뿐 필요한걸 그때 그때 마춰서 쓰시면 되겠네요