OOP Lab 12

Due Date:

June 7,   23 : 59

  • Submit your assignment using the following file format:

   LabNumber_StudentName_Student_ID.zip

  Example: Lab12_Hongkildong_201620505.zip

  • The  zip file will contain two types of files, namely:

1)      Report file with file format “Report_Lab number” (eg. report_12) to answer theory questions and to write the screen shot of your program.

2)      Source code file that contains codes of classes to answer programming questions.


 Objectives

·       How to write a generic class

·       How to write a generic method

·       How to create an object of a generic class

·       How to invoke a generic method


Exercises

1.  Complete the given partial code (see Q1_partial Code) (10pt)

The program has two generic methods, namely:  reverseArray () and printArray().  reverseArray() method  reverses the order of elements of an array with different types. PrintArray() display  different types of array before calling reverseArray() and  after calling reverseArray () using a generic method. This is similar to the code in Fig. 20.3.  your slide.

Hence, Complete the program by filling the missing parts of the given source code.

 

프로그램에는 reverseArray()와 printArray()라는 두 가지 일반적인 방법이 있다.

reversArray() 방식은 다른 유형의 배열 요소 순서를 반대로 한다.

PrintArray()는 reverseArray()를 호출하기 전과 generic 방법을 사용하여 reverseArray()를 호출한 후에 서로 다른 유형의 어레이를 표시한다. 이것은 당신의 슬라이드 그림 20.3의 코드와 유사하다.
따라서 지정된 소스 코드의 누락된 부분을 채워 프로그램을 완료하십시오.

 

 


2. Complete the given partial code (see Q2-partialCode) (10pt).

In Fig.20.3, the program has printArray () generic method. Assume that you want to add another generic printArray () method that overloads the existing generic printArray () method which takes two additional integer arguments, namely  lowSubscriptand highSubscript. A call to this method prints only the designated portion of the array. 

Validate lowSubscript and higSubscript.

If either of them is out of range, the overloaded printArray () method should throw an InvalidSubscriptException;

otherwise, printArray () should return the number of elements printed. Then modify main () to use both versions of printArray () on integerArray, doubleArray and characterArray. Test all capabilities of both versions of printArray().

Hence, complete the program by filling the missing parts of the given source code.

 

그림 20.3에서 프로그램은 printArray()의 일반적 방법을 가지고 있다.

두 개의 정수 인수(lowSubscript 및 highSubscript)를 추가로 사용하는, 기존의 일반 printArray() 메서드를 오버로드하는,

른 일반 printArray() 메서드를 추가하려고 한다고 가정해 보십시오.

 

이 방법을 호출하면 배열의 지정된 부분만 print된다. lowSubscript 및 highSubscript를 확인하십시오.

둘 중 하나가 범위를 벗어나면 과부하된 printArray() 메서드가 InValidSubscriptException 해야한다.

예외. 그렇지 않으면 인쇄 배열()은 인쇄된 요소의 수를 반환해야 한다.

 

그런 다음 주()를 수정하여 integerArray, doubleArray 및 characterArray에서 printArray()의 두 버전을 모두 사용하십시오.

두 가지 버전의 printArray()의 모든 기능을 테스트하십시오.


3. Complete the given partial code (see Q3-PartialCode) (10pt).

In Fig.20.3, the program has printArray () generic method. Assume that you want to add another non-generic printArray () method. This method specifically prints an array of Strings in the following tabular format.  Complete the program by filling the missing parts of the given source code.

 

그림 20.3에서 프로그램은 printArray()의 generic 메써드를 가지고 있다. non-generic 않은 printArray() 메서드를 하나 더 추가한다고 가정하십시오. 이 방법은 문자열 배열을 다음과 같은 표 형식으로 인쇄한다. 지정된 소스 코드의 누락된 부분을 채워 프로그램을 완료하십시오.

Array stringArray contains:

One   two   three   four

Five   six   seven   eight

 


4. Complete the given partial code (see the Q3-Partial Code) (10pt).

 A generic isEqualTo() method compares its two arguments with the equals () method and returns true if they are equal and false otherwise.  You can calls the generic isEqualTo()  method with a variety of built-in types, such as  Integers, Strings, Doubles and Object. 

 

a)  Complete the program by filling the missing parts of the given source code.

 

b)  What result do you get when you attempt to run this program?  Include the result in your report file

 

c)  Write your reason for question in b?  Include your reason in the report file.

Depending on the program, we may need to treat basic types of data as objects.
For example, if an argument in a method requires only an object type, data of the base type cannot be used as it is.
At this point, we must first convert the base type of data to an object and then perform the operation.
The class that packages these eight base types of data(byte, short, int, long, float, double, char, boolean) into objects is called the Wrapper class.
The wrapper class receives data for each type as an argument and makes it an object with that value.

So "int", "double" can be used in generic where the default data type is not available.

 

In "isEqualTo" method, we can not use operater(==) in if( ) phrase.

Because the wrapper class is also an object, using the equivalent operator (==) does not compare the values of two instances, but rather the address values of two instances.
Therefore, if you compare two different instances with equivalent operators (==), the false value is always returned.
Therefore, you must use the equal() method to determine exactly whether the values stored in the instance are equal.

 

프로그램에 따라 기본 타입의 데이터를 객체로 취급해야 하는 경우가 있습니다.

예를 들어, 메소드의 인수로 객체 타입만이 요구되면, 기본 타입의 데이터를 그대로 사용할 수는 없습니다.

이때에는 기본 타입의 데이터를 먼저 객체로 변환한 후 작업을 수행해야 합니다.

 

이렇게 8개의 기본 타입에 해당하는 데이터를 객체로 포장해 주는 클래스를 래퍼 클래스(Wrapper class)라고 합니다.

래퍼 클래스는 각각의 타입에 해당하는 데이터를 인수로 전달받아, 해당 값을 가지는 객체로 만들어 줍니다.

이러한 래퍼 클래스는 모두 java.lang 패키지에 포함되어 제공됩니다.

래퍼 클래스도 객체이므로 동등 연산자(==)를 사용하게 되면, 두 인스턴스의 값을 비교하는 것이 아니라 두 인스턴스의 주소값을 비교하게 됩니다.

따라서 서로 다른 두 인스턴스를 동등 연산자(==)로 비교하게 되면, 언제나 false 값을 반환되게 됩니다.

그러므로 인스턴스에 저장된 값의 동등 여부를 정확히 판단하려면 equals() 메소드를 사용해야만 합니다.

 

일반 isEqualTo() 방법은 자신의 두 인수를 equals() 메쏘드로 비교하고 동일하면 true를 반환하고, 그렇지 않으면 false를 반환한다. Integer, String, Doubles, Object 등 다양한 내장형식으로 일반 isEqalTo() 메서드를 호출할 수 있다. 
a) 주어진 소스 코드의 누락된 부분을 채워 프로그램을 완성한다.
b) 이 프로그램을 실행하려고 할 때 어떤 결과를 얻는가? 보고서 파일에 결과 포함
c) 질문 이유를 b에 쓰시겠습니까? 보고서 파일에 이유를 포함하십시오.


5.  Write your own Program (10pt)

 Write a generic class called “Pair” which has two type parameters, namely F and S.

·       This class has two fields. The type of the first field is F and the type of the second filed is S.

·       Add get() and  set()  methods for the first  field of the  pair.

·       Add get() and  set()  methods for the second   field of the  pair

·       In order to test the generic Pair class, write test class that contains the main () method. 

·       The input and output format of the program is given below. 

 

F와 S라는 두 가지 유형 매개변수를 가진 "페어"라는 일반 클래스를 쓰세요.
· 이 class는 두 개의 필드가 있다. 첫 번째 필드의 유형은 F이고 두 번째 필드의 유형은 S이다.
· 클래스의 첫 번째 필드에 get() 과 set() 메써드를 추가해라.
· 클래스의 두 번째 필드에 get() 과 set() 메써드를 추가한다.
· 제너릭 Pair 클래스를 테스트하려면 main() 방법을 포함하는 테스트 클래스를 작성한다. 
· 프로그램의 입력·출력 형식은 아래와 같다.

 

Input: 

          First Field      Second Field 

1                       one

2                       Second

3                      Third

 

Output: 

         Original pair: (1, One) 

         Modified pair: (2, Second) 

         

                   

            

'📌 java > Object-oriented Programming' 카테고리의 다른 글

Homework_13  (0) 2020.06.09
java - ArrayList  (0) 2020.06.09
Homework_11  (0) 2020.05.30
Homework_10  (0) 2020.05.19
Homework_9  (0) 2020.05.13
복사했습니다!