Question

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

72a. class Product{ String name; Int price; Int quantity; 1/do required changes in Product class } Now create HashSet of Prod

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

72a.

code:

import java.util.HashSet;
class Product{
   // data members of class product
   String name;
   int price;
   int quantity;
   // parametrized constructor for initializing data members
   public Product(String name, int price, int quantity) {
       super();
       this.name = name;
       this.price = price;
       this.quantity = quantity;
   }
   // setters and getters for data members
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getPrice() {
       return price;
   }
   public void setPrice(int price) {
       this.price = price;
   }
   public int getQuantity() {
       return quantity;
   }
   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }
  
}
public class Tester {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       // creating objects of class product
       Product product=new Product("Chacolate",20,2);
       Product product2=new Product("FaceWash",50,1);
       // creating HashSet of type product
       HashSet<Product> h=new HashSet<Product>();
       // checking if duplicate present or not
       if(h.contains(product)==false) {
           h.add(product);
       }
       if(h.contains(product2)==false) {
           h.add(product2);
       }
       if(h.contains(product)==false) {
           h.add(product);
       }// printing out the contents of HashSet
       for(Product i:h) {
           System.out.println("Name:"+i.name+" "+"Price:"+i.price+" "+"Quantity:"+i.quantity);
       }

   }

}

output:

Name:FaceWash Price: 50 Quantity: 1 Name:Chacolate Price: 20 Quantity:2

code screenshot:

import java.util.HashSet; class Product // data members of class product String name; int price; int quantity: 11 parametrize

public class Tester { public static void main(String[] args) { // TODO Auto-generated method stub // creating objects of clas

72b.

code:

import java.util.HashMap;
import java.util.Set;

public class SeventyTwob {

   public static void main(String[] args) {
       // creating HashMap with String and Integer
       HashMap<String,Integer> h=new HashMap<String,Integer>();
       // adding key,value pair to HashMap
       h.put("Barry",25);
       h.put("Joe",45);
       h.put("Catilin",23);
       h.put("stark",40);
       // set for storing keys of HashMap
       Set<String> keys=h.keySet();
       // enhanced for loop for printing HashMap values
       for(String key:keys) {
           System.out.println(key+" "+h.get(key));
       }

   }

}

output:

Joe 45 stark 40 Barry 25 Catilin 23

code screenshot:

import java.util.HashMap; import java.util.Set; public class SeventyTwob { public static void main(String[] args) { // creati

72c.

code:

import java.util.HashMap;
import java.util.Set;

class Employee{
   // data members of class Employee
   private int id;
   private String name;
   private int sal;
   // parametrized constructor to initialize data members
   public Employee(int id, String name, int sal) {
       super();
       this.id = id;
       this.name = name;
       this.sal = sal;
   }
   // setters and getters
   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;
   }
   public static void main(String[] args) {
       // creating object of employee class
       Employee employee=new Employee(1,"barry",50000);
       Employee employee1=new Employee(2,"Catilin",30000);
       Employee employee2=new Employee(3,"West",43000);
       Employee employee3=new Employee(4,"Iris",35000);
       Employee employee4=new Employee(5,"joe",65000);
       // creating HashMap with Employee,Integer pair
       HashMap<Employee,Integer> hashMap=new HashMap<Employee,Integer>();
       // checking duplicates present or not
       if(hashMap.containsKey(employee)==false) {
           hashMap.put(employee,1);
       }
       if(hashMap.containsKey(employee1)==false) {
           hashMap.put(employee1,2);
       }
       if(hashMap.containsKey(employee2)==false) {
           hashMap.put(employee2,3);
       }
       if(hashMap.containsKey(employee3)==false) {
           hashMap.put(employee3,4);
       }
       if(hashMap.containsKey(employee4)==false) {
           hashMap.put(employee4,5);
       }
       // set for storing keys
       Set<Employee> keys=hashMap.keySet();
       // iterating over each key and printing its values
       for(Employee key:keys) {
           System.out.println(hashMap.get(key)+" "+"ID:"+key.id+" "+"Name:"+" "+key.name+" "+"Salary:"+" "+key.sal);
       }
   }
  
}


output:

1 ID:1 Name: barry Salary: 50000 5 ID:5 Name: joe Salary: 65000 4 ID:4 Name: Iris Salary: 35000 3 ID:3 Name: West Salary: 430

code screenshot:

import java.util.HashMap; import java.util.Set; class Employee // data members of class Employee private int id; private Stri

public static void main(String[] args) { // creating object of employee class Employee employee=new Employee(1, barry, 5000

72d.

code:

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class SeventyTwoD {
   public static void main(String[] args) {
       // creating Map for sorting based on key
       Map<Integer,String> map=new TreeMap<Integer, String>();
       map.put(4, "Caty");
       map.put(1, "barry");
       map.put(3, "Joe");
       // set for storing keys
       Set<Integer> keys=map.keySet();
       // loop for iterating over keys
       for(Integer key:keys) {
           System.out.println(key+" "+map.get(key));
       }
      
   }
}

output:

1 barry 3 Joe 4 Caty

code screenshot:

import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util. TreeMap;

Note:Check the code screenshot for indentation of code.

Add a comment
Know the answer?
Add Answer to:
Java Do 72a, 72b, 72c, 72d. Code & output required. 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 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...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

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

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Public class Person t publie alass EmployeeRecord ( private String firstName private String last ...

    public class Person t publie alass EmployeeRecord ( private String firstName private String last Nanei private Person employee private int employeeID publie EnmployeeRecord (Person e, int ID) publie Person (String EName, String 1Name) thia.employee e employeeID ID setName (EName, 1Name) : publie void setName (String Name, String 1Name) publie void setInfo (Person e, int ID) this.firstName- fName this.lastName 1Name this.employee e employeeID ID: publio String getFiritName) return firstName public Person getEmployee) t return employeei public String getLastName public int getIDO...

  • I have most of the code for this assignment but need some help getting the rest....

    I have most of the code for this assignment but need some help getting the rest. Go to bottom to see what I have. Please help me figure out the foreach loops and the put() statement. In this assignment, you will be using a new data structure, HashMaps, to repeat Assignment 8.3. Main Method (5 points) Declare and initialize an HashMap with values of type of type Employee, and keys of type integer. In a while loop, keep initializing objects...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

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