Skip to content

Commit 89ac27d

Browse files
authored
Add files via upload
1 parent a8a593b commit 89ac27d

5 files changed

+257
-0
lines changed

Password.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Proj1;
2+
3+
public class Password {
4+
5+
public static char[] generatePassword() {
6+
char[] symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); //initializing available symbols
7+
int length = (int) (Math.random()*5) + 8;
8+
char[] password = new char[length]; //Creating char array for symbols in password
9+
10+
11+
for (int i = 0; i < length; i++) {
12+
int rnd = (int) (Math.random()*symbols.length); //Randomizing symbols from available symbols
13+
password[i] = symbols[rnd];
14+
}
15+
return password;
16+
17+
}
18+
19+
public static boolean passwordCheck(String password) {
20+
if ((password.length() >= 8) &&
21+
(password.length() <= 12)&& // Checking whether the password matches
22+
(password.matches(".*[a-z]+.*")) && // the given requirments or not
23+
(password.matches(".*[0-9]+.*")) &&
24+
(password.matches(".*[A-Z]+.*")))
25+
return true;
26+
else
27+
return false;
28+
}
29+
public static void main(String[] cmdln) {
30+
int amount = 0;
31+
int passwordsamounts = 1000;
32+
for(int i = 0; i < passwordsamounts; i++) { //generating n passwords aswell as checking them
33+
System.out.println(Password.generatePassword());
34+
char[] passarray = generatePassword();
35+
String password = new String(passarray);
36+
37+
if(passwordCheck(password)==true) {amount++;}
38+
}
39+
System.out.println(amount + " out of "+ passwordsamounts + " passwords met the requirements");
40+
}
41+
}
42+
43+

Uppgift4.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Proj1;
2+
import java.util.Arrays;
3+
4+
public class Uppgift4 {
5+
6+
public static int[] addAtIndex(int[] a, int x, int index) {
7+
if(a[index] != 0) { // Check's if the desired index is "free"
8+
for(int i = a.length-1; i > index ; i--) { // If it isnt; starting backwards and moving each elemnt
9+
// To the right
10+
a[i] = a[i-1];
11+
12+
}
13+
a[index] = x; // Set's the desired index to the desired number
14+
}
15+
else {
16+
a[index] = x;
17+
}
18+
return a;
19+
}
20+
21+
public static void main(String[] cmdln) {
22+
int[] arr = {0,1,0,0,0,2,0,1,4,0};
23+
arr = addAtIndex(arr, 9, 5);
24+
System.out.println(Arrays.toString(arr));
25+
}
26+
}

Uppgift5.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Proj1;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
6+
public class Uppgift5 {
7+
8+
public static void main(String[] cmdln) {
9+
int[] symbols = new int[127]; // Initializing array with 127 elements (for each ASCII- symbol)
10+
int counter = 0;
11+
Arrays.fill(symbols, 0); // Fills the array with zeros
12+
13+
try {
14+
File filen = new File(cmdln[0]); // Importing file
15+
FileInputStream fil = new FileInputStream(filen);
16+
while (fil.available() > 0) { // check's so that the file has any symbols left
17+
char chartemp = (char) fil.read(); // reading one byte and typecasting it to a char
18+
int inttemp = (int) chartemp; // typecasting the char to an int
19+
symbols[inttemp] = symbols[inttemp] + 1; // incrementing the "char int" by 1
20+
counter++;
21+
}
22+
System.out.println(Arrays.toString(symbols));
23+
fil.close();
24+
25+
for (int i = 0; i < symbols.length; i++) {
26+
double percentage;
27+
int amount;
28+
char character;
29+
30+
if (symbols[i] != 0) {
31+
character = (char) i;
32+
amount = symbols[i];
33+
percentage = symbols[i] / (double) counter;
34+
System.out.println(character + " occured " + amount + " times, frequency: " + percentage + "%");
35+
}
36+
37+
}
38+
39+
} catch (Exception e) {
40+
System.out.println("Could not find file!");
41+
}
42+
43+
}
44+
45+
}

Uppgift6.java

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package Proj1;
2+
3+
import java.util.Arrays;
4+
5+
public class Uppgift6 {
6+
7+
public static int[] mergeArrays(int[] a, int[] b) {
8+
9+
try {
10+
int[] arr = new int[a.length + b.length]; // Initializing new array with the length of a+b
11+
int i = 0, j = 0, k = 0;
12+
while (i < a.length && j < b.length) { // Comparing elements with eachother and depending
13+
if (a[i] < b[j]) { // on which element that is lower than the other
14+
arr[k] = a[i]; // we put that element in the new array
15+
i++;
16+
k++;
17+
} else {
18+
arr[k] = b[j];
19+
j++;
20+
k++;
21+
}
22+
23+
}
24+
25+
if (i < a.length) { // These arraycopy's take what's rest of an array and
26+
// Fills the rest of the new array the rest
27+
System.arraycopy(a, i, arr, k, (a.length - i));
28+
}
29+
30+
if (j < b.length) {
31+
32+
System.arraycopy(b, j, arr, k, (b.length - j));
33+
34+
}
35+
return arr;
36+
37+
} catch (Exception e) {
38+
System.out.println("Error occured");
39+
40+
}
41+
42+
return null;
43+
}
44+
45+
public static boolean isSorted(int[] a) { // Checking so that the array is sorted
46+
try {
47+
for (int i = a.length - 1; i > 0; i--) {
48+
if (a[i] < a[i - 1]) {
49+
return false;
50+
}
51+
}
52+
} catch (Exception e) {
53+
54+
}
55+
return true;
56+
}
57+
58+
public static void main(String[] cmdln) {
59+
for (int i = 0; i < 20; i++) {
60+
int[] a = new int[(int) (Math.random() * 26) + 1]; //Creating arrays with 1-25 elements
61+
int[] b = new int[(int) (Math.random() * 26) + 1];
62+
63+
for (int j = 0; j < a.length; j++) { // Filling the new arrays with values between 1-100
64+
a[j] = (int) (Math.random() * 100) + 1;
65+
}
66+
for (int j = 0; j < b.length; j++) {
67+
b[j] = (int) (Math.random() * 100) + 1;
68+
}
69+
70+
Arrays.sort(a); // Sorting new arrays
71+
Arrays.sort(b);
72+
73+
int[] c = mergeArrays(a, b);
74+
System.out.println(Arrays.toString(c));
75+
System.out.println(isSorted(c));
76+
77+
}
78+
79+
}
80+
81+
}

Uppgift8.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Proj1;
2+
3+
import java.util.Scanner;
4+
import java.io.*;
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
8+
public class Uppgift8 {
9+
public static boolean addToList(ArrayList<String> list, String name) {
10+
int index = list.size();
11+
for (int i = 0; i < list.size() - 1; i++) {
12+
String current = list.get(i).replaceAll("\\s+", ""); //Removing all whitespace in each name
13+
String next = list.get(i + 1).replaceAll("\\s+", "");
14+
String trimname = name.replaceAll("\\s+", "");
15+
16+
if (current.equals(trimname)) { // If the name exists in the list; return false
17+
return false;
18+
19+
} else if (trimname.compareTo(current) > 0) { // Checking which index the name should be placed on
20+
if (trimname.compareTo(next) < 0) {
21+
index = i + 1;
22+
}
23+
24+
}
25+
26+
}
27+
list.add(index,name);
28+
System.out.println(list);
29+
return true;
30+
}
31+
32+
33+
34+
public static void main(String[] cmdln) {
35+
ArrayList<String> klasslista = new ArrayList<String>();
36+
String[] names = { "Amanda Andersson", "Donald Trumpeten", "Janne Bildoktor", "Oscar Johansson",
37+
"Gustav Andersson", "Zorro Zorrosson", "Zzueen Queeson" };
38+
39+
try {
40+
File fil = new File(cmdln[0]); // Creating an arraylist with the different names
41+
Scanner scan = new Scanner(fil);
42+
while (scan.hasNext()) {
43+
String s = scan.nextLine();
44+
klasslista.add(s);
45+
46+
}
47+
Collections.sort(klasslista); // Sorting the list alphabetically
48+
49+
for (int i = 0; i < names.length; i++) {
50+
51+
addToList(klasslista, names[i]);
52+
53+
}
54+
55+
scan.close();
56+
} catch (Exception e) {
57+
System.out.println("Cant Find File");
58+
}
59+
60+
}
61+
62+
}

0 commit comments

Comments
 (0)