daniel_lawson9999

Daniel Lawson Chapter 5 Exercises 2-24 even

May 8th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.66 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------------------------------------------------------
  2. //Exercise 2
  3. /**
  4.  * Created by Daniel on 5/6/2016.
  5.  */
  6. public class exercise2 {
  7.     public static void main(String[] args){
  8.         System.out.println(GDC(64,8));
  9.     }
  10.     public static int GDC(int a, int b){
  11.         int n = 0;
  12.         for(int i = Math.max(Math.abs(a),Math.abs(b));i>=1;i--)if(a%i == 0&& b%i == 0)n = Math.max(n,i);
  13.         return n;
  14.     }
  15. }
  16. //-------------------------------------------------------------------------------------------------------------------------------------
  17. //Exercise 4
  18. /**
  19.  * Created by Daniel on 5/6/2016.
  20.  */
  21. import java.util.*;
  22. public class exercise4 {
  23.     public static void main(String[] args){
  24.         randomX();
  25.     }
  26.     public static void randomX(){
  27.         Random r = new Random();
  28.         int random = 0;
  29.         while(!(random>=16)){
  30.             random = r.nextInt(16) + 5;
  31.             for(int i = 1; i <= random; i++){
  32.                 System.out.print("*");
  33.             }
  34.             System.out.println();
  35.         }
  36.     }
  37. }
  38.  
  39. //-------------------------------------------------------------------------------------------------------------------------------------
  40. //Exercise 6
  41. /**
  42.  * Created by Daniel on 5/6/2016.
  43.  */
  44. import java.util.*;
  45. public class exercise6 {
  46.     public static void main(String[] args){
  47.         makeGuesses();
  48.     }
  49.     public static void makeGuesses(){
  50.         int g = 0;
  51.         int c = 0;
  52.         Random r = new Random();
  53.         while(g<48){
  54.             g = r.nextInt(50)+1;
  55.             c++;
  56.             System.out.printf("guess = %d\n",g);
  57.         }
  58.         System.out.printf("total guesses = %d\n",c);
  59.     }
  60.  
  61. }
  62.  
  63. //-------------------------------------------------------------------------------------------------------------------------------------
  64. //Exercise 8
  65. /**
  66.  * Created by Daniel on 5/8/2016.
  67.  */
  68. import java.util.*;
  69. public class exercise8 {
  70.     public static void main(String[] args){
  71.         randomWalk();
  72.     }
  73.     public static void randomWalk(){
  74.         Random r = new Random();
  75.         int p = 0;
  76.         int m = 0;
  77.         while(Math.abs(p)<3){
  78.             if(r.nextInt(2)==1)p+=1;
  79.             else p-=1;
  80.             System.out.printf("position = %2d\n", p);
  81.             m = Math.max(m,p);
  82.         }
  83.         System.out.printf("max position = %2d",m);
  84.     }
  85.  
  86.  
  87. }
  88. //-------------------------------------------------------------------------------------------------------------------------------------
  89. //Exercise 10
  90. /**
  91.  * Created by Daniel on 5/8/2016.
  92.  */
  93. import java.util.*;
  94. public class exercise10 {
  95.     public static void main(String[] args){
  96.         hopscotch(3);
  97.     }
  98.     public static void hopscotch(int h){
  99.         if(h>=0)System.out.println("   1  ");
  100.         int n = 1;
  101.         for(int i = 1; i<=h; i++){
  102.             n++;
  103.             int s = new String("" + n).length() + new String("" +(n+1)).length();
  104.             System.out.print(n);
  105.             for(int j = 1;j<=7-s; j++)System.out.print(" ");
  106.             System.out.println(n+1);
  107.             n+=2;
  108.             System.out.printf("%4d\n",n);
  109.         }
  110.  
  111.     }
  112. }
  113. //-------------------------------------------------------------------------------------------------------------------------------------
  114. //Exercise 12
  115. /**
  116.  * Created by Daniel on 5/8/2016.
  117.  */
  118. import java.util.*;
  119. public class exercise12 {
  120.     public static void main(String[] args){
  121.         Scanner s = new Scanner(System.in);
  122.         printAverage(s);
  123.     }
  124.     public static void printAverage(Scanner s){
  125.         double n = 0;
  126.         int c = 0;
  127.         int m = 0;
  128.         System.out.print("Type a number: ");
  129.         n = s.nextDouble();
  130.         while(n>=0){
  131.             m+= n;
  132.             c++;
  133.             System.out.print("Type a number: ");
  134.             n = s.nextDouble();
  135.         }
  136.        if(c>0)System.out.println("Average was " + (m/c));
  137.         else System.out.println("Average was 0");
  138.     }
  139. }
  140. //-------------------------------------------------------------------------------------------------------------------------------------
  141. //Exercise 14
  142. /**
  143.  * Created by Daniel on 5/8/2016.
  144.  */
  145. public class Exercise14 {
  146.     public static void main(String[] args){
  147.        System.out.println(hasMidpoint(2,5,8));
  148.     }
  149.     public static boolean hasMidpoint(int n1,int n2, int n3){
  150.         int small = Math.min(n1,Math.min(n2,n3));
  151.         int  big  = Math.max(n1,Math.max(n2,n3));
  152.         int mid = n1 + n2 + n3 -big - small;
  153.         return ((big -small)/2 + small == mid);
  154.     }
  155.  
  156. }
  157. //-------------------------------------------------------------------------------------------------------------------------------------
  158. //Exercise 16
  159. /**
  160.  * Created by Daniel on 5/8/2016.
  161.  */
  162. public class exercise16 {
  163.     public static void main(String[] args){
  164.         System.out.println(anglePairs(120,60,30));
  165.     }
  166.     public static boolean anglePairs(int n1, int n2, int n3){
  167.         return (n1 + n2 == 180 || n1+n3 == 180 || n2+n3 ==180) && (n1+n2 == 90||n1+n3 == 90||n2+n3==90);
  168.     }
  169. }
  170. //-------------------------------------------------------------------------------------------------------------------------------------
  171. //Exercise 18
  172. /**
  173.  * Created by Daniel on 5/8/2016.
  174.  */
  175. public class exercise18 {
  176.     public static void main(String[] args){
  177.        System.out.println(digitSum(29107));
  178.     }
  179.     public static int digitSum(int n){
  180.         int s = 0;
  181.         while(Math.abs(n)>0){
  182.           s += n%10;
  183.           n/=10;
  184.         }
  185.         return s;
  186.     }
  187. }
  188. //-------------------------------------------------------------------------------------------------------------------------------------
  189. //Exercise 20
  190. /**
  191.  * Created by Daniel on 5/8/2016.
  192.  */
  193. public class exercise20 {
  194.     public static void main(String[] args){
  195.         System.out.println(digitRange(6));
  196.         System.out.println(digitRange(68437));
  197.     }
  198.     public static int digitRange(int n){
  199.         int max = 0;
  200.         int min = 10;
  201.         while(Math.abs(n)>0){
  202.             max = Math.max(max, n%10);
  203.             min = Math.min(min, n%10);
  204.             n/=10;
  205.         }
  206.         return (max - min +1);
  207.     }
  208. }
  209. //-------------------------------------------------------------------------------------------------------------------------------------
  210. //Exercise 22
  211. /**
  212.  * Created by Daniel on 5/8/2016.
  213.  */
  214. public class exercise22 {
  215.     public static void main(String args[]){
  216.         System.out.println(allDigitsOdd(135319));
  217.         System.out.println(allDigitsOdd(915213));
  218.     }
  219.     public static boolean allDigitsOdd(int n){
  220.         while(n>0){
  221.             if((n%10)%2 == 0) return false;
  222.             n/=10;
  223.         }
  224.         return true;
  225.     }
  226. }
  227. //-------------------------------------------------------------------------------------------------------------------------------------
  228. //Exercise 24
  229. /**
  230.  * Created by Daniel on 5/8/2016.
  231.  */
  232. public class exercise24 {
  233.     public static void main(String[] args){
  234.         System.out.println(isAllVowels("eIEiO"));
  235.         System.out.println(isAllVowels("oink"));
  236.         System.out.println(isAllVowels(" "));
  237.     }
  238.     public static boolean isAllVowels(String s) {
  239.         for (int i = 1; i <= s.length(); i++) {
  240.             if (Character.toLowerCase(s.charAt(i - 1)) != 'a' && Character.toLowerCase(s.charAt(i - 1)) != 'e'&&                                                        Character.toLowerCase(s.charAt(i - 1)) != 'i' && Character.toLowerCase(s.charAt(i - 1)) != 'o' && Character.toLowerCase(s.charAt(i - 1)) != 'u'&& Character.toLowerCase(s.charAt(i - 1)) != ' ') {//really long if statement
  241.                return false;
  242.            }
  243.         }
  244.  
  245.         return true;
  246.     }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment