daniel_lawson9999

Daniel Lawson Chapter 6 Exercises All Evens

May 21st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.03 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------------------------------------------------------
  2. //Exercise 2
  3. /**
  4.  * Created by Daniel on 5/17/2016.
  5.  */
  6. import java.util.Scanner;
  7.  
  8. public class exercise2 {
  9.     public static void main(String[] args){
  10.         String test = "5 7 2 8 9 10 12 98 7 14 20 22";
  11.         Scanner s = new Scanner(test);
  12.         evenNumbers(s);
  13.  
  14.     }
  15.     public static void evenNumbers(Scanner s){
  16.         int count = 0;//keeps track of total numbers
  17.         int sum = 0;//keeps track of total sum
  18.         int evens = 0;//keeps track of total evens
  19.         while(s.hasNextInt()){//while there is another number
  20.             count++;// up the count
  21.             int num = s.nextInt();//get the next integer
  22.             sum+= num;// increment the sum by the next integer
  23.             if(num%2==0)evens++;//see if the integer is even
  24.         }
  25.        System.out.printf("%d numbers, sum = %d\n%d evens (%.2f%%)\n",count,sum,evens,(evens*1.0/count*100));//print the results
  26.     }
  27.  
  28. }
  29. //-------------------------------------------------------------------------------------------------------------------------------------
  30. //Exercise 4
  31. /**
  32.  * Created by Daniel on 5/17/2016.
  33.  */
  34. import java.util.*;
  35. public class exercise4 {
  36.     public static void main(String[] args){
  37.         Scanner s = new Scanner("3 pennies 2 quarters 1 Pennies 23 NickLes 4 DIMES");
  38.         countCoins(s);
  39.     }
  40.     public static void countCoins(Scanner s){
  41.         double sum = 0;// keeps track of sum
  42.         while(s.hasNextInt()){// as long as there is another integer
  43.             int n = s.nextInt();// store the integer
  44.             String type = s.next();// store the string of the integer
  45.             //increment the sum based off the type of coin:
  46.             if(type.equalsIgnoreCase("pennies"))sum+=n*.01;
  47.             else if(type.equalsIgnoreCase("nickles"))sum+=n*.05;
  48.             else if(type.equalsIgnoreCase("dimes"))sum+=n*.10;
  49.             else if(type.equalsIgnoreCase("quarters"))sum+=n*.25;
  50.         }
  51.         System.out.printf("Total Money: $%.2f",sum);//print out the total sum
  52.     }
  53.  
  54. }
  55. //-------------------------------------------------------------------------------------------------------------------------------------
  56. //Exercise 6
  57.  
  58. /**
  59.  * Created by Daniel on 5/18/2016.
  60.  */
  61. import java.util.*;
  62. import java.io.*;
  63. public class exercise6 {
  64.     public static void main(String[] args)throws FileNotFoundException{
  65.         File f = new File("hours.txt");
  66.         Scanner s = new Scanner(f);
  67.         System.out.println(readEntireFile(s));
  68.     }
  69.     public static String readEntireFile(Scanner s){
  70.         String text = "";//String that holds the file
  71.         while(s.hasNextLine())text+=s.nextLine() + "\n";//for each line in the old file create a new line in the string
  72.         return text;//return the string
  73.     }
  74. }
  75. //-------------------------------------------------------------------------------------------------------------------------------------
  76. //Exercise 8
  77.  
  78. /**
  79.  * Created by Daniel on 5/18/2016.
  80.  */
  81. import java.util.*;
  82. import java.io.*;
  83. public class exercise8 {
  84.     public static void main(String[] args)throws FileNotFoundException{
  85.         Scanner s = new Scanner(new File("hours.txt"));
  86.         PrintStream o = new PrintStream("actions.txt");
  87.         doubleSpace(s,o);
  88. }
  89.     public static void doubleSpace(Scanner s, PrintStream o){
  90.         String text = "";//will store new double spaced file
  91.         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
  92.         o.println(text);//print text to the new file
  93.     }
  94. }
  95.  
  96. //-------------------------------------------------------------------------------------------------------------------------------------
  97. //Exercise 10
  98. /**
  99.  * Created by Daniel on 5/18/2016.
  100.  */
  101. import java.util.*;
  102. import java.io.*;
  103. public class exercise10 {
  104.     public static void main(String[] args)throws FileNotFoundException {
  105.         File f = new File("actions.txt");
  106.         wordWrap(f);
  107.     }
  108.     public static void wordWrap(File f)throws FileNotFoundException {
  109.         Scanner s = new Scanner(f);// create a scanner for the file
  110.         String text = "";//create a string which will store the file
  111.         while (s.hasNextLine()) text += s.nextLine();//put the file in to the string
  112.         PrintStream o = new PrintStream(f);//we can now overide the original file because we have stored it in a string
  113.         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())));
  114.         //run a for loop for equal to the length of text / 60.0 rounded up (ceil).
  115.         //in each iteration of the foor loop it will output the next 60 characters.
  116.         //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
  117.         //this prevents the program from trying to create a substring passed the size of the document and instead will limit it
  118.         // to the size of the document
  119.     }
  120. }
  121. //-------------------------------------------------------------------------------------------------------------------------------------
  122. //Exercise 12
  123. /**
  124.  * Created by Daniel on 5/18/2016.
  125.  */
  126. import java.util.*;
  127. import java.io.*;
  128. public class exercise12 {
  129.     public static void main(String[] args)throws FileNotFoundException{
  130.         File f = new File("actions.txt");
  131.         stripHtmlTags(f);
  132.     }
  133.     public static void stripHtmlTags(File f)throws FileNotFoundException{
  134.         Scanner s = new Scanner(f);
  135.         String text = "";//will store document
  136.         while(s.hasNextLine())text+= s.nextLine() + "\n";//copy document to string
  137.         boolean canMove  = true;//holds value of whether or not them cursor is in a html tag
  138.         for(int i = 0; i < text.length(); i++){//run for each character
  139.             if(text.charAt(i) == '<')canMove = false;//set value to false if < is present
  140.             if(canMove)System.out.print(text.charAt(i));//print text if < is not present
  141.             if(text.charAt(i) == '>')canMove = true;//document can resume printing when > is present
  142.  
  143.     }
  144. }
  145. }
  146.  
  147.  
  148. //-------------------------------------------------------------------------------------------------------------------------------------
  149. //Exercise 14
  150.  
  151. /**
  152.  * Created by Daniel on 5/19/2016.
  153.  */
  154. import java.util.*;
  155. public class exercise14 {
  156.     public static void main(String[] args){
  157.         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";
  158.         Scanner scan = new Scanner(s);
  159.         printDuplicates(scan);
  160.     }
  161.     public static void printDuplicates(Scanner s){
  162.         String word = "";//stores word
  163.         String prevWord = "";//stores previous word
  164.         int count = 1;//keeps track the count
  165.         while(s.hasNextLine()){//while there is a line
  166.             String line = s.nextLine();//put the next line in a string
  167.             Scanner l = new Scanner(line);//create a scanner for the line
  168.             prevWord = l.next();// get the first word of the line
  169.             while(l.hasNext()){//while there is a word in the next line
  170.                 word = l.next();//store the next word
  171.                 if(word.equalsIgnoreCase(prevWord))count++;//up the word count if prevWord and the current word are equal
  172.                 else{//if they are not equal
  173.                     if(count >= 2)System.out.print(prevWord+"*"+count +" ");//if the count was >= 2, print the word and how many times it showed up
  174.                     count = 1;//set the count back to one
  175.                 }
  176.                 prevWord = word;//set the previous word equal to word
  177.             }
  178.             if(count >= 2)System.out.print(prevWord+"*"+count +" ");//check again in case the last word was part of a series of words
  179.             //because in the while loop it only prints the count if there was a change
  180.             System.out.println();
  181.         }
  182.  
  183.     }
  184. }
  185.  
  186. //-------------------------------------------------------------------------------------------------------------------------------------
  187. //Exercise 16
  188. /**
  189.  * Created by Daniel on 5/21/2016.
  190.  */
  191. import java.util.*;
  192. public class exercise16 {
  193.     public static void main(String[] args){
  194.         String s = "Benson Eric Kim KIm Kim Jenny Nancy Nancy Paul Paul\nEthan Jamie Jamie Alyssa Alyssa Helene Helene Jessica Jessica\n";
  195.         Scanner scan = new Scanner(s);
  196.         mostCommonNames(scan);
  197.     }
  198.     public static void mostCommonNames(Scanner l){
  199.         while(l.hasNextLine()){//while there is a line to process
  200.             String line = l.nextLine();//create a string for the line
  201.             Scanner s = new Scanner(line);//create a scanner for the string
  202.             String tempName = "";//stores string of the current name
  203.             String prevTempName = "";//stores string of the last name
  204.             String name = "";//stores the string of the name that shows up the most
  205.             int tempCount = 1;// keeps track of how many times a temporary name shows up
  206.             int maxCount = 1;//keeps track of the most shown name
  207.             if(s.hasNext()){//get first name
  208.                 prevTempName = s.next();
  209.                 name = prevTempName;
  210.             }
  211.             while(s.hasNext()){//while there is a name
  212.                 tempName = s.next();// get the next name
  213.                 if(tempName.equalsIgnoreCase(prevTempName))tempCount++;//if the name and last name are equal up the tempCount
  214.                 if(tempCount > maxCount){//if the tempCount is > than the maxCount, set the maxCount to the tempCount, and assign the name to the temp name
  215.                     maxCount = tempCount;
  216.                     name = tempName;
  217.                 }
  218.                 if(!tempName.equalsIgnoreCase(prevTempName))tempCount = 1;//reset counter if they are not equal
  219.                 prevTempName  = tempName;//set prevTempName equal to tempName
  220.             }
  221.             System.out.printf("Most common: %s\n",name);//print the lines most common name
  222.         }
  223.     }
  224. }
  225.  
  226. //-------------------------------------------------------------------------------------------------------------------------------------
  227. //Exercise 18
  228. /**
  229.  * Created by Daniel on 5/21/2016.
  230.  */
  231. import java.util.*;
  232. public class exercise18 {
  233.     public static void main(String[] args){
  234.         String example = "Kane, Erica\n--+-+\nChandler, Adman\n++-+\nMartin, Jake\n+++++++";
  235.         Scanner s = new Scanner(example);
  236.         plusScores(s);
  237.     }
  238.     public static void plusScores(Scanner s){
  239.         while(s.hasNextLine()){//while there is a next line
  240.             int plus = 0;//keeps track of the pluses
  241.             String name = s.nextLine();//get the name
  242.             String score = s.nextLine();//get the pluses / minuses
  243.             for(int i  = 0 ;i < score.length();i++)if(score.charAt(i) == '+')plus++;//record the number of plusses
  244.             System.out.printf("%s: %.1f%% plus\n",name,plus*1.0/score.length()*100);//print the results
  245.         }
  246.     }
  247. }
Add Comment
Please, Sign In to add comment