Concept explainers
Program:
//import util package
import java.util.*;
//class definition
public class Test {
// main method
public static void main(String[] args) {
//Create a new Set set1 ofLinkedHash Set type
LinkedHashSet<String> set1 = new LinkedHashSet<>();
/* add NewYork element into the set using the add() method */
set1.add("New York");
/*Create a new set set2 which equals set1 */
LinkedHashSet<String> set2 = set1;
//clone set1 and assign it a new set set2
LinkedHashSet<String> set3 =
(LinkedHashSet<String>)(set1.clone());
//add Atlanta to the set1
set1.add("Atlanta");
//print each set
System.out.println("set1 is " + set1);
System.out.println("set2 is " + set2);
System.out.println("set3 is " + set3);
/* Print each element present in set 1 using the ForEach() method */
set1.forEach(e -> System.out.print(e + " "));
}
}
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)
- Trace 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_forwardusing System; class Program { publicstaticvoid Main(string[] args) { int number = 1,i; while (number <= 88) { Console.WriteLine(number); number = number + 2; { Random r = new Random(); int[] randNo=newint[3]; for(i=0;i<3;i++) { randNo[i]= r.Next(1,88) Console.WriteLine("Random Number between 1 and 88 is "+randNo); } } } } } how can this program be fixed to choose three random numbers in C# between 1-88 instead of just one random number?arrow_forwardpublic class Test { } public static void main(String[] args) { String str = "Salom"; System.out.println(str.indexOf('t)); }arrow_forward
- Java source code reading- Please read the java source code on the left and answer the questions on the rightarrow_forwardclass overload { int x; double y; void add(int a , int b) { x = a + b; } void add(double c , double d) { y = c + d; } overload() { this.x 0; %3D this.y = 0; } %3D } class Overload_methods { public static void main(String args[]) { overload obj int a = 2; double b - 3.2; obj.add(a, a); obj.add(b, b); System.out.println(obj.x + } = new overload(); + obj.y); } #3Run the codearrow_forwardCorrect my codes in java //Stringimport java.util.Scanner;class salary{double multiply(double month,int day){double salary=month*day;return salary;}String schoole (String teacher1,String teacher2){String manger=teacher1+teacher2;return manger;}}public class Assignment2 {public static void main(String args[]){salary obj=new salary();Scanner sc=new Scanner (System.in);System.out.println();double month=sc.nextDouble();int day=sc.nextInt();String teacher1=sc.next();String teacher2=sc.next();double salary=obj.salary(month,day);String schoole=obj.schoole(teacher1,teacher2);String manger;System.out.println("manger"+manger);System.out.println("salary"+salary);}}arrow_forward
- // SuperMarket.java - This program creates a report that lists weekly hours worked // by employees of a supermarket. The report lists total hours for // each day of one week. // Input: Interactive // Output: Report. import java.util.Scanner; public class SuperMarket { public static void main(String args[]) { // Declare variables. final String HEAD1 = "WEEKLY HOURS WORKED"; final String DAY_FOOTER = " Day Total "; // Leading spaces are intentional. final String SENTINEL = "done"; // Named constant for sentinel value. double hoursWorked = 0; // Current record hours. String hoursWorkedString = ""; // String version of hours String dayOfWeek; // Current record day of week. double hoursTotal = 0; // Hours total for a day. String prevDay = ""; // Previous day of week. boolean done = false; // loop control Scanner input = new…arrow_forwardusing System; class Program { publicstaticvoid Main(string[] args) { int number = 1; while (number <= 88) { Console.WriteLine(number); number = number + 2; } int[] randNo = newint[88]; Random r = new Random(); int i=0; while (number <= 88) { randNo[i] = number; number+=1; i+=1; } for (i = 0; i < 3; i++) { Console.WriteLine("Random Numbers between 1 and 88 are " + randNo[r.Next(1, 88)]); } } } this code counts from 1-88 in odds and then selects three different random numbers. it keeps choosing 0 as a random number everytime. how can that be fixed?arrow_forwardclass operators { public static void main(String args[]) { int x = 8; System.out.println(++x * 3 + " " + x); } } Give result for the code Java Try to do ASAP ?arrow_forward
- Contoh Dalam pemrograman java: class Introduction { public static void main(String args[ ]) { byte x=50.5; System.out.println("Nilai dari variabel x= "+x)3; } ERROR MENGAPA???arrow_forwardInstructions Insurance.java >- Terminal + 1 import java.util.Scanner; sandbox $ 0 2 class Insurance { The Harrison Group Life Insurance company computes public static void main (String args[]) { // Write your code here annual policy premiums based on the age the customer 4 turns in the current calendar year. The premium is } computed by taking the decade of the customer's age, adding 15 to it, and multiplying by 20. 7 public static int calculatePremium(int curr, int birth) { 8 // Write your code here } For example, a 34-year-old would pay $360, which is calculated by adding the decades (3) to 15, and then 10 } 11 multiplying by 20. Write an application that prompts a user for the current year then a birth year. Pass both to a method that calculates and returns the premium amount, and then display the returned amount. An example of the program is shown below: Enter the current year >> 2020 Enter the birth year >> 2001 The premium is $320 Grading Write your Java code in the area on the…arrow_forwardJAVA Language Caesar Shift Question: Modify the Caesar class so that it will allow various sized shifts to be used, instead of just a shift of size 3. (Hint: Use an instance variable in the Caesar class to represent the shift, add a constructor to set it, and change the encode method to use it.) import java.util.*; public class TestCipher { public static void main(String[] args) { int shift = 7; Caesar caesar = new Caesar(); String text = "hello world"; String encryptTxt = caesar.encrypt(text); System.out.println(text + " encrypted with shift " + shift + " is " + encryptTxt); } } abstract class Cipher { public String encrypt(String s) { StringBuffer result = new StringBuffer(""); // Use a StringBuffer StringTokenizer words = new StringTokenizer(s); // Break s into its words while (words.hasMoreTokens()) { // For each word in s…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