Concept explainers
Program:
//import the util package
import java.util.*;
//class Definition
public class Test {
// main method
public static void main(String[] args) {
//declare a LinkedHashMap
Map<Integer, String> map = new LinkedHashMap<>();
//insert keys and values into the map
map.put("123", "John Smith");
map.put("111", "George Smith");
map.put("123", "Steve Yao");
map.put("222", "Steve Yao");
//print elements present in the map
System.out.println("(1) " + map);
/*print elements in a new Treemap which implements map */
System.out.println("(2) " + new TreeMap<, String>(map));
//check if key equals 123 then print the value
map.forEach((k, v) -> {
if (k.equals("123"))
System.out.println(v);});
}
}
Want to see the full answer?
Check out a sample textbook solutionChapter 21 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- make this code workable import java.util.ArrayList; public class Album { private String title, artist; private int year; private ArrayList<Songs> songs; public Album() { title = ""; artist = ""; year = 0; songs = new ArrayList<>(); } public Album(String title, String artist, int year, ArrayList<Song> songs) { super(); this.title = title; this.artist = artist; this.year = year; this.songs = songs; } /** * @return the title */ public String getTitle() { return title; } /** * @param title the title to set */ public void setTitle(String title) { this.title = title; } /** * @return the artist */ public String getArtist() { return artist; } /** * @param artist the artist to set */ public void setArtist(String artist) { this.artist = artist; } /** * @return the year */ public int getYear() { return year; } /** * @param year the year to set */ public void setYear(int year) { this.year = year; } /** * @return the songs */ public ArrayList<Song>…arrow_forwardTrace through the following program and show the output. Show your work for partial credit. public class Employee { private static int empID = 1111l; private String name , position; double salary; public Employee(String name) { empID ++; this.name 3 пате; } public Employee(Employee obj) { empID = obj.empĪD; пате %3D оbj.naте; position = obj.position; %3D public void empPosition(String empPosition) {position = empPosition;} public void empSalary(double empSalary) { salary = empSalary;} public String toString() { return name + " "+empID + " "+ position +" $"+salary; public void setName(String empName){ name = empName;} public static void main(String args[]) { Employee empOne = new Employee("James Smith"), empTwo; %3D empOne.empPosition("Senior Software Engineer"); етрOпе.етpSalary(1000); System.out.println(empOne); еmpTwo empTwo.empPosition("CEO"); System.out.println(empOne); System.out.println(empTwo); %3D етpОпе empOne ;arrow_forwardclass WaterSort { Character top = null; // create constants for colors static Character red= new Character('r'); static Character blue = new Character('b'); static Character yellow= new Character('g'); // Bottles declaration public static void showAll( StackAsMyArrayList bottles[]) { for (int i = 0; i<=4; i++) { System.out.println("Bottle "+ i+ ": " + bottles[i]); } } public static void main(String args[]) { //part 1 //create the bottle StackAsMyArrayList bottleONE = new StackAsMyArrayList<Character>(); System.out.println("\nbottleONE:"+ bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());…arrow_forward
- Java: ShowStudent.java: class ShowStudent { public static void main (String args[]) { Student pupil = new Student(); pupil.setIdNumber(234); pupil.setPoints(47); pupil.setHours(15); pupil.showIdNumber(); pupil.showPoints(); pupil.showHours(); System.out.println("The grade point average is " + pupil.getGradePoint()); } } ShowStudent2.java: class ShowStudent2 { public static void main (String args[]) { Student pupil = new Student(); pupil.showIdNumber(); pupil.showPoints(); pupil.showHours(); System.out.println("The grade point average is " + pupil.getGradePoint()); } } Student.java: class Student { // the private data members private int idNumber; private int hours; private int points; // Constructor added in part c Student() { } // end of constructor added in part c // the public get and set methods public void…arrow_forward25. Show the output of the following code: public class Test { public static void main(String[] args) { Map map = new LinkedHashMap(); map.put("123", "John Smith"); map.put("111", "George Smith"); map.put("123", "Steve Yao"); map.put("222", "Steve Yao"); System.out.printin("(1) " + map): System.out.printin("(2) "+ new TreeMap(map)): } }arrow_forwardimport java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // TODO: Read and set base and height for triangle1 (use setBase() and setHeight()) // TODO: Read and set base and height for triangle2 (use setBase() and setHeight()) System.out.println("Triangle with smaller area:"); // TODO: Determine smaller triangle (use getArea()) // and output smaller triangle's info (use printInfo()) }} public class Triangle { private double base; private double height; public void setBase(double userBase){ base = userBase; } public void setHeight(double userHeight) { height = userHeight; } public double getArea() { double area = 0.5 * base * height; return area; } public void printInfo() { System.out.printf("Base:…arrow_forward
- import java.util.Objects;class CovidVariant { String Name; String Unique_code; CovidVariant left, right; public CovidVariant(String name, String code) { this.Name = name; this.Unique_code = code; left = right = null; }}class VariantCollection { private boolean compare_code(String code, String code_2) { int year = Integer.parseInt(code.substring(0, 2)); int month = Integer.parseInt(code.substring(2, 4)); int date = Integer.parseInt(code.substring(4, 6)); int year_2 = Integer.parseInt(code_2.substring(0, 2)); int month_2 = Integer.parseInt(code_2.substring(2, 4)); int date_2 = Integer.parseInt(code_2.substring(4, 6)); if (year == year_2) { if (month == month_2) { if (date > date_2) { return true; } else { return false; } } else if (month > month_2) { return true; }…arrow_forwardthe code: class HighArray { private long[] a; private int nElems; public HighArray(int max) { a = new long[max]; nElems = 0; } public void insert(long value) { a[nElems] = value; nElems++; } public boolean find(long searchKey) { int j; for (j = 0; j < nElems; j++) if (a[j] == searchKey) break; if (j == nElems) return false; else return true; } public long getMax() { if (nElems == 0) return 01; else { long max = a[0]; for (int i = 1; i < nElems; i++) { if (a[i] > max) max = a[i]; } return max; } } public boolean delete(long value) { int j; for (j = 0; j < nElems; j++) if (value == a[j]) break; if (j == nElems) return false; else { for (int k = j; k < nElems; k++) a[k] = a[k + 1]; nElems--; return true; } } public void display() { for (int j = 0; j < nElems; j++) System.out.print(a[j] + " "); System.out.println(""); }} public class HighArrayApp { public static void main(String[] args) { int maxSize =…arrow_forwardpackage priorityqueue;import java.util.Scanner; public class AdmissionsDesk{ private WaitingRoom hosp = new WaitingRoom(); public static void main(String[] args) { AdmissionsDesk desk = new AdmissionsDesk(); } public AdmissionsDesk() { queuePatients(); fillBeds(); admissionDesk(); } public void queuePatients() { Scanner reader = null; try { reader = new Scanner(new File("patients.dat")); } catch (IOException e) { System.out.println(e); System.exit(0); } while(reader.hasNextLine()) { String str = reader.nextLine(); Scanner parser = new Scanner(str); parser.useDelimiter("/"); // hosp.queuePatient(new Patient(parser.next(), parser.nextInt())); } } public void fillBeds() { // for (int i = 0; i < 10; i++) // hosp.admitPatient(); } public…arrow_forward
- Public class DLL{ public DLLNode first; public DLLNode last; public DLL() {this.first=Null; this.last=Null;} Public void Display() {if (first==Null) {s.o.p(“ the list is empty”);} else {for (DLLNode curr=first; curr!=Null; curr=curr.succ) {System.out.println(“the element is”+ curr.element); }} Public void AddBegining(DLLNode Node) {Node.succ=first; First.prev=Node First=Node; Node.prev=Null; if(first ==null && last==null) Last=node; } Public void Append(DLLNode Node) {Node.succ=Null; if(first==Null) {first=Node; Node.prev=Null} Public void insetAfter(int obj, DLLNode Node) {DLLNode curr=first; while((curr!=Null)&&( curr.element!=obj)) { curr=curr.succ; } If(curr.element==obj) {Node.succ=curr.succ; Curr.succ.prev=Node; Curr.succ=Node; Node.prev=curr;} Else System.out.println(“the element doesn’t exist”); Public void delete(DLLNode del) { if(first==Null) {System.out.println(“the list is empty”); } Else if(del==first)…arrow_forwardvoid getInput(){for(int i =0; i < studentName.length; i++){System.out.print("Student name: ");studentName[i] = keyboard.nextLine();System.out.print("Studnet ID: ");midTerm1[i] = keyboard.nextInt();}keyboard.close();} Can't put full student name becuase nextLine();arrow_forwardimport java.util.ArrayList; public class Bus { privateStringname; privateintsize; privateintbasePrice; privateArrayList<Trip>approvedTrips; privateintlevel;// 1,2,3 for low,medium, high repectively; privateintid; privatestaticintnextId=0; privateMinistrymny; protectedStringtripTypes; privateintgetNextId(){ returnnextId++; } publicBus(){ approvedTrips=newArrayList<Trip>(); } publicBus(Stringname,intsize,intbasePrice,intlev,Ministrymny){ approvedTrips=newArrayList<Trip>(); this.name=name; this.size=size; this.basePrice=basePrice; this.level=lev; this.id=getNextId(); this.mny=mny; tripTypes="BASICTRANSPORT"; } publicbooleanavailable(Datedate){ booleanretval=true; for(Tript:approvedTrips) if(t.getDate().getDay()==date.getDay()) retval=false; returnretval; } publicintgetBasePrice(){ returnbasePrice; } publicintgetId(){ returnid; } publicStringgetName(){ returnname; } publicdoublegetSize(){ returnsize; } publicStringtoString(){ returnname; }…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education