SlideShare a Scribd company logo
1 of 3
Assignment 7 Write a generic class Pair which has two type parameters—F and S—each
representing the type of the first and second element of the pair, respectively. Add set and get
methods for the first and second elements of the pair. (Hint: The class header should be public
class Pair.) Write a separate PairTest class to test class Pair. Create 2 Pair types and test your get
and set methods on the following pair types: • Pair p1 takes Integer and String types as a pair
• Pair p2 takes String and Integer PairTest should output enough information to the terminal to
show that your generic class Pair is able to set() and get() different types.
Solution
ANS:
// Pair contains two types.
public class Pair< F, S >
{
private F first; // first element of a pair
private S second; // second element of a pair
// constructor
public Pair( F firstElement, S secondElement )
{
first = firstElement;
second = secondElement;
} // end Pair constructor
// get first
public F getFirst()
{
return first;
} // end method getFirst
// get second
public S getSecond()
{
return second;
} // end method getSecond
// set first
public void setFirst( F firstElement )
{
first = firstElement;
} // end method setFirst
// set second
public void setSecond( S secondElement )
{
second = secondElement;
} // end method setSecond
} // end class Pair
// Generic Pair class testing program.
public class PairTest
{
public static void main( String args[] )
{
// create pair of integer and string
Pair< Integer, String > numberPair =
new Pair< Integer, String >( 1, "one" );
// display original numberPair
System.out.printf( "Original pair: < %d, %s > ",
numberPair.getFirst(), numberPair.getSecond() );
// modify pair
numberPair.setFirst( 2 );
numberPair.setSecond( "Second" );
// display modified numberPair
System.out.printf( "Modified pair: < %d, %s > ",
numberPair.getFirst(), numberPair.getSecond() );
} // end method main
} // end class PairTest

More Related Content

More from bickerstaffinell

a) Describe two ways the researcher could minimise experimenter bias i.docx
a) Describe two ways the researcher could minimise experimenter bias i.docxa) Describe two ways the researcher could minimise experimenter bias i.docx
a) Describe two ways the researcher could minimise experimenter bias i.docx
bickerstaffinell
 
Airport Delays The problems created by increased security measures at.docx
Airport Delays The problems created by increased security measures at.docxAirport Delays The problems created by increased security measures at.docx
Airport Delays The problems created by increased security measures at.docx
bickerstaffinell
 
After reading the article on Big Bottom Market make an analysis of the.docx
After reading the article on Big Bottom Market make an analysis of the.docxAfter reading the article on Big Bottom Market make an analysis of the.docx
After reading the article on Big Bottom Market make an analysis of the.docx
bickerstaffinell
 

More from bickerstaffinell (20)

A- Imagine you were recently hired as a general manager working in a.docx
A-  Imagine you were recently hired as a general manager working in a.docxA-  Imagine you were recently hired as a general manager working in a.docx
A- Imagine you were recently hired as a general manager working in a.docx
 
a) Describe two ways the researcher could minimise experimenter bias i.docx
a) Describe two ways the researcher could minimise experimenter bias i.docxa) Describe two ways the researcher could minimise experimenter bias i.docx
a) Describe two ways the researcher could minimise experimenter bias i.docx
 
All organisations are affected to some extent by external factors and.docx
All organisations are affected to some extent by external factors and.docxAll organisations are affected to some extent by external factors and.docx
All organisations are affected to some extent by external factors and.docx
 
All the following are important uses of -ICD-11-- EXCEPT- Select one-.docx
All the following are important uses of -ICD-11-- EXCEPT- Select one-.docxAll the following are important uses of -ICD-11-- EXCEPT- Select one-.docx
All the following are important uses of -ICD-11-- EXCEPT- Select one-.docx
 
All of the following were discussed in lecture as ways teams create no.docx
All of the following were discussed in lecture as ways teams create no.docxAll of the following were discussed in lecture as ways teams create no.docx
All of the following were discussed in lecture as ways teams create no.docx
 
All of the following statements are true with respect to the bacteria.docx
All of the following statements are true with respect to the bacteria.docxAll of the following statements are true with respect to the bacteria.docx
All of the following statements are true with respect to the bacteria.docx
 
Airport Delays The problems created by increased security measures at.docx
Airport Delays The problems created by increased security measures at.docxAirport Delays The problems created by increased security measures at.docx
Airport Delays The problems created by increased security measures at.docx
 
Agency theory explains manager's behavior- Which one is false- The agm.docx
Agency theory explains manager's behavior- Which one is false- The agm.docxAgency theory explains manager's behavior- Which one is false- The agm.docx
Agency theory explains manager's behavior- Which one is false- The agm.docx
 
After studying our second case with ISIS- we should have a good unders.docx
After studying our second case with ISIS- we should have a good unders.docxAfter studying our second case with ISIS- we should have a good unders.docx
After studying our second case with ISIS- we should have a good unders.docx
 
After reading the article on Big Bottom Market make an analysis of the.docx
After reading the article on Big Bottom Market make an analysis of the.docxAfter reading the article on Big Bottom Market make an analysis of the.docx
After reading the article on Big Bottom Market make an analysis of the.docx
 
A vaccine against HIV Group of answer choices may be impossible due.docx
A vaccine against HIV    Group of answer choices may be impossible due.docxA vaccine against HIV    Group of answer choices may be impossible due.docx
A vaccine against HIV Group of answer choices may be impossible due.docx
 
Additional information for the Income Statement- 1 mterest expense of.docx
Additional information for the Income Statement- 1 mterest expense of.docxAdditional information for the Income Statement- 1 mterest expense of.docx
Additional information for the Income Statement- 1 mterest expense of.docx
 
Addison Ltd owns all of the share capital of Erin Ltd- In relation to.docx
Addison Ltd owns all of the share capital of Erin Ltd- In relation to.docxAddison Ltd owns all of the share capital of Erin Ltd- In relation to.docx
Addison Ltd owns all of the share capital of Erin Ltd- In relation to.docx
 
actually I need answers of all these questions as soon as possible ple.docx
actually I need answers of all these questions as soon as possible ple.docxactually I need answers of all these questions as soon as possible ple.docx
actually I need answers of all these questions as soon as possible ple.docx
 
accumulated depreciation A- is allocated as an expense during future p.docx
accumulated depreciation A- is allocated as an expense during future p.docxaccumulated depreciation A- is allocated as an expense during future p.docx
accumulated depreciation A- is allocated as an expense during future p.docx
 
Accounting and Economic profiss are computed differently and serve dif.docx
Accounting and Economic profiss are computed differently and serve dif.docxAccounting and Economic profiss are computed differently and serve dif.docx
Accounting and Economic profiss are computed differently and serve dif.docx
 
According to the Solow model- Suppose that an earthquake destroys most.docx
According to the Solow model- Suppose that an earthquake destroys most.docxAccording to the Solow model- Suppose that an earthquake destroys most.docx
According to the Solow model- Suppose that an earthquake destroys most.docx
 
According to Culturally-Endorsed Leadership Theory- cultures differ on.docx
According to Culturally-Endorsed Leadership Theory- cultures differ on.docxAccording to Culturally-Endorsed Leadership Theory- cultures differ on.docx
According to Culturally-Endorsed Leadership Theory- cultures differ on.docx
 
According to a study done by Nick Wilson of Otago University Wellingto.docx
According to a study done by Nick Wilson of Otago University Wellingto.docxAccording to a study done by Nick Wilson of Otago University Wellingto.docx
According to a study done by Nick Wilson of Otago University Wellingto.docx
 
A-) What is the difference between encrypting and signing in asymmetri.docx
A-) What is the difference between encrypting and signing in asymmetri.docxA-) What is the difference between encrypting and signing in asymmetri.docx
A-) What is the difference between encrypting and signing in asymmetri.docx
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 

Assignment 7 Write a generic class Pair which has two type parametersF.docx

  • 1. Assignment 7 Write a generic class Pair which has two type parameters—F and S—each representing the type of the first and second element of the pair, respectively. Add set and get methods for the first and second elements of the pair. (Hint: The class header should be public class Pair.) Write a separate PairTest class to test class Pair. Create 2 Pair types and test your get and set methods on the following pair types: • Pair p1 takes Integer and String types as a pair • Pair p2 takes String and Integer PairTest should output enough information to the terminal to show that your generic class Pair is able to set() and get() different types. Solution ANS: // Pair contains two types. public class Pair< F, S > { private F first; // first element of a pair private S second; // second element of a pair // constructor public Pair( F firstElement, S secondElement ) { first = firstElement; second = secondElement; } // end Pair constructor // get first
  • 2. public F getFirst() { return first; } // end method getFirst // get second public S getSecond() { return second; } // end method getSecond // set first public void setFirst( F firstElement ) { first = firstElement; } // end method setFirst // set second public void setSecond( S secondElement ) { second = secondElement; } // end method setSecond } // end class Pair // Generic Pair class testing program. public class PairTest {
  • 3. public static void main( String args[] ) { // create pair of integer and string Pair< Integer, String > numberPair = new Pair< Integer, String >( 1, "one" ); // display original numberPair System.out.printf( "Original pair: < %d, %s > ", numberPair.getFirst(), numberPair.getSecond() ); // modify pair numberPair.setFirst( 2 ); numberPair.setSecond( "Second" ); // display modified numberPair System.out.printf( "Modified pair: < %d, %s > ", numberPair.getFirst(), numberPair.getSecond() ); } // end method main } // end class PairTest