-
-
Save cwake/d272b1f2fef82dbf7b99 to your computer and use it in GitHub Desktop.
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.PrintStream; | |
import java.util.Scanner; | |
import java.util.Arrays; | |
//CSC 142 , Check personality, personalityTest.java | |
// June 12, 2015, Chloe Wake | |
// Reads through a file of results from the Keirsey Personality test and outputs the answers in a output file. | |
public class outline4 { | |
public static final int DIMENSIONS = 4;//the number of personality dimensions | |
public static void main(String[] args) throws FileNotFoundException{ | |
//Scanner console = new Scanner(System.in); | |
userIntro(); //give an intro to the user | |
processFile(); //read in the file and store it so we can access it | |
} | |
//method that reads in a file and stores each line in an array | |
public static void processFile() throws FileNotFoundException { | |
Scanner console = new Scanner(System.in);//create scanner for user input | |
File fileName = inputFile(console); //store the file the user input typed in | |
Scanner read1 = new Scanner(fileName);//create scanner for the .txt file specified by user | |
Scanner inputFile = new Scanner(fileName); //make a new scanner to read the file now that we know it exists | |
PrintStream output = new PrintStream(outputFile(console));//create printStream object for output | |
// while loop to get the desired output | |
while (inputFile.hasNextLine()) { // while the file has text in the line read through it | |
String name = inputFile.nextLine(); // first line is the name of the person who took the test, go to the next line | |
String answers = inputFile.nextLine().toUpperCase(); // line after the name is the | |
int[] findA = countA(answers); | |
int[] findB = countB(answers); | |
int[] percentage = percentage(findB); | |
String[] type = type(percentage); | |
outputFile(name, percentage, type, findA, findB); | |
} | |
} | |
public static File inputFile(Scanner console) {//ask for file | |
System.out.print("Type the filename of the file you want to input: "); | |
File answer = new File(console.nextLine()); | |
while (!answer.exists()) { //if it doesn't exist, throw an error | |
System.out.println("That file does not exist! Try again."); | |
answer = new File(console.nextLine()); //store the file the user input typed in | |
} | |
return answer; | |
} | |
//method that writes output back into a text file | |
public static File outputFile(Scanner console) { | |
System.out.print("output file name? ");// PrintStream to write to a user specified output file | |
File output = new File (console.nextLine()); | |
return output; | |
} | |
//method to count how many B's the person answered and stores them in an array | |
public static int[] countB(String answers) { // takes the information from the user input that was stored in the answers variable | |
int[] countB = new int[DIMENSIONS]; // method declares the array of 4, searching through 4 at a time | |
for (int i = 0; i < answers.length(); i++) { // for every time i is less than the length of the answer continue on | |
char t = answers.charAt(i); // declare a char and make it set to the character at place i in the answers | |
if (t == 'B') { // if at that spot you find a B continue | |
if (i % 7 == 0) { // found a B is the spot it is at evenly divisible by 7 aka in the 7th spot? | |
countB[0]++; // ok then at it to the array in the first spot | |
} | |
if (i % 7 == 1 || i % 7 == 2) { //is the B found in | |
countB[1]++;// ok then at it to the array in the second spot | |
} | |
if (i % 7 == 3 || i % 7 ==4) { | |
countB[2]++;// ok then at it to the array in the third spot | |
} | |
if (i % 7 == 5 || i % 7 == 6) { | |
countB[3]++; // ok then at it to the array in the fourth spot | |
} | |
} | |
} | |
return countB; | |
} | |
// method counts the A's in each dimension and return the total as an array | |
public static int[] countA(String answers) { | |
int[] countA = new int[DIMENSIONS]; | |
for (int i = 0; i < answers.length(); i++) { | |
char t = answers.charAt(i); | |
if (t == 'A') { | |
if (i % 7 == 0) { | |
countA[0]++; | |
} | |
if (i % 7 == 1 || i % 7 == 2) { | |
countA[1]++; | |
} | |
if (i % 7 == 3 || i % 7 ==4) { | |
countA[2]++; | |
} | |
if (i % 7 == 5 || i % 7 == 6) { | |
countA[3]++; | |
} | |
} | |
} | |
return countA; | |
} | |
//method to take the percentage of B for each type section and see what category they fit in | |
public static String[]type(int[] percentage) { // string type takes the int array from the percentage method | |
String[] type = new String[DIMENSIONS]; // type is a string with a array length of 4 | |
for (int i = 0; i <= type.length - 1; i++) { // for when i is less than or equal to the length of the type minus one add one and keep going | |
if (percentage[i] > 50) { // if the percentage at spot "i" is greater than 50 | |
if (i == 0) { // if i is nothing type at spot 0 | |
type[0] = "I"; // if i is nothing type at spot 0 is 0 then it's an I | |
} else if (i == 1) { | |
type[1] = "N"; // | |
} else if (i == 2) { | |
type[2] = "F"; // | |
} else { | |
type[3] = "P"; // | |
} | |
} else if (percentage[i] < 50) { | |
if (i == 0) { | |
type[0] = "E"; | |
} else if (i == 1) { | |
type[1] = "S"; | |
} else if (i == 2) { | |
type[2] = "T"; | |
} else { | |
type[3] = "J"; | |
} | |
} else if (percentage[i] == 50) { | |
type[i] = "X"; | |
} | |
} | |
return type; | |
} | |
// method to calculate the percentages of 'B' answers | |
public static int[] percentage(int[] countB) { // takes the information from the method count B | |
int[] percentage = new int[DIMENSIONS]; // declares an array length of 4 | |
percentage[0] =100*countB[0]/10; // makes the percent into a whole number | |
for (int i = 0; i < countB.length; i++) { // for when i is less than the length of the array for countB keep going till you have all in the array | |
double d = percentage[i]; // takes a double of percent at place i in the array so 1,2, 3, 4 | |
percentage[i] = (int) Math.round(d); // at place i make it an int that rounds the double | |
} | |
for(int i = 0; i< percentage.length; i++){ | |
double j = percentage[i]; | |
percentage[i]= (int) Math.round(j); | |
} | |
return percentage; | |
} | |
// The resulting output | |
public static void outputFile(String name, int[] percentage, String[] type, int[] countA, int[] countB) { | |
System.out.println(name + ": \n"); // print out the name plus : and end line | |
System.out.print("answers: " + Arrays.toString(countA) + "\n"); // print out the answers for countA in an array, ends line | |
System.out.println( Arrays.toString(countB) ); | |
System.out.println("percent B: " + Arrays.toString(percentage)); | |
System.out.println("type: " + Arrays.toString(type)); | |
System.out.println(); | |
} | |
//introduction so the user knows what the program is about | |
public static void userIntro() { | |
System.out.println("This program processes a file of answers to the"); | |
System.out.println("Keirsey Temperament Sorter. It will convert the"); | |
System.out.println("string of A and B answers for each person into"); | |
System.out.println("a sequence that finds how many answers were B"); | |
System.out.println(", how many were A and of what percent were B and then into a"); | |
System.out.println("four-letter personality type."); | |
System.out.println(); | |
} | |
} |
Never mind, not a group project
Hello,
My name is Jonathan and I work with the introductory computer science courses at the University of Washington. This post contains a solution to a DNA homework assignment identical to the one we use in our class.
This post's code is indexed by Google, enabling our current students to easily find it and submit it as their own work
Would you please remove this post or make it secret in order to help us enforce our academic policy?
By removing this post, you would be removing a tool that our students could use in academic misconduct.
Thank you,
Jonathan Sanders
Intro Support, Computer Science & Engineering
University of Washington
jsanders@cs.washington.edu
Hello,
My name is Jonathan and I work with the introductory computer science courses at the University of Washington. This post contains a solution to a DNA homework assignment identical to the one we use in our class.
This post's code is indexed by Google, enabling our current students to easily find it and submit it as their own work
Would you please remove this post or make it secret in order to help us enforce our academic policy?
By removing this post, you would be removing a tool that our students could use in academic misconduct.
Thank you,
Jonathan Sanders
Intro Support, Computer Science & Engineering
University of Washington
jsanders@cs.washington.edu
You're forgetting about self-learners like me. I have no other ways of viewing the answer to these problems. If your students are copying off of here, I recommend slightly changing the requirements in such a way that some amount of work is still required. Don't forget that even if you manage to remove it, cheaters will be cheaters, and will probably just copy off of someone.
ありがとう ございます,
茂木陽介
Most of this is from project 3 that we just did but it gives us a skeleton to works with.