Question

Java

Do 61a, 61b, 61c, 61d. Show Output and Code.

public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) {

61a. public class Demo { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(10

61c. public class Demo { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

61a)

import java.util.*; 
  
public class demo { 
  
    // Function to remove duplicates from an ArrayList 
    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) 
    { 
  
       
        ArrayList<T> newList = new ArrayList<T>(); 
  
        // Traverse through the first list 
        for (T element : list) { 
  
            // If this element is not present in newList 
            // then add it 
            if (!newList.contains(element)) { 
  
                newList.add(element); 
            } 
        } 
  
        // return the new list 
        return newList; 
    } 
  
    
    public static void main(String args[]) 
    { 
  
        // Get the ArrayList with duplicate values 
        ArrayList<Integer> a1 = new ArrayList<Integer>( );
        a1.add(10);
        a1.add(5);
        a1.add(7);
        a1.add(16);
        a1.add(7);
        a1.add(16);
                
  
        // Remove duplicates 
        ArrayList<Integer> 
            newList = removeDuplicates(list); 
  
        // Print the ArrayList with duplicates removed 
        System.out.println("ArrayList with duplicates removed: "
                           + newList); 
    } 
} 

output

ArrayList with duplicates removed: [10, 5, 7, 16]

61b)

import java.util.*; 
  
public class demo { 
  
   
    public static void main(String args[]) 
    { 
  
        // Get the ArrayList with duplicate values 
        ArrayList<Integer> a1 = new ArrayList<Integer>( );
        a1.add(10);
        a1.add(5);
        a1.add(7);
        a1.add(16);
        a1.add(7);
        a1.add(16);
        
        ArrayList<Integer> a2 = new ArrayList<Integer>( );
        a2.add(100);
        a2.add(7);
        a2.add(5);
        a2.add(56);
        a2.add(17);
        a2.add(26);
        
        ArrayList<Integer> a3= new ArrayList<Integer>( );
        a3.addAll(a1);
        a3.addAll(a2);

                
  
        
  
        // Print the ArrayList 
        System.out.println("ArrayList : "
                           + a3); 
    } 
} 

output

ArrayList : [10, 5, 7, 16, 7, 16, 100, 7, 5, 56, 17, 26]

61c)

import java.util.*; 
  
public class demo { 
  
    // Function to remove duplicates from an ArrayList 
    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) 
    { 
  
       
        ArrayList<T> newList = new ArrayList<T>(); 
  
        // Traverse through the first list 
        for (T element : list) { 
  
            // If this element is not present in newList 
            // then add it 
            if (!newList.contains(element)) { 
  
                newList.add(element); 
            } 
        } 
  
        // return the new list 
        return newList; 
    } 
  
    
    public static void main(String args[]) 
    { 
  
        // Get the ArrayList with duplicate values 
        ArrayList<Integer> a1 = new ArrayList<Integer>( );
        a1.add(10);
        a1.add(5);
        a1.add(7);
        a1.add(16);
        a1.add(7);
        a1.add(16);
                
        ArrayList<Integer> a2 = new ArrayList<Integer>( );
        a2.add(100);
        a2.add(7);
        a2.add(5);
        a2.add(56);
        a2.add(17);
        a2.add(26);
        
        ArrayList<Integer> a3= new ArrayList<Integer>( );
        a3.addAll(a1);
        a3.addAll(a2);
        // Remove duplicates 
        ArrayList<Integer> 
            newList = removeDuplicates(a3); 
  
        // Print the ArrayList with duplicates removed 
        System.out.println("ArrayList with duplicates removed: "
                           + newList); 
    } 
} 

output

ArrayList with duplicates removed: [10, 5, 7, 16, 100, 56, 17, 26]

61d)

import java.util.*; 


public class Employee{
    int id,salary;
    String name;
    Employee(int id,String name,int salary)
    {
        this.id=id;
        this.name=name;
        this.salary=salary;
    }
     
}  
public class demo { 
  
   
    public static void main(String args[]) 
    { 
  
        
       
        
        ArrayList<Employee> a1= new ArrayList<Employee>( );
        a1.add(new Employee(101,"sam",1000));
        a1.add(new Employee(102,"sameer",2000));
        a1.add(new Employee(103,"shivam",3000));
        a1.add(new Employee(104,"ram",4000));
        a1.add(new Employee(105,"ramesh",5000));
        a1.add(new Employee(106,"sameera",6000));
        
        ArrayList<Integer> a2=new ArrayList<>();
        for (Employee i : a1) 
        {
            a2.add(i.id);
        }

                
  
        
  
        // Print the ArrayList 
        System.out.println("ArrayList with ids : "
                           + a2); 
    } 
} 

output

ArrayList with ids: [101, 102, 103, 104, 105, 106]

Please do give a thumbs-up, thanks

Add a comment
Know the answer?
Add Answer to:
Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int...

    Java Do 68a, 68b, 68c, 68d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Given: public class ItemTest { private final int id; public ItemTest(int id) { this.id = id;...

    Given: public class ItemTest { private final int id; public ItemTest(int id) { this.id = id; } public void updateId(int newId) { id = newId; } public static void main(String[] args) { ItemTest fa = new ItemTest(42); fa.updateId(69); System.out.println(fa.id); } } What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The attribute id in the ItemTest object remains unchanged. D. The attribute id in the ItemTest object is modified to the new value. E....

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • What is the output of this program? class A { public int i; private int j;...

    What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!

  • output What is the output of the following: class Access public int x; private int y...

    output What is the output of the following: class Access public int x; private int y public void cal(int x, int y) { this.x = x + 1; this.y = y; } public void print() { System.out.print(""+y); } } class AccessSpecifier { public static void main(String args[]) { Access obj = new Access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } } 33 Compilation error 23 None of the available choices Runtime error

  • Code in java Using the template below: public class Lab03 { public static void main(String[] args)...

    Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...

  • Java. What is the output of this code? Ace.java public class Ace { private double a;...

    Java. What is the output of this code? Ace.java public class Ace { private double a; public Ace ( double x) {       a = x; } public void multiply (double f) {       a *= x;         } public String toString () {       return String.format ("%.3f", x); AceDem.java public class AceDemo { public static void main(String args[]) {    Ace card = new Ace (2.133); card.multiply (.5); System.out.println (card); } }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT