Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 2
- /**
- * Created by Daniel on 5/17/2016.
- */
- import java.util.Scanner;
- public class exercise2 {
- public static void main(String[] args){
- String test = "5 7 2 8 9 10 12 98 7 14 20 22";
- Scanner s = new Scanner(test);
- evenNumbers(s);
- }
- public static void evenNumbers(Scanner s){
- int count = 0;//keeps track of total numbers
- int sum = 0;//keeps track of total sum
- int evens = 0;//keeps track of total evens
- while(s.hasNextInt()){//while there is another number
- count++;// up the count
- int num = s.nextInt();//get the next integer
- sum+= num;// increment the sum by the next integer
- if(num%2==0)evens++;//see if the integer is even
- }
- System.out.printf("%d numbers, sum = %d\n%d evens (%.2f%%)\n",count,sum,evens,(evens*1.0/count*100));//print the results
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 4
- /**
- * Created by Daniel on 5/17/2016.
- */
- import java.util.*;
- public class exercise4 {
- public static void main(String[] args){
- Scanner s = new Scanner("3 pennies 2 quarters 1 Pennies 23 NickLes 4 DIMES");
- countCoins(s);
- }
- public static void countCoins(Scanner s){
- double sum = 0;// keeps track of sum
- while(s.hasNextInt()){// as long as there is another integer
- int n = s.nextInt();// store the integer
- String type = s.next();// store the string of the integer
- //increment the sum based off the type of coin:
- if(type.equalsIgnoreCase("pennies"))sum+=n*.01;
- else if(type.equalsIgnoreCase("nickles"))sum+=n*.05;
- else if(type.equalsIgnoreCase("dimes"))sum+=n*.10;
- else if(type.equalsIgnoreCase("quarters"))sum+=n*.25;
- }
- System.out.printf("Total Money: $%.2f",sum);//print out the total sum
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 6
- /**
- * Created by Daniel on 5/18/2016.
- */
- import java.util.*;
- import java.io.*;
- public class exercise6 {
- public static void main(String[] args)throws FileNotFoundException{
- File f = new File("hours.txt");
- Scanner s = new Scanner(f);
- System.out.println(readEntireFile(s));
- }
- public static String readEntireFile(Scanner s){
- String text = "";//String that holds the file
- while(s.hasNextLine())text+=s.nextLine() + "\n";//for each line in the old file create a new line in the string
- return text;//return the string
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 8
- /**
- * Created by Daniel on 5/18/2016.
- */
- import java.util.*;
- import java.io.*;
- public class exercise8 {
- public static void main(String[] args)throws FileNotFoundException{
- Scanner s = new Scanner(new File("hours.txt"));
- PrintStream o = new PrintStream("actions.txt");
- doubleSpace(s,o);
- }
- public static void doubleSpace(Scanner s, PrintStream o){
- String text = "";//will store new double spaced file
- while(s.hasNextLine())text+= s.nextLine() + "\n\n";//for each line in the old file, store it in text but with two more lines after
- o.println(text);//print text to the new file
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 10
- /**
- * Created by Daniel on 5/18/2016.
- */
- import java.util.*;
- import java.io.*;
- public class exercise10 {
- public static void main(String[] args)throws FileNotFoundException {
- File f = new File("actions.txt");
- wordWrap(f);
- }
- public static void wordWrap(File f)throws FileNotFoundException {
- Scanner s = new Scanner(f);// create a scanner for the file
- String text = "";//create a string which will store the file
- while (s.hasNextLine()) text += s.nextLine();//put the file in to the string
- PrintStream o = new PrintStream(f);//we can now overide the original file because we have stored it in a string
- for (int i = 0; i < Math.ceil(text.length() / 60.0); i++)o.println(text.substring(i * 60, Math.min((i + 1) * 60,text.length())));
- //run a for loop for equal to the length of text / 60.0 rounded up (ceil).
- //in each iteration of the foor loop it will output the next 60 characters.
- //the reason the Math.min exists is to make it so on the last iteration of the loop, in documents that do not have word amounts of 60,120,180
- //this prevents the program from trying to create a substring passed the size of the document and instead will limit it
- // to the size of the document
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 12
- /**
- * Created by Daniel on 5/18/2016.
- */
- import java.util.*;
- import java.io.*;
- public class exercise12 {
- public static void main(String[] args)throws FileNotFoundException{
- File f = new File("actions.txt");
- stripHtmlTags(f);
- }
- public static void stripHtmlTags(File f)throws FileNotFoundException{
- Scanner s = new Scanner(f);
- String text = "";//will store document
- while(s.hasNextLine())text+= s.nextLine() + "\n";//copy document to string
- boolean canMove = true;//holds value of whether or not them cursor is in a html tag
- for(int i = 0; i < text.length(); i++){//run for each character
- if(text.charAt(i) == '<')canMove = false;//set value to false if < is present
- if(canMove)System.out.print(text.charAt(i));//print text if < is not present
- if(text.charAt(i) == '>')canMove = true;//document can resume printing when > is present
- }
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 14
- /**
- * Created by Daniel on 5/19/2016.
- */
- import java.util.*;
- public class exercise14 {
- public static void main(String[] args){
- String s = "dasfasdf hello hello hello how how how are are are is\n is food is food yeah no no no no no!\n";
- Scanner scan = new Scanner(s);
- printDuplicates(scan);
- }
- public static void printDuplicates(Scanner s){
- String word = "";//stores word
- String prevWord = "";//stores previous word
- int count = 1;//keeps track the count
- while(s.hasNextLine()){//while there is a line
- String line = s.nextLine();//put the next line in a string
- Scanner l = new Scanner(line);//create a scanner for the line
- prevWord = l.next();// get the first word of the line
- while(l.hasNext()){//while there is a word in the next line
- word = l.next();//store the next word
- if(word.equalsIgnoreCase(prevWord))count++;//up the word count if prevWord and the current word are equal
- else{//if they are not equal
- if(count >= 2)System.out.print(prevWord+"*"+count +" ");//if the count was >= 2, print the word and how many times it showed up
- count = 1;//set the count back to one
- }
- prevWord = word;//set the previous word equal to word
- }
- if(count >= 2)System.out.print(prevWord+"*"+count +" ");//check again in case the last word was part of a series of words
- //because in the while loop it only prints the count if there was a change
- System.out.println();
- }
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 16
- /**
- * Created by Daniel on 5/21/2016.
- */
- import java.util.*;
- public class exercise16 {
- public static void main(String[] args){
- String s = "Benson Eric Kim KIm Kim Jenny Nancy Nancy Paul Paul\nEthan Jamie Jamie Alyssa Alyssa Helene Helene Jessica Jessica\n";
- Scanner scan = new Scanner(s);
- mostCommonNames(scan);
- }
- public static void mostCommonNames(Scanner l){
- while(l.hasNextLine()){//while there is a line to process
- String line = l.nextLine();//create a string for the line
- Scanner s = new Scanner(line);//create a scanner for the string
- String tempName = "";//stores string of the current name
- String prevTempName = "";//stores string of the last name
- String name = "";//stores the string of the name that shows up the most
- int tempCount = 1;// keeps track of how many times a temporary name shows up
- int maxCount = 1;//keeps track of the most shown name
- if(s.hasNext()){//get first name
- prevTempName = s.next();
- name = prevTempName;
- }
- while(s.hasNext()){//while there is a name
- tempName = s.next();// get the next name
- if(tempName.equalsIgnoreCase(prevTempName))tempCount++;//if the name and last name are equal up the tempCount
- if(tempCount > maxCount){//if the tempCount is > than the maxCount, set the maxCount to the tempCount, and assign the name to the temp name
- maxCount = tempCount;
- name = tempName;
- }
- if(!tempName.equalsIgnoreCase(prevTempName))tempCount = 1;//reset counter if they are not equal
- prevTempName = tempName;//set prevTempName equal to tempName
- }
- System.out.printf("Most common: %s\n",name);//print the lines most common name
- }
- }
- }
- //-------------------------------------------------------------------------------------------------------------------------------------
- //Exercise 18
- /**
- * Created by Daniel on 5/21/2016.
- */
- import java.util.*;
- public class exercise18 {
- public static void main(String[] args){
- String example = "Kane, Erica\n--+-+\nChandler, Adman\n++-+\nMartin, Jake\n+++++++";
- Scanner s = new Scanner(example);
- plusScores(s);
- }
- public static void plusScores(Scanner s){
- while(s.hasNextLine()){//while there is a next line
- int plus = 0;//keeps track of the pluses
- String name = s.nextLine();//get the name
- String score = s.nextLine();//get the pluses / minuses
- for(int i = 0 ;i < score.length();i++)if(score.charAt(i) == '+')plus++;//record the number of plusses
- System.out.printf("%s: %.1f%% plus\n",name,plus*1.0/score.length()*100);//print the results
- }
- }
- }
Add Comment
Please, Sign In to add comment