forked from jean/GDP-Uebungen
		
	Aufgabe 2 fin
This commit is contained in:
		
							parent
							
								
									ff7d904cab
								
							
						
					
					
						commit
						888399464e
					
				| @ -1,5 +1,6 @@ | |||||||
| package Uebungen.Uebung5; | package Uebungen.Uebung5; | ||||||
| 
 | 
 | ||||||
|  | import java.awt.desktop.SystemSleepEvent; | ||||||
| import java.util.Scanner; | import java.util.Scanner; | ||||||
| import java.lang.Character; | import java.lang.Character; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| @ -19,51 +20,73 @@ public class Aufgabe2 { | |||||||
|         Scanner scanner = new Scanner(System.in); |         Scanner scanner = new Scanner(System.in); | ||||||
|         System.out.print("Enter desired string: "); |         System.out.print("Enter desired string: "); | ||||||
|         char[] stringAsCharArr = scanner.nextLine().toCharArray(); |         char[] stringAsCharArr = scanner.nextLine().toCharArray(); | ||||||
|         char[] usedCharArr = usedChars2(stringAsCharArr); |         stringAsCharArr = normalize(stringAsCharArr); | ||||||
|         for(char character:usedCharArr) System.out.println(" \""+ character + "\""); // Debug |         char[] usedCharArr = usedChars(stringAsCharArr); | ||||||
|         // System.out.println(stringAsCharArr.hashCode()); // Get the hashcode of the array |         int[]  charAmountArr = countChars(usedCharArr ,stringAsCharArr); | ||||||
|  |         histogramm(20,usedCharArr, charAmountArr); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static char[] usedChars(char[] charArray){ |     public static char[] normalize(char[] workerArray){ | ||||||
|         char[] workingArray = new char[charArray.length]; |         for(int i=0; i<workerArray.length;i++) | ||||||
|         int position = 0; // current position |             workerArray[i]=Character.toLowerCase(workerArray[i]); | ||||||
|         for(char currChar:charArray) { |         return workerArray; | ||||||
|             currChar=Character.toLowerCase(currChar); //normalize |  | ||||||
|             boolean exists=false; |  | ||||||
|             for(char targetChar:workingArray) if(targetChar==currChar) exists=true; |  | ||||||
|             if(!exists){ |  | ||||||
|                 workingArray[position]=currChar; |  | ||||||
|                 position++; |  | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     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]); | ||||||
|         } |         } | ||||||
|         char[] targestArray = new char[position]; | 
 | ||||||
|         for(int i=0;i<position;i++) targestArray[i] = workingArray[i]; |  | ||||||
|         Arrays.sort(targestArray); |  | ||||||
|         return targestArray; |  | ||||||
|     } |     } | ||||||
|     // Improved function: less code, less demanding |     public static int scale(int value, int oldMax, int newMax){ | ||||||
|     public static char[] usedChars2(char[] workingArray){ |         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 |         // workingArray gets an new hashcode after modifying, so its okay using it | ||||||
|         // now we remove all characters that occur multiple times |         // now we remove all characters that occur multiple times | ||||||
|         // we also count how many chars we got |         int counter=0; // we also count how many different chars we found | ||||||
|         int counter=0; |         for(int i=0; i<workerArray.length;i++){ | ||||||
|         for(int i=0; i<workingArray.length;i++){ |             if((int)workerArray[i]!=0) { //there is no need to run if the char is already the null character | ||||||
|             if((int)workingArray[i]!=0) { |  | ||||||
|                 counter++; // found a new one -> increment counter |                 counter++; // found a new one -> increment counter | ||||||
|                 // now remove all similar chars from that point till the end of the array |                 // now remove all similar chars from that point till the end of the array | ||||||
|                 for (int j = i + 1; j < workingArray.length; j++) { |                 for (int j = i + 1; j < workerArray.length; j++) // loop through starting from current pos. | ||||||
|                     if (workingArray[i] == workingArray[j]) workingArray[j] = (char) 0; // set the char to 	"Null character" |                     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 |         char[] targetArray = new char[counter]; // create new | ||||||
|         int targetPosition = 0; // initiate counter for the Target Array |         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 |             if((int)currChar!=0){ // if its not a null character it is a new char | ||||||
|                 targetArray[targetPosition] = currChar; // add the current char |                 targetArray[targetPosition] = currChar; // add the current char | ||||||
|                 targetPosition++; // we added a new char, so we need to increment for the next one |                 targetPosition++; // we added a new char, so we need to increment for the next one | ||||||
|             } |             } | ||||||
|         } | 
 | ||||||
|         return targetArray; |         return targetArray; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user