Question

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) {

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

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

68a.

40 41 42 43 44 45 46 47 48 class Demo public static void main(String[] args) { //680 ArrayList<Employee> al = new ArrayList<E

Sample Output :

Run: Demo X → If 1 C:\Program Files\Java\jdk-13\bin\java.exe [101, 102, 103, 104, 105, 106] (sam, sameer, shyam, ram, rames

Program Code to Copy

import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.Collectors;
class Demo{
    public static void main(String[] args) {
        //68a
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(101,"sam",1000));
        al.add(new Employee(102,"sameer",2000));
        al.add(new Employee(103,"shyam",3000));
        al.add(new Employee(104,"ram",4000));
        al.add(new Employee(105,"ramesh",5000));
        al.add(new Employee(106,"sameera",60000));
        ArrayList<Integer> ids =  new ArrayList(al.stream().map(x->x.getId()).collect(Collectors.toList()));
        ArrayList<String> names =  new ArrayList(al.stream().map(x->x.getName()).collect(Collectors.toList()));
        ArrayList<Integer> sals =  new ArrayList(al.stream().map(x->x.getSal()).collect(Collectors.toList()));
        System.out.println(ids);
        System.out.println(names);
        System.out.println(sals);
    }
}

68b

Program Code Screenshot

40 41 42 43 44 45 46 class Demo{ public static void main(String[] args) { //680 ArrayList<Employee> al = new ArrayList<Employ

Sample Output :

Run: 个 Demo C:\Program Files\Java\jdk-13\bin\java.exe [Employee@649d209a, Employee@6adca536, Employee@357246de, Employee@28

Program Code to Copy

import java.util.ArrayList;
import java.util.HashSet;
import java.util.stream.Collectors;
class Demo{
    public static void main(String[] args) {
        //68a
        ArrayList<Employee> al = new ArrayList<Employee>();
        al.add(new Employee(101,"sam",10000));
        al.add(new Employee(102,"sameer",2000));
        al.add(new Employee(103,"shyam",30000));
        al.add(new Employee(104,"ram",4000));
        al.add(new Employee(105,"ramesh",15000));
        al.add(new Employee(106,"sameera",60000));
        ArrayList<Employee> highSal =  new ArrayList(al.stream().filter(x->x.getSal()>5000).collect(Collectors.toList()));
        System.out.println(highSal);
    }
}

68c.

Program Code Screenshot :

Main.java import java.util. HashSet; 1 2 3 4 5 on 7 class Demo{ public static void main(String[] args) { // Create hash set H

Sample Output :

Run: Demo X C:\Program Files\Java\jdk-13\bin\java.exe Integers [3, 4, 5] IR Process finished with exit code o

Program Code to Copy

import java.util.HashSet;

class Demo{
    public static void main(String[] args) {
        //Create hash set
        HashSet<Integer> ints = new HashSet<>();
        //Add ints
        ints.add(4);
        ints.add(3);
        ints.add(4);
        ints.add(5);
        System.out.println("Integers "+ints);
    }
}

68d.

1 2 3 Main.java import java.util.HashSet; import java.util.objects; class Employee private int id; private String name; priva

38 39 @ 40 41 @Override public boolean equals(object o) { if (this o) return true; if (o == null || getClass() != 0.getClass(

Sample Output :

Run: Demo C:\Program Files\Java\jdk-13\bin\java.exe ... [Employee{id=101, name=sam, sal=10000}, Employee{id=103, name=sh

Program Code to Copy

import java.util.HashSet;
import java.util.Objects;
class Employee{
    private int id;
    private String name;
    private int sal;

    public Employee(int id, String name, int sal) {
        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 getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return id == employee.id &&
                sal == employee.sal &&
                Objects.equals(name, employee.name);
    }

    //Hashcode and equals method
    @Override
    public int hashCode() {
        return Objects.hash(id, name, sal);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sal=" + sal +
                '}';
    }
}

class Demo{
    public static void main(String[] args) {
        //68a
        HashSet<Employee> al = new HashSet<>();
        al.add(new Employee(101,"sam",10000));
        al.add(new Employee(101,"sam",10000));
        al.add(new Employee(103,"shyam",30000));
        al.add(new Employee(104,"ram",4000));
        al.add(new Employee(103,"shyam",30000));
        al.add(new Employee(106,"sameera",60000));

        System.out.println(al);
    }
}
Add a comment
Know the answer?
Add Answer to:
Java Do 68a, 68b, 68c, 68d. Show code & output. 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 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    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) { 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...

  • What is the final value of the count field? public class Person { private String name;...

    What is the final value of the count field? public class Person { private String name; private int age; private static int count=0; public Person(String name, int age) { this.name = name; this.age age; count++; } public static void main(String[] args) { Person a - new Person("John Doe". 35): Person b new Person("Jane Doe", 38): for(int i=0;i<5;i++) Person tempa

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • 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!

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

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