PLEASE ANSWER THE VOCABULARY AND ALL QUESTIONS FROM 1 TO 5 ! Lesson Objectives:
ID: 3850498 • Letter: P
Question
PLEASE ANSWER THE VOCABULARY AND ALL QUESTIONS FROM 1 TO 5 !
Lesson Objectives:
• Create a collection without using generics
• Create a collection using generics
• Implement an ArrayList
• Implement a Set
Vocabulary:
Identify the vocabulary word for each definition below.
Try It/Solve It:
1. What is the difference between a set and a list?
2. You decide you want to roll 2 dice and see what the frequency is of each possible number combination. Would you
use a Set collection to do this? State your reason(s).
3. Using a collection create a variable that will store a list of countries (Strings). Your collection should not store
duplicates, and order is not important. Test your code by adding 6 countries, one of which is a duplicate.
4. Would the following Collection.sort() statements both work? Explain your answer.
HashSet countriesSet = new HashSet();
Collections.sort(countriesSet);
ArrayList countriesList = new ArrayList();
Collections.sort(countriesList);
www.oracle.com/academy
2
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
5. Below is a user implementation of a Stack using arrays.
• push adds an item to the Stack
• pop removes an item from the stack
• isEmpty return a Boolean value of true if the Stack is empty
Convert this to a generic implementation using an ArrayList.
public class ArrayStack {
private int maxsize;
private int top;
private int[] items;
public ArrayStack(int maxsize) {
if (maxsize <= 0)
throw new ArrayStackException(
"Stack size must be positive");
items = new int[maxsize];
this.maxsize = maxsize;
top = 0;
}
public void push(int item) {
if (top == items.length)
throw new ArrayStackException("Overflow Error");
items[top] = item;
top++;
}
public int pop() {
if (isEmpty())
throw new ArrayStackException("Underflow Error");
return items[--top];
}
public boolean isEmpty() {
return (top == 0);
}
public static class ArrayStackException extends RuntimeException {
public ArrayStackException(String message) {
super(message);
}
}
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(3);
stack.push(1);
stack.push(2);
stack.push(3);
//stack.push(4); //overflow error
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
Explanation / Answer
------------------------------ vocabulary section ----------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
1) The difference between set and list is , set cannot store duplictes whereas list can store duplicates.
2) No I won't choose collection, since it wont store duplicate values. So it two dices get the same value, then it store only one value. So it is hard to get frequency of numbers appear on dice rolling.
3)
import java.util.*;
public class SetCountriesDemo {
public static void main(String args[]) {
String countries_names[] = {"America","Australia","England","Newzealand","India","India"};
Set<String> countries = new HashSet<String>();
try {
for(int i = 0; i < 6; i++) {
countries.add(countries_names[i]);
}
System.out.println(countries);
}
catch(Exception e) {}
}
}
Sample output
------------------------------------------------------------------------------------------------------------------------------------------------------------
4)
For HashSet, collections.sort() ,method cannot use. It will give error.
For ArrayList,col/ections.sort() ,method can use. This will execute properly.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
5)
Generic based using ArrayLists was added .. added sample output to..
import java.util.*;
public class ArrayStack<E> {
private ArrayList<E> items = new ArrayList<E>();
public ArrayStack() {
}
public void push(E item) {
items.add(item);
}
public E pop() {
if(!isEmpty()) {
E val = items.get(items.size()-1);
items.remove(items.size()-1);
return val;
}
return null;
}
public boolean isEmpty() {
if(items.size() > 0) {
return false;
}
return true;
}
public static void main(String[] args) {
ArrayStack stack = new ArrayStack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4); //no overflow error..this time
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
Sample output
4
3
2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.