Question

In: Computer Science

Create a generic method to print objects in java. Include a tester class.

Create a generic method to print objects in java. Include a tester class.

Solutions

Expert Solution

Here we will create a class employee and we will declare instance variables for the class along with getter and setter.

All the comments are written in he codes to have a better understanding of the program.

We have two classes :-

1. Employee

2. Tester

In tester class we have created a generic method to call the objects of the employee class.

Codes for the employee class:

public class Employee {

        //Instance variables of the class
        private Integer id;
        private String name;

        //Setter and getter methods for the instance variables
        public Integer getId() {
                return id;
        }

        public void setId(Integer id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        //override the toString class for a better and customised output view
        @Override
        public String toString(){
                return "Employee ID = " + id +", name = " +name;
        }
}

Codes for the Tester class:

public class Tester {
        public static void main(String[] args){
                //Creating 2 objects of employee class
                Employee e1 = new Employee();
                e1.setId(123);
                e1.setName("Danny");

                Employee e2 =new Employee();
                e2.setId(1234);
                e2.setName("Robert");

                //creating an array of Employee and assigning the objects of employee to it
                Employee empArray[] =new Employee[2];
                empArray[0]= e1;
                empArray[1]= e2;

                //calling the display method which is a generic method to display the objects of the class.
                display(empArray);
        }

        //creation of the generic method named display which takes an array as a parameter.
        private static <E> void display(E[] elements){
                for (E e: elements) {
                        System.out.println(e);
                }
        }
}

All the comments are written in the program for better understanding

The output of the program:


Related Solutions

java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic...
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic type passed to the class. (1 point) Create a constructor that populate an array list filled with the generic type through inserting new elements into the specified location index-i in the list. (1 point) Implement mergeSort using a list and recursion. (2 points) Write the main method to test your program and use System.nanoTime() to find out the speed of each step of your...
IN JAVA, For this exercise, you will create your own example of using a generic method....
IN JAVA, For this exercise, you will create your own example of using a generic method. First write a program that calls a method to multiply two integers and return the integer result. Then modify that program to make the method generic, calling it with two generic data types (T1 and T2) and then returning the result in the same type (also needs to be generic). Demonstrate your program by calling the method two times, once with an integer and...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
In java please create a class with a method to be able to add ints into...
In java please create a class with a method to be able to add ints into a linked List and to be able to print out all the values at the end. public class Node { int data; Node next;
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT