Last active
February 3, 2018 23:29
-
-
Save iamaamir/5c65826c8e98d2d2314955ff104a2719 to your computer and use it in GitHub Desktop.
Please write a program to use vector and list to store 10 integers in order (from smallest to largest). You need to find proper location using vector or list functions for each number added. Please do NOT use any sorting function. a. Generate 1 random integer between -100 and 100 and display it on screen b. Insert the number generated in step b …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.Vector; | |
import java.util.stream.IntStream; | |
/*Please write a program to use vector and list to store 10 integers in order | |
(from smallest to largest). | |
You need to find proper location using vector or list functions for each number added. | |
Please do NOT use any sorting function. | |
a. Generate 1 random integer between -100 and 100 and display it on screen | |
b. Insert the number generated in step b into Vector and List | |
c. Display the contents of Vector and List after adding a number | |
d. Use loop structure to repeat step a-c 10 times. | |
For example, the output screen may display as following: | |
Number generated: 23 | |
Vector: 23 | |
List: 23 | |
Number generated: -15 | |
Vector: -15, 23 | |
List: -15, 23 | |
Number generated: 40 | |
/*//**/ | |
public class VetorsAndList { | |
private int num; | |
private final List<Integer> list; | |
private final Vector<Integer> vector; | |
private final Random random; | |
private final int LIMIT = 10; | |
public VetorsAndList() { | |
this.num = 0; | |
this.vector = new Vector(LIMIT); | |
this.list = new ArrayList(LIMIT); | |
this.random = new Random(); | |
} | |
public void Solution() { | |
int i = 0; | |
while (++i <= LIMIT) { | |
// Generate 1 random integer between -100 and 100 and display it on screen | |
num = random.nextInt(100 + 1 - (-100)) + (-100); | |
spit("Number generated: " + num); | |
// b. Insert the number generated in step b into Vector and List | |
vector.add(num); | |
list.add(num); | |
for (int j = 0; j < list.size(); j++) { | |
/*Note: since both list will have same number | |
* and same size we could check for one and same apply on other | |
*/ | |
if (j < list.size() - 1) { | |
if (list.get(j) > list.get(j + 1)) { | |
int l = list.get(j); | |
int v = vector.get(j); | |
list.remove(j); | |
vector.remove(j); | |
list.add(j, list.get(j)); | |
vector.add(j, vector.get(j)); | |
list.remove(j + 1); | |
vector.remove(j + 1); | |
list.add(l); | |
vector.add(v); | |
j = -1; | |
} | |
} | |
} | |
// Display the contents of Vector and List after adding a number | |
spit("Vector: " + strip(vector)); | |
spit("List : " + strip(list)); | |
spit(""); | |
} | |
} | |
/* | |
This is just an example do not use it if you are in learning mode :P | |
*/ | |
public void shorterSolution() { | |
IntStream.range(0, LIMIT).forEach((i) -> { | |
// Generate 1 random integer between -100 and 100 and display it on screen | |
num = random.nextInt(100 + 1 - (-100)) + (-100); | |
spit("Number generated: " + num); | |
vector.add(num); | |
list.add(num); | |
vector.sort(Integer::compareTo); | |
list.sort(Integer::compareTo); | |
spit("Vector: " + strip(vector)); | |
spit("List : " + strip(list)); | |
spit(""); | |
}); | |
} | |
private String strip(Object obj) { | |
return obj.toString().replaceAll("[\\[\\]]", ""); | |
} | |
private void spit(String str) { | |
System.out.println(str); | |
} | |
//let the party begin | |
public static void main(String[] args) { | |
new VetorsAndList().Solution(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment