-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssEx1.java
More file actions
132 lines (111 loc) · 3.52 KB
/
AssEx1.java
File metadata and controls
132 lines (111 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class AssEx1 {
/** read strings from file
* and add them to an array. Assume that in the file there is one string
* per line.
* @param filename
* return array
*/
public static String[] readFromFile(String fileName) {
List<String> allLines=new ArrayList<String>();
String[] content;
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
allLines.add(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
content=new String[allLines.size()];
for (int i=0;i<content.length;i++) {
content[i]=allLines.get(i);
}
return content;
}
/**
* Add all of the elements of an array of Strings
* to a set of strings - note repeats will disappear
* your set should be instantiated as a TreeSet (see lecture 1)
*/
public static Set<String> arrayToSet(String[] myArray){
Set <String> content=new TreeSet<String>();
for (int i=0;i<myArray.length;i++) {
content.add(myArray[i]);
}
return content; // replace this with your implementation
}
/** randomly re-order an array
*
* @param origArray
*/
public static void jumbleArray(String[] origArray){
Random r=new Random();
String temp="";
for (int i=0;i<origArray.length;i++) {
int a=r.nextInt(origArray.length);
temp=origArray[i];
origArray[i]=origArray[a];
origArray[a]=temp;
}
return; //replace this with your implementation
}
/** create a string consisting all of the elements in an array,
* one element per row.
* Use a for--each loop to return them in the order in which they
* are stored
*/
public static String arrayToString(String[] stringArray){
StringBuilder content=new StringBuilder();
for (int i=0;i<stringArray.length;i++) {
content.append(stringArray[i]+" ");
}
content.append("\n");
return content.toString(); //replace this with your implementation
}
/** create a string consisting of all of the elements in a set,
* one element per row.
* Use a for--each loop to return them in the order in which they
* are stored
*/
public static String setToString(Set<String> stringSet){
StringBuilder content=new StringBuilder();
for (String str : stringSet) {
content.append(str+" ");
}
content.append("\n");
return content.toString(); //replace this with your implementation
}
/**
* main method - do not edit this
* @param args
*/
public static void main(String[] args){
String fileName = args[0];
String[] originalArray = readFromFile(fileName);
System.out.println("the array has length " + originalArray.length + "\n");
String[] newArray1 = originalArray.clone();
String[] newArray2 = originalArray.clone();
jumbleArray(newArray1);
jumbleArray(newArray2);
System.out.print("The original array is:\n");
System.out.print(arrayToString(originalArray) + "\n");
System.out.print("The frst jumbled array is:\n");
System.out.print(arrayToString(newArray1) + "\n");
System.out.print("The corresponding set is:\n");
System.out.print(setToString(arrayToSet(newArray1))+"\n");
System.out.print("The second jumbled array is:\n");
System.out.print(arrayToString(newArray2) + "\n");
System.out.print("The corresponding set is:\n");
System.out.print(setToString(arrayToSet(newArray2)) + "\n");
}
}