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...
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...
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...
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...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
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...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Write a tester program to test the mobile class defined below. Create the class named with...
Write a tester program to test the mobile class defined below. Create the class named with your id (for example: Id12345678) with the main method. Create two mobiles M1 and M2 using the first constructor. Print the characteristics of M1 and M2. Create one mobile M3 using the second constructor. Change the id and brand of the mobile M3. Print the id of M3. Your answer should include a screenshot of the output. Otherwise, you will be marked zero for...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method that does the following: Prompt the user to enter a number of inches Read the number of inches entered from the console Convert the inches into an equivalent number of miles, yards, feet, and inches. Output the results to the console. For example, if a user entered 100000 inches, the program would output: 100000 inches is equivalent to: Miles: 1 Yards: 1017 Feet: 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT