daniel_lawson9999

Daniel Lawson Chapter 4 Exercises 2-20 Even

Apr 24th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.44 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------------------------------------------------------
  2. // Exercise 2
  3. /**
  4.  * Created by Daniel on 4/21/2016.
  5.  */
  6. public class Exercise2 {
  7.     public static void main(String[] args){
  8.         String str = repl("lol",3);
  9.         System.out.println(str);
  10.     }
  11.     public static String repl(String s, int l){
  12.         String str = "";
  13.         for(int i = 1; i <= l; i++){
  14.             str = str + s;
  15.         }
  16.         return str;
  17.     }
  18. }
  19. //-------------------------------------------------------------------------------------------------------------------------------------
  20. // Exercise 4
  21. /**
  22.  * Created by Daniel on 4/21/2016.
  23.  */
  24. public class Exercise4 {
  25.     public static void main(String[] args) {
  26.         int days = daysInMonth(4);
  27.         System.out.println(days);
  28.     }
  29.  
  30.     public static int daysInMonth(int m) {
  31.     int n = 31;
  32.     if (m == 2) n = 28;
  33.     if (m == 4 || m == 6 || m == 9 || m == 11) n = 30;
  34.         return n;
  35.  
  36.     }
  37. }
  38. //-------------------------------------------------------------------------------------------------------------------------------------
  39. // Exercise 6
  40. /**
  41.  * Created by Daniel on 4/21/2016.
  42.  */
  43. public class Exercise6 {
  44.     public static void main(String[] args){
  45.         printRange(19,11);
  46.         printRange(5,5);
  47.         printRange(2,7);
  48.     }
  49.     public static void printRange(int n1, int n2){
  50.       for(int i = Math.min(n1,n2); i <= Math.max(n1,n2);i++)System.out.print(i + " ");
  51.       System.out.println();
  52.     }
  53. }
  54.  
  55. //-------------------------------------------------------------------------------------------------------------------------------------
  56. // Exercise 8
  57. /**
  58.  * Created by Daniel on 4/22/2016.
  59.  */
  60.  
  61. import java.util.*;
  62.  
  63. public class Exercise8 {
  64.     public static void main(String[] args) {
  65.         Scanner c = new Scanner(System.in);
  66.         smallestLargest(c);
  67.     }
  68.  
  69.     public static void smallestLargest(Scanner c) {
  70.         System.out.print("How many numbers do ou want to enter?");
  71.         int n = c.nextInt();
  72.         int min = 0;
  73.         int max = 0;
  74.         int num = 0;
  75.         if(n<= 0) throw new IllegalArgumentException("You can't enter numbers <= 0!");
  76.         for (int i = 1; i <= n; i++) {
  77.             System.out.printf("Number %d: ", i);
  78.             num = c.nextInt();
  79.             max = Math.max(num,max);
  80.             min = Math.min(num,min);
  81.         }
  82.         System.out.printf("Smallest = %d\n",min);
  83.         System.out.printf("Largest = %d\n",max);
  84.  
  85.     }
  86.  
  87. }
  88.  
  89. //-------------------------------------------------------------------------------------------------------------------------------------
  90. // Exercise 10
  91. /**
  92.  * Created by Daniel on 4/22/2016.
  93.  */
  94. import com.sun.javaws.exceptions.InvalidArgumentException;
  95.  
  96. import java.util.*;
  97. public class Exercise10 {
  98.     public static void main(String[] args){
  99.         Scanner c = new Scanner(System.in);
  100.         printGPA(c);
  101.     }
  102.     public static void printGPA(Scanner c){
  103.         System.out.print("Enter a student record: ");
  104.         String name = c.next();
  105.         int n = c.nextInt();
  106.         double sum = 0;
  107.         for(int i = 1; i <= n;i++){
  108.             double num  = c.nextDouble();
  109.             sum += num;
  110.         }
  111.         sum /= n;
  112.         System.out.printf("%s's grade is %.2f",name,sum);
  113.  
  114.     }
  115. }
  116.  
  117. //-------------------------------------------------------------------------------------------------------------------------------------
  118. // Exercise 12
  119. /**
  120.  * Created by Daniel on 4/22/2016.
  121.  */
  122. public class Exercise12 {
  123.     public static void main(String[] args){
  124.         printTriangleType(5,7,7);
  125.         printTriangleType(6,6,6);
  126.         printTriangleType(5,7,8);
  127.         //printTriangleType(2,18,2); // will throw an exception
  128.     }
  129.     public static void printTriangleType(double s1, double s2, double s3){
  130.         double h = Math.max(s1,s2);
  131.         h = Math.max(h,s3);
  132.         double s = s1 + s2 + s3 - h;
  133.         double m = Math.min(s1,s2);
  134.         m = Math.min(m,s3);
  135.         if(h>s || m<= 0)throw new IllegalArgumentException("Sum of any two sides should not be lower than the third side, and side lengths should not be negative");
  136.         String type;
  137.        if(s1 == s2 && s1 == s3){
  138.            type = "equilateral";
  139.        }else if(s1 == s2 || s1 == s3 || s2 == s3){
  140.            type = "isosceles";
  141.        }else{
  142.            type = "scalene";
  143.        }
  144.         System.out.println(type);
  145.     }
  146.  
  147. }
  148.  
  149. //-------------------------------------------------------------------------------------------------------------------------------------
  150. // Exercise 14
  151. /**
  152.  * Created by Daniel on 4/24/2016.
  153.  */
  154. public class Exercise14 {
  155.     public static void main(String[] args){
  156.        //Println statements to  test method
  157.        System.out.println(pow(-4.0,3));
  158.        System.out.println(pow(4.0,-2));
  159.     }
  160.     public static double pow(double n, int p){
  161.         double a = 1;
  162.         for(int i = 1;i<= Math.abs(p);i++){
  163.             if(p>0)a*=n;
  164.             if(p<0)a/=n;
  165.         }
  166.         return a;
  167.     }
  168. }
  169.  
  170. //-------------------------------------------------------------------------------------------------------------------------------------
  171. // Exercise 16
  172. /**
  173.  * Created by Daniel on 4/24/2016.
  174.  */
  175. import java.util.*;
  176. public class Exercise16 {
  177.     public static void main(String[] args) {
  178.         Scanner c = new Scanner(System.in);
  179.         printPalindrome(c);
  180.     }
  181.  
  182.     public static void printPalindrome(Scanner c) {
  183.         System.out.print("Enter a string : ");
  184.         String s = c.nextLine();
  185.         String b = "";
  186.         for (int i = s.length() - 1; i >= 0; i--) {
  187.             b += s.charAt(i);
  188.         }
  189.         if (s.equalsIgnoreCase(b)) System.out.printf("%s backwards is %s, %s is a palindrome", s, b, s);
  190.         else System.out.printf("%s backwards is %s, %s is not palindrome", s, b, s);
  191.     }
  192. }
  193. //-------------------------------------------------------------------------------------------------------------------------------------
  194. // Exercise 18
  195. /**
  196.  * Created by Daniel on 4/24/2016.
  197.  */
  198. public class Exercise18 {
  199.     public static void main(String[] args) {
  200.         //example calls
  201.         System.out.println(wordCount("hello"));
  202.         System.out.println(wordCount("how are you?"));
  203.         System.out.println(wordCount("  this  string  has  wide  spaces  "));
  204.         System.out.println(wordCount("  "));
  205.     }
  206.  
  207.     public static int wordCount(String s) {
  208.         boolean lastSpace = true;
  209.         boolean Space = false;
  210.         int count = 0;
  211.         for (int i = 0; i < s.length(); i++) {
  212.             if (s.charAt(i) == ' ') Space = true;
  213.             if (!Space && lastSpace) count++;
  214.             lastSpace = Space;
  215.             Space = false;
  216.         }
  217.         return count;
  218.     }
  219.  
  220. }
  221. //-------------------------------------------------------------------------------------------------------------------------------------
  222. // Exercise 20
  223. /**
  224.  * Created by Daniel on 4/24/2016.
  225.  */
  226. public class Exercise20 {
  227.     public static void main(String[] args) {
  228.         //println statements to test numUnique
  229.         System.out.println(numUnique(18, 3, 4));
  230.         System.out.println(numUnique(6, 7, 6));
  231.         System.out.println(numUnique(5, 5, 5));
  232.     }
  233.     public static int numUnique(int n1, int n2, int n3) {
  234.         int u = 3;
  235.         if (n1 == n2 && n1 == n3) u = 1;
  236.         else if (n1 == n2 || n1 == n3 || n2 == n3) u = 2;
  237.         return u;
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment