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: Brief Version (11th Global Edition)
- Assume Student and GradeBook classes are defined as follows: public class Student { int id; double testsMarks[]; public double getAverage(){ double sum = 0; for(double marks : testsMarks) { sum+=marks; } return sum/testsMarks.length; } } import java.util.ArrayList; public class GradeBook { String subject; ArrayList<Student> students; public Student getStudentWithMinAverage(){ } } Define the body of getStudentWithMinAverage method that shall return the student object from students ArrayList who got minimum average marks in the class. (Please write minimum code and make use of provided code, making no change in classes' structure)arrow_forwardimport java.util.Arrays; import java.util.Scanner; public class Game { private Player[] players; private Board board; private int currentPlayerIndex; private int numOfPlayers; private Scanner scanner = new Scanner(System.in); // constructor public Game(){ //create a new board; this.board = new Board(); //setup game; this.setupGame(); //start game; this.play(); } //Game Setup public void setupGame(){ System.out.println("How many players would you like to included in this game? (2-4)"); numOfPlayers = scanner.nextInt(); while (numOfPlayers < 2 || numOfPlayers > 4) { System.out.println("Invalid input. Please enter a number between 2 and 4."); numOfPlayers = scanner.nextInt(); } scanner.nextLine(); String[] playerNames = new String[numOfPlayers]; this.players = new…arrow_forwardmake 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_forward
- public class LabProgram { public static void main(String args[]) { Course cis162 = new Course(); int beforeCount; String toDrop; // Example students for testing cis162.addStudent(new Student("Henry", "Nguyen", 3.5)); cis162.addStudent(new Student("Brenda", "Stern", 2.0)); cis162.addStudent(new Student("Lynda", "Robison", 3.2)); cis162.addStudent(new Student("Sonya", "King", 3.9)); toDrop = "Stern"; beforeCount = cis162.countStudents(); cis162.dropStudent(toDrop); System.out.println("Course size: " + beforeCount + " students"); System.out.println("Course size after drop: " + cis162.countStudents() + " students"); } } import java.text.DecimalFormat; // Class representing a student public class Student { private String first; // first name private String last; // last name private double gpa; // grade point average // Student class constructor public Student(String f, String l, double g) {…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_forwardpublic class MyGenClass { private T1 name; private T2 stulD; private T3 CGPA; public void setStuData( ){ l parameters name = n; stulD = d: CGPA = g; public void printAsString() { System.out.printin( '): # print all Information as string public static void main(String args||) { Il Create an object stu by parameters String, Integer, Double II Call setStuData() with any values you like Il Call printAsString()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_forwardpublic class MyGenClass { private T1 name; private T2 stulD; private T3 CGPA; public void setStuData( ){ I/ parameters name = n; stulD = d: CGPA = g; } public void printAsString() { System.out.println( } ); I/ print all information as string public static void main(String args||) { Il Create an object stu by parameters String, Integer, Double Il Call setStuData() with any values you like /l Call printAsString() } }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_forward
- import 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_forwardimport java.io.*;import java.util.*; public class Client { private ArrayList<Student> students;private String dealWithType;public Client() {students = new ArrayList<Student> ();dealWithType = "coursework";dealWithType = "research";readStudent();readResult();}public void run() {for(Student s : students) {s.display();System.out.println();} } private void readResult() {for(Student s : students) {try {String data = getResultData(s.getId());if(data.equals("")) {throw new StudentNotFoundException(s.getId());}s.setData(data);}catch(StudentNotFoundException ex){System.out.println(ex);System.out.println();}}} private String getResultData(long id) {String toReturnLine = "";try {Scanner file = new Scanner(new File(dealWithType+".csv"));// read in header and throw awayfile.nextLine();while (file.hasNextLine()) {String line = file.nextLine();String[] data = line.split(",");long studentID = Long.parseLong(data[0]);if(studentID == id) {toReturnLine =…arrow_forwardpublic class CustomLinkedList { public static int findMax(IntNode headObj) { /* Type your code here */ } public static void main(String[] args) { IntNode headObj; IntNode currObj; IntNode lastObj; int i; int max; // Create head node headObj = new IntNode(-1); lastObj = headObj; // Add nodes to the list for (i = 0; i < 20; ++i) { currObj = new IntNode(i); lastObj.insertAfter(currObj); lastObj = currObj; } max = findMax(headObj); System.out.println(max); }}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