Aufgabe 2 fin

This commit is contained in:
jean 2020-11-13 18:59:47 +01:00
parent ff7d904cab
commit 888399464e

View File

@ -1,5 +1,6 @@
package Uebungen.Uebung5;
import java.awt.desktop.SystemSleepEvent;
import java.util.Scanner;
import java.lang.Character;
import java.util.Arrays;
@ -19,51 +20,73 @@ public class Aufgabe2 {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter desired string: ");
char[] stringAsCharArr = scanner.nextLine().toCharArray();
char[] usedCharArr = usedChars2(stringAsCharArr);
for(char character:usedCharArr) System.out.println(" \""+ character + "\""); // Debug
// System.out.println(stringAsCharArr.hashCode()); // Get the hashcode of the array
stringAsCharArr = normalize(stringAsCharArr);
char[] usedCharArr = usedChars(stringAsCharArr);
int[] charAmountArr = countChars(usedCharArr ,stringAsCharArr);
histogramm(20,usedCharArr, charAmountArr);
}
public static char[] usedChars(char[] charArray){
char[] workingArray = new char[charArray.length];
int position = 0; // current position
for(char currChar:charArray) {
currChar=Character.toLowerCase(currChar); //normalize
boolean exists=false;
for(char targetChar:workingArray) if(targetChar==currChar) exists=true;
if(!exists){
workingArray[position]=currChar;
position++;
}
}
char[] targestArray = new char[position];
for(int i=0;i<position;i++) targestArray[i] = workingArray[i];
Arrays.sort(targestArray);
return targestArray;
public static char[] normalize(char[] workerArray){
for(int i=0; i<workerArray.length;i++)
workerArray[i]=Character.toLowerCase(workerArray[i]);
return workerArray;
}
// Improved function: less code, less demanding
public static char[] usedChars2(char[] workingArray){
public static void histogramm(int width, char[] availableChars, int[] charAmount){
int max = maximum(charAmount);
for(int i=0;i<availableChars.length;i++) {
System.out.print("\""+availableChars[i]+"\": ");
if(max>width)
for(int j=0;j<scale(charAmount[i],max,width);j++) System.out.print("#");
else
for(int j=0;j<charAmount[i];j++) System.out.print("#");
System.out.println("x"+charAmount[i]);
}
}
public static int scale(int value, int oldMax, int newMax){
double calc = (double) value/oldMax*newMax;
return (int) calc;
}
public static int maximum(int[] inputArray){
int largestNumber = inputArray[0];
for(int i=1;i<inputArray.length;i++)
if(inputArray[i]>largestNumber)
largestNumber = inputArray[i];
return largestNumber;
}
public static int[] countChars(char[] availableChars, char[] scannedArray){
int[] occurrences = new int[availableChars.length];
// Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0.
for(char currChar:scannedArray)
for(int i=0;i<availableChars.length;i++)
if(availableChars[i]==currChar)
occurrences[i]++;
return occurrences;
}
public static char[] usedChars(char[] inputArray){
char[] workerArray = inputArray.clone(); // System.out.println(inputArray.hashCode()); // Get the hashcode of the array
// workingArray gets an new hashcode after modifying, so its okay using it
// now we remove all characters that occur multiple times
// we also count how many chars we got
int counter=0;
for(int i=0; i<workingArray.length;i++){
if((int)workingArray[i]!=0) {
int counter=0; // we also count how many different chars we found
for(int i=0; i<workerArray.length;i++){
if((int)workerArray[i]!=0) { //there is no need to run if the char is already the null character
counter++; // found a new one -> increment counter
// now remove all similar chars from that point till the end of the array
for (int j = i + 1; j < workingArray.length; j++) {
if (workingArray[i] == workingArray[j]) workingArray[j] = (char) 0; // set the char to "Null character"
}
for (int j = i + 1; j < workerArray.length; j++) // loop through starting from current pos.
if (workerArray[i] == workerArray[j]) // check if char already exists
workerArray[j] = (char) 0; // set the char to "Null character"
}
}
char[] targetArray = new char[counter]; // create new
int targetPosition = 0; // initiate counter for the Target Array
for(char currChar:workingArray){ // seek through all chars
for(char currChar:workerArray) // seek through all chars
if((int)currChar!=0){ // if its not a null character it is a new char
targetArray[targetPosition] = currChar; // add the current char
targetPosition++; // we added a new char, so we need to increment for the next one
}
}
return targetArray;
}
}