inital commit
This commit is contained in:
commit
d1948b0e58
67 changed files with 5280 additions and 0 deletions
3
Lab104-VenbergGE/manifest.mf
Normal file
3
Lab104-VenbergGE/manifest.mf
Normal file
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
89
Lab104-VenbergGE/src/Client.java
Normal file
89
Lab104-VenbergGE/src/Client.java
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Gabriel Venberg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import javax.swing.JOptionPane;
|
||||
import java.util.Scanner;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
public class Client {
|
||||
public static String fileToString(String filePath) throws FileNotFoundException{//helper function.
|
||||
File file = new File(filePath);
|
||||
Scanner input = new Scanner(file);
|
||||
String output = "";
|
||||
while(input.hasNextLine()){
|
||||
output = output +'\n'+input.nextLine();
|
||||
}
|
||||
return output;
|
||||
}
|
||||
public static void main(String[] args) throws FileNotFoundException{
|
||||
String[] functions = {"Harmonic", "sum", "find"};
|
||||
|
||||
String input;
|
||||
while(true){
|
||||
input = (String) JOptionPane.showInputDialog(null, "What function would you like to test?",
|
||||
"Testing", JOptionPane.QUESTION_MESSAGE, null, functions, functions[1]);
|
||||
|
||||
switch(input){
|
||||
case "Harmonic":
|
||||
input = (String) JOptionPane.showInputDialog(null, "what harmonic number do you want?", "Harmonic", 0);
|
||||
double hResult =Recursion.harmonic(Integer.parseInt(input));
|
||||
JOptionPane.showMessageDialog(null, "the result is "+hResult);
|
||||
System.out.println("tested harmonic, input was "+input+" and result was "+hResult);
|
||||
break;
|
||||
case "sum":
|
||||
while(true){ //the loop is just to provide the option of continuing if you want
|
||||
input = (String) JOptionPane.showInputDialog(null, "enter the file containing the array", "Sum", 0);
|
||||
int sResult;
|
||||
try{
|
||||
sResult = Recursion.sum(new File(input));
|
||||
JOptionPane.showMessageDialog(null, sResult);
|
||||
System.out.println("testing sum, user entered "+input+" and file contained "+fileToString(input)+". result was "+sResult);
|
||||
}
|
||||
catch(FileNotFoundException e){
|
||||
int reply = JOptionPane.showConfirmDialog(null, "file does not exit", "continue?", 0);
|
||||
if(reply == 0){
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "find": //you didnt say anything about how this loop should behave. Also, you spesifically said the find method has a void return type, so I cant use the dialog box for output.
|
||||
input = (String) JOptionPane.showInputDialog(null, "Enter the directory you want to search", "Find", 0);
|
||||
String input2 = JOptionPane.showInputDialog(null, "enter your seach term", 0);
|
||||
try{
|
||||
Recursion.find(input, input2);
|
||||
System.out.println("tested find, start directory was "+input+" and search term was "+input2);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
JOptionPane.showMessageDialog(null, "cant find that file");
|
||||
}
|
||||
break;
|
||||
}
|
||||
int doAgain = JOptionPane.showConfirmDialog(null, null, "continue?", 0);
|
||||
if(doAgain==1){break;}
|
||||
}
|
||||
}
|
||||
}
|
119
Lab104-VenbergGE/src/Recursion.java
Normal file
119
Lab104-VenbergGE/src/Recursion.java
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Gabriel Venberg
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gabriel Venberg
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
import java.lang.StringBuilder;
|
||||
|
||||
public class Recursion {
|
||||
|
||||
public static int sum(File file) throws FileNotFoundException{
|
||||
Scanner input = new Scanner(file);
|
||||
|
||||
int fileLength = 0;
|
||||
//wont be using the singly linked list class from last time, as it was really incomplete. Ill hack this together with a proper array.
|
||||
while(input.hasNext()==true){
|
||||
if(input.hasNextInt()){
|
||||
fileLength ++;
|
||||
input.nextInt();
|
||||
}
|
||||
else{
|
||||
input.next();
|
||||
}
|
||||
}
|
||||
input.close();
|
||||
|
||||
input = new Scanner(file); //resetting the scanner.
|
||||
|
||||
int[] product = new int[fileLength];
|
||||
|
||||
for(int i=0; i<product.length; i++){
|
||||
product[i]=input.nextInt();
|
||||
}
|
||||
|
||||
return sumCalc(product);
|
||||
}
|
||||
|
||||
public static double harmonic(double n){
|
||||
if(n<=0){
|
||||
throw new IllegalArgumentException("input must be > 1");
|
||||
}
|
||||
else if(n==1){
|
||||
return 1;
|
||||
}
|
||||
else{
|
||||
return (1.0/n)+harmonic(n-1.0);
|
||||
}
|
||||
}
|
||||
|
||||
//splitting up the code here, was to implement the sum by itself.
|
||||
|
||||
private static int sumCalc(int a[]){
|
||||
/* It can actually be realy easy to check if a number is a power of 2 thanks to binary.
|
||||
all powers of 2 take the form of 1 followed by a n 0's. If we sub 1 from this number,
|
||||
it becomes 0 followed by n 0's. Something like this (for the number 16)
|
||||
10000 (16)
|
||||
01111 (15)
|
||||
So taking a bitwise and will give us 0. (note that this only works with counting numbers, so no 0!)
|
||||
|
||||
Note that I dont have a proof that only powers of 2 do this, just a strong
|
||||
suspicion and a brute force search of the first million integers in python.
|
||||
I may have gotten a bit distracted...
|
||||
*/
|
||||
if((a.length&(a.length-1))==0){
|
||||
if(a.length==1){return a[0];}
|
||||
else{
|
||||
int[] tmp = new int[a.length/2];
|
||||
for(int i=0; i<tmp.length; i++){
|
||||
tmp[i] = a[2*i]+a[2*i+1];
|
||||
}
|
||||
return sumCalc(tmp);
|
||||
}
|
||||
}
|
||||
else{
|
||||
throw new IllegalArgumentException("input must be a power of 2.");
|
||||
}
|
||||
}
|
||||
|
||||
public static void find(String startPath, String filename) throws IOException{
|
||||
/*so, I spent an hour trying to write a (O(n!)(I think)) recursive substring search function,
|
||||
rather than a find [startPath] -name 'filename' ... I can *totaly* read...
|
||||
*/
|
||||
|
||||
File startFile = new File(startPath);
|
||||
|
||||
if(!startFile.isDirectory()){ //base case is a non-directory
|
||||
if(startFile.getName().equals(filename)){
|
||||
System.out.println(startFile.getCanonicalPath());
|
||||
}
|
||||
}
|
||||
else{
|
||||
String[] nextFiles = startFile.list(); //array of files to try next
|
||||
|
||||
for(int i=0; i<nextFiles.length; i++){
|
||||
find(nextFiles[i], filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue