Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this data structure hw, need your help Generic Method Copy the class and run the program public class PrintArrayElements public static void main(String[] args) //

this data structure hw, need your help image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Generic Method Copy the class and run the program public class PrintArrayElements public static void main(String[] args) // create arrays of Integer, Double and Character Integer) integerArray = {1,2,3,4,5,6); Double doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; Character characterArray = {'H', 'E', 'L', 'L', '0'); System.out.printf("Array integerArray contains:%n"); printArray(integerArray); // pass an Integer array System.out.printf("%nArray doubleArray contains:9n"); printArray(doubleArray); // pass a Double array System.out.printf("%nArray characterArray contains:%n"); printArray(character Array); // pass a Character array // method printArray to print Integer array public static void printArray(Integer inputArray) // display array elements for (Integer element:inputArray) System.out.printf("%s", element); System.out.println(); // method printArray to print Double array public static void printArray(Double[inputArray) // display array elements for (Double element:inputArray) System.out.printf("%s", element): System.out.println(); // method printArray to print Character array public static void printArray(Character inputArray) // display array elements for (Character element:inputArray) OM C 77 methoprintArray to print Double array public static void printArray(Double[inputArray) W/ display array elements for (Double element: inputArray) System.out.printf("%s", element); System.out.println(); // method printArray to print Character array public static void printArray(Character[] inputArray) // display array elements for (Character element:inputArray) System.out.printf("%s", element); System.out.println(); } // end class Overloaded Methods*** The class above has three different methods called printarray but each one has parameter of different data type. We call this Note the three methods performed the same operations but with different types. We can replace the three methods with one generic method as follows W/ generic method printArray public static void printArray() inputArray) 7/ display array elements for (T element : inputArray) System.out.printf("%s", element); System.out.println(); Notes 1- All generic method declarations have a type parameter section (ST), which appears before method return type. 2- Type parameter is an identifier that specifies a generic type name. 3. Type parameter can be used to declare return type, parameter types, local variables, 4. Type parameter can represent only objects, not primitive types. Exercise Add another generic method shift that receives an array and shifts its element one position to the left as depicted by the following figure 0 1 2 3 4 So after calling the method, the above array will be as follows 5 731 4 Call your method in the main with different types of arrays, print the array before and after calling the function. Generic Class Assume we would like to implement the concept of a queue to process set of objects, we want our class to be general for any type of objects. Remember the basic idea of queue concept is adding new elements at the end and removing elements from the beginning. import java.util.ArrayList; public class Queue { public ArrayList elements new ArrayList(); public void add(Te) elements.add(e); public I remove() return elements.remove(0); Exercises 1. Create new class Employee that has id(int), namedString), salary(double). a. add constructor, setter getter methods b. add method to String returns string contains all data 2. Create new class Car that has plateNo(int), brand(String). a. add constructor, setter/getter method b. add method toString returns string contains all data, 3- Copy class Queue above a. add method print to print data of all elements in the queue b add method reverse that reverse elements order in the list. 4. Create class Generic App which has main method, a create two queues one for Employee objects and one for Car objects b. Add objects to cach queue and test the methods

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

Who does the ACA impact and who does it not?

Answered: 1 week ago