daniel_lawson9999

All Chapter 7

Oct 19th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.38 KB | None | 0 0
  1. All of chapter 7
  2. Self check even
  3. Daniel Lawson 10/4/2016
  4. 2.
  5. numbers[0] first element , numbers[9] last element if it is 10 long,numbers[numbers.length-1] last element no matter the length
  6. 4.
  7. int max = 38;
  8. int min = -7;
  9. int[] odd = new int[Math.abs(max - min+2)/2 ];
  10. for(int i = 0;i<odd.length;i++){
  11.     odd[i] = min + i*2;
  12. }
  13. System.out.println(Arrays.toString(odd));
  14. 6.
  15.  
  16. 0 = 3,    1 = 3, 2    3     4 = 6,    5 = 9     6       7 = -18,    the array = [3,3,0,0,6,9,0,-18]
  17.  
  18. 8.
  19.  a. int[] a = {17,-3,42,5,9,28};
  20.  
  21. 10.
  22. public static int max(int[] array){
  23.  int max = 0;
  24.  for(int i = 0;i<array.length;i++){
  25.     if(array[i] > max)max = array[i];
  26.  }
  27.  return max;
  28. }
  29. 12.
  30. traversing an arrary is manipulating an array using for loops , an example of this is the max method created in the last problem.
  31. 14.
  32. the arrary contains 3, 24, 8, -5, 6, 1
  33. 16.
  34. for count change the int[] list into String[] list ,change target to a char,  and then have another for loop inside hte for each loop saying for(int i = 0; i < list[n].length();i++) if list[n].charAt(i)  = target count++;
  35. for equals change the type of list1, list 2 to String, also chang ethe if statement in the second forloop to !list1[i].equals(list2[i])
  36. RAW Paste Data
  37.  
  38. Daniel Lawson 10/4/2016
  39.  
  40. 2.
  41. numbers[0] first element , numbers[9] last element if it is 10 long,numbers[numbers.length-1] last element no matter the length
  42. 4.
  43. int max = 38;
  44. int min = -7;
  45. int[] odd = new int[Math.abs(max - min+2)/2 ];
  46. for(int i = 0;i<odd.length;i++){
  47.     odd[i] = min + i*2;
  48. }
  49. System.out.println(Arrays.toString(odd));
  50. 6.
  51.  
  52. 0 = 3,    1 = 3, 2    3     4 = 6,    5 = 9     6       7 = -18,    the array = [3,3,0,0,6,9,0,-18]
  53.  
  54. 8.
  55.  a. int[] a = {17,-3,42,5,9,28};
  56.  
  57. 10.
  58. public static int max(int[] array){
  59.  int max = 0;
  60.  for(int i = 0;i<array.length;i++){
  61.     if(array[i] > max)max = array[i];
  62.  }
  63.  return max;
  64. }
  65. 12.
  66. traversing an arrary is manipulating an array using for loops , an example of this is the max method created in the last problem.
  67. 14.
  68. the arrary contains 3, 24, 8, -5, 6, 1
  69. 16.
  70. for count change the int[] list into String[] list ,change target to a char,  and then have another for loop inside hte for each loop saying for(int i = 0; i < list[n].length();i++) if list[n].charAt(i)  = target count++;
  71. for equals change the type of list1, list 2 to String, also chang ethe if statement in the second forloop to !list1[i].equals(list2[i])
  72.  
  73. Weather:
  74. /**
  75.  * Created by danie on 9/30/2016.
  76.  */
  77. import java.util.*;
  78. public class weather {
  79.     public static void main(String[] args){
  80.         Scanner s = new Scanner(System.in);
  81.         System.out.print("How many day's high temp? ");
  82.         int[] day = new int[s.nextInt()];
  83.         double average = 0;
  84.         for(int i =0;i<day.length;i++){
  85.             System.out.print("Enter day  " +  (i+1) +": ");
  86.             day[i] = s.nextInt();
  87.             average+=day[i];
  88.         }
  89.         average /= day.length;
  90.         System.out.printf("Average temp= %.1f\n",average);
  91.         int count  = 0;
  92.         for(int i = 0;i<day.length;i++){
  93.             if(day[i] > average){
  94.                 count++;
  95.             }
  96.         }
  97.         System.out.println(count + " days were above average");
  98.         System.out.println("Temperatures: " + Arrays.toString(day));
  99.         Arrays.sort(day);
  100.         System.out.println("Two coldest days: " + day[0] + ", " + day[1]);
  101.         System.out.println("Two hottest days: " + day[day.length -1] + ", "+  day[day.length-2]);
  102.     }
  103. }
  104.  
  105. public class mostCommon { //gets the most common number (
  106.     public static void main(String[] args){
  107.         System.out.println(most(655534));
  108.  
  109.     }
  110.     public static int most(int n){
  111.         int[] howMany = new int[10];
  112.         while(n>0){
  113.             int temp = n % 10;
  114.             howMany[temp]++;
  115.             n /= 10;
  116.         }
  117.  
  118.         int most = 0;
  119.         int valueOfMost = 0;
  120.         for(int i = 0; i <10;i++){
  121.             if(howMany[i] > most){
  122.                 most = howMany[i];
  123.                 valueOfMost = i;
  124.             }
  125.         }
  126.         return valueOfMost;
  127.     }
  128. }
  129.  
  130. /**
  131.  * Created by danie on 10/5/2016.
  132.  */
  133. //Merges multiple arrays
  134. import java.util.*;
  135. public class merge {
  136.     public static void main(String[] args){
  137.         int[] food = {1,2,3,4,5};
  138.         int[] ood = {6,7,8,9,10};
  139.         int[] od = {11,12,13,14,15};
  140.         System.out.println(Arrays.toString(merge(food,ood)));
  141.         System.out.println(Arrays.toString(merge3(food,ood,od)));
  142.     }
  143.     public static int[] merge(int[] a1, int[] a2){
  144.         int[] merge = new int[a1.length + a2.length];
  145.         for(int i  = 0; i < merge.length;i++){
  146.             if(i < a1.length){
  147.                merge[i]  = a1[i];
  148.             }
  149.             else{
  150.                 merge[i] = a2[i - a1.length];
  151.             }
  152.         }
  153.         return merge;
  154.     }
  155.     public static int[] merge3(int[] a1, int[] a2, int[] a3){
  156.         return merge(merge(a1,a2),a3);
  157.     }
  158. }
  159. //Finds the median of the string
  160. /**
  161.  * Created by danie on 10/17/2016.
  162.  */
  163. import java.util.*;
  164. public class median {
  165.     public static void main(String[] args){
  166.         int[] test = {1,2,4,3,5,6,7,9};
  167.         System.out.println(median(test));//prints 5
  168.     }
  169.     public static int median(int[] a){
  170.         int[] sort  = Arrays.copyOf(a,a.length);
  171.         Arrays.sort(sort);
  172.         return sort[sort.length/2];
  173.     }
  174. }
  175. //reverses the order of a string
  176. public class copy {
  177.     public static void main(String args[]){
  178.         int[] values = {1,2,3,4,5,6,45};
  179.         reverseOrder(values);
  180.         System.out.println(Arrays.toString(values));
  181.     }
  182.     public static void reverseOrder(int[] a){
  183.         for(int i = 0;i<a.length/2;i++){
  184.             int temp = a[i];
  185.             a[i] = a[a.length -1 - i];
  186.             a[a.length-1-i] = temp;
  187.         }
  188.     }
  189. }
  190.  
  191. public class attendanceProgram {
  192. /*
  193. yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna
  194. ayyanyyyyayanaayyanayyyananayayaynyayayynynya
  195. yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny */ // the file to read from
  196.     public static void main(String[] args)throws FileNotFoundException{
  197.         File f = new File ("C:\\Users\\danie\\IdeaProjects\\project7\\src\\textfile.txt");
  198.         Scanner s = new Scanner(f);
  199.         int section = 1;
  200.         while(s.hasNextLine()){
  201.             String line =  s.nextLine();
  202.             int[] lineValue = new int[5];
  203.             double[] lineValueAverage = new double[5];
  204.             for(int i = 0;i<9;i++){
  205.                 for(int j = 0;j<5;j++){
  206.                     if(line.charAt(5*i+j)=='y')lineValue[j]+=3;
  207.                     if(line.charAt(5*i+j)=='n')lineValue[j]+=2;
  208.                     lineValue[j] = Math.min(20,lineValue[j]);
  209.                 }
  210.             }
  211.  
  212.             for(int i = 0; i < 5; i++) lineValueAverage[i] = lineValue[i] * 5;
  213.             System.out.println("Section " +section);
  214.             System.out.println("Student points: " + Arrays.toString(lineValue));
  215.             System.out.println("Student points: " + Arrays.toString(lineValueAverage));
  216.             System.out.println();
  217.             section++;
  218.         }
  219.     }
  220. }
  221.  
  222. /**
  223.  * Created by danie on 10/17/2016.
  224.  */
  225. import java.io.*;
  226. import java.util.*;
  227.  
  228. /*
  229. hello hi asdf y y y 123456 12
  230. 1234567890 1234567890  
  231. */ // file being read from
  232.  
  233. public class wordLengths {
  234.     public static void main(String[] args)throws FileNotFoundException{
  235.         File f = new File("C:\\Users\\danie\\IdeaProjects\\project7\\src\\wordLengths.txt");
  236.         wordLengths(f);
  237.     }
  238.     public static void wordLengths (File f)throws FileNotFoundException{
  239.         Scanner l = new Scanner(f);
  240.         Scanner s = new Scanner(f);
  241.         int length = 0;
  242.         while(l.hasNext()){
  243.             String temp = l.next();
  244.             length = Math.max(temp.length(),length);
  245.         }
  246.         int[] count = new int[length];
  247.         while(s.hasNext()){
  248.             String temp = s.next();
  249.             count[temp.length()-1]++;
  250.         }
  251.         for(int i = 0;i <count.length;i++){
  252.             System.out.printf(" %4d: %4d ",i+1,count[i] );
  253.             for(int j = 0;j<count[i];j++){
  254.                 System.out.print("*");
  255.             }
  256.             System.out.println();
  257.         }
  258.     }
  259. }
  260. // Grocery list with arrays
  261. /**
  262.  * Created by danie on 9/29/2016.
  263.  */
  264. public class GroceryList {
  265.   public static final int size = 10;
  266.   private GroceryItemOrder[] total = new GroceryItemOrder[size];
  267.   private int count = 0;
  268.  
  269.     public GroceryList(){
  270.         for(int i = 0;i<total.length;i++)total[i] = new GroceryItemOrder("null",0,0);
  271.     };
  272.     public void add(GroceryItemOrder item){
  273.         total[count] = item;
  274.         count++;
  275.     }
  276.     public double getTotalCost(){//returns the total cost of all the items
  277.         double sum = 0;
  278.         for(int i = 0;i< total.length; i++)sum+=total[i].getCost();
  279.         return sum;
  280.     }
  281. }
  282.  
  283. /**
  284.  * Created by danie on 9/29/2016.
  285.  */
  286. public class GroceryItemOrder {
  287.     private String name;
  288.     private int quantity;
  289.     private double pricePerUnit;
  290.     public GroceryItemOrder(String name, int quantity, double pricePerUnit){
  291.         this.name = name;
  292.         this.quantity =  quantity;
  293.         this.pricePerUnit = pricePerUnit;
  294.     }
  295.     public double getCost(){
  296.         return quantity * pricePerUnit;
  297.     }
  298.     public void setQuantity(int quantity){
  299.         this.quantity = quantity;
  300.     }
  301.     public void setPricePerUnit(int cost){
  302.         pricePerUnit = cost;
  303.     }
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment