JAVA- Without changing ANY of the code that is already posted, complete this cod
ID: 3739774 • Letter: J
Question
JAVA- Without changing ANY of the code that is already posted, complete this code to satisfy the comments.
//////////////////////////////
import java.util.*;
class Person {
private String lastName;
private String firstName;
private int age;
public Person(String first, String last, int age) {
lastName = last;
firstName = first;
age = age;
}
@Override
public String toString() {
String result = "";
//!!! return "person info: firstName lastName, age"
return result;
}
public int getAge() {
return age;
}
}
public class Lab7
{
public static void main(String[] args) {
testStack();
testQueue();
}
private static void testStack() {
Stack<Person> personStack = new Stack<>();// JDK stack has no limits
//!!! add five persons with different names and ages
//!!! print contents of the stack
//!!! find a person with smallest age and prints the person
}
private static void testQueue() {
Queue<Integer> intQueue1 = new LinkedList<>();
PriorityQueue<Integer> intQueue2 = new PriorityQueue<>();
//!!! Add 10 random numbers (0 ~ 99) to queue and priority queue
//!!! Each queue should have different random numbers
//!!! Use offer for queues
//!!! Find the smallest number from all.
//!!! Use an iterator for intQueue1.
//!!! Remove all elements from both queues
//!!! For each element removed, print content of the queues
//!!! Use remove for queue
}
}
Explanation / Answer
Please find the below updated Person and Lab7 class. Also find the output of the Lab7 class.
Please check and revert in case anything needs to change.
***** Person ****
import java.util.*;
class Person {
private String lastName;
private String firstName;
private int age;
public Person(String first, String last, int age) {
lastName = last;
firstName = first;
this.age = age;
}
@Override
public String toString() {
String result = firstName + " " + lastName + ", " + age;
return result;
}
public int getAge() {
return age;
}
}
***** Lab7 ******
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
public class Lab7 {
public static void main(String[] args) {
testStack();
testQueue();
}
private static void testStack() {
Stack<Person> personStack = new Stack<>();
Person person1 = new Person("John", "Dias", 23);
personStack.push(person1);
Person person2 = new Person("Lionel", "Mesi", 27);
personStack.push(person2);
Person person3 = new Person("Sachin", "Tendulkar", 41);
personStack.push(person3);
Person person4 = new Person("Jim", "Terry", 21);
personStack.push(person4);
Person person5 = new Person("Alex", "Bertram", 38);
personStack.push(person5);
for (Person person : personStack) {
System.out.println(person);
}
Person minAgePerson = personStack.elementAt(0);
int minAge = personStack.elementAt(0).getAge();
for (int i=1;i<personStack.size();i++) {
if (personStack.elementAt(i).getAge() < minAge) {
minAge = personStack.elementAt(i).getAge();
minAgePerson = personStack.elementAt(i);
}
}
System.out.println("Person with smallest age: "+minAgePerson);
}
private static void testQueue() {
Queue<Integer> intQueue1 = new LinkedList<>();
PriorityQueue<Integer> intQueue2 = new PriorityQueue<>();
Random random = new Random();
for (int i=0;i<10;i++) {
intQueue1.offer(random.nextInt(100));
intQueue2.add(random.nextInt(100));
}
System.out.println(" intQueue1:");
System.out.println(intQueue1);
int minQueue1 = intQueue1.element();
Iterator<Integer> itr1 = intQueue1.iterator();
while(itr1.hasNext()){
int element = itr1.next();
if (element < minQueue1) {
minQueue1 = element;
}
}
System.out.println("Smallest number in intQueue1: "+minQueue1);
System.out.println(" intQueue2:");
System.out.println(intQueue2);
int minQueue2 = intQueue2.element();
Iterator<Integer> itr2 = intQueue2.iterator();
while(itr2.hasNext()){
int element = itr2.next();
if (element < minQueue2) {
minQueue2 = element;
}
}
System.out.println("Smallest number in intQueue2: "+minQueue2);
System.out.println(" Removing intQueue1:");
System.out.println(intQueue1);
Iterator<Integer> itr = intQueue1.iterator();
while(itr.hasNext()){
itr.next();
itr.remove();
System.out.println(intQueue1);
}
System.out.println(" Removing intQueue2:");
System.out.println(intQueue2);
intQueue2.remove(0);
System.out.println(intQueue2);
for (int i=0;i<10;i++) {
intQueue2.remove();
System.out.println(intQueue2);
}
}
}
***** Output *****
John Dias, 23
Lionel Mesi, 27
Sachin Tendulkar, 41
Jim Terry, 21
Alex Bertram, 38
Person with smallest age: Jim Terry, 21
intQueue1:
[51, 62, 13, 93, 63, 96, 98, 22, 6, 89]
Smallest number in intQueue1: 6
intQueue2:
[1, 4, 10, 12, 8, 40, 44, 83, 78, 20]
Smallest number in intQueue2: 1
Removing intQueue1:
[51, 62, 13, 93, 63, 96, 98, 22, 6, 89]
[62, 13, 93, 63, 96, 98, 22, 6, 89]
[13, 93, 63, 96, 98, 22, 6, 89]
[93, 63, 96, 98, 22, 6, 89]
[63, 96, 98, 22, 6, 89]
[96, 98, 22, 6, 89]
[98, 22, 6, 89]
[22, 6, 89]
[6, 89]
[89]
[]
Removing intQueue2:
[1, 4, 10, 12, 8, 40, 44, 83, 78, 20]
[1, 4, 10, 12, 8, 40, 44, 83, 78, 20]
[4, 8, 10, 12, 20, 40, 44, 83, 78]
[8, 12, 10, 78, 20, 40, 44, 83]
[10, 12, 40, 78, 20, 83, 44]
[12, 20, 40, 78, 44, 83]
[20, 44, 40, 78, 83]
[40, 44, 83, 78]
[44, 78, 83]
[78, 83]
[83]
[]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.