daniel_lawson9999

Daniel Lawson Chapter 4 Self Check 2-18 even

Apr 19th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. Daniel Lawson 2-18 self-check (even)
  2.  
  3. 2.
  4. a. true
  5. b. false
  6. c. true
  7. d. false
  8. e. false
  9. f. false
  10. g. false
  11. h. true
  12. i. true
  13.  
  14.  
  15. 4.
  16. -in line 4, it does not store the return of the minimum in a container. (Which would be smaller in this case)
  17. -in line 5, they used = instead of ==
  18. -in line 6, they forgot to put a outside of the the "" to use its actual values.
  19. -in line 10, they set the method as a void when it should int because it returns an integer.
  20. -in line 16, you do not need to specify the data type of smaller when returning it because it was already defined
  21. -The program does not have an if statement to handle if smaller is b, or both values are equal. It could be structured something like:
  22. if (a == b){
  23.  a is the same as b
  24. }else if(smaller == b){
  25.  b is smaller
  26. }else if(smaller == a){
  27. a is smaller
  28. }
  29. -in line 13 it forgot to put the if after the else and between (a=>v)
  30. -This is an 8th bonus error! - The program does not need to create a minimum method because one could already use the built in Math.min method
  31. 6.
  32. a.
  33. // b = 5, b  = 6,
  34. it would print:10 6
  35. b.
  36. // a = 9, b = 9,
  37. it would print:9 9
  38. c.
  39. //a = 3
  40. it would print:3 4
  41. d. a = 30, a = 29
  42. it would print:29 30
  43. 8.
  44. if the number % 2 == 0 is true, but number % 3 is false, then it will print that the number is odd when it is actually even
  45. in the correct version the else statement should be outside of the first if.
  46. It would also be nice if it said something if it was even but not divisble by 6like:
  47. Scanner console =  new Scanner(System.in);
  48. System.out.print("Type a number: ");
  49. int number = console.nextInt();
  50. if(number % 2 ==0){
  51.     System.out.println("Even");
  52.     if(number % 3 == 0){
  53.         System.out.println("Divisible by 6.");
  54.         }
  55.     }
  56. else{
  57. System.out.println("Odd.");
  58. }
  59. 10.
  60. a = 2;
  61. if(x<30){
  62. x++;
  63. }
  64. System.out.println("Java is awesome" + x);
  65. 12.
  66. Scanner c = new Scanner(System.in);
  67.  
  68. public static int m(Scanner c){
  69. System.out.print("How much will John be spending? ");
  70. double amount = c.nextDouble();
  71. System.out.println();
  72. int numBills = (int) (amount / 20.0);
  73. if(numBills * 20.0 <amount){
  74.     numBills++;
  75. }
  76. return numBills;
  77. }
  78. int b1 = m(c);
  79. int b2 = m(c);
  80. System.out.println("John needs " + b1 +" bills");
  81. System.out.println("John needs " + b2 +" bills");
  82. 14.
  83. /**
  84.  * Created by Daniel on 4/19/2016.
  85.  */
  86. import java.util.*;
  87. public class selfCheck14 {
  88.     public static void main(String[] args){
  89.         Scanner c = new Scanner(System.in);
  90.         card(c);
  91.     }
  92.     public static void card(Scanner c){
  93.         String rank = "error";
  94.         String suit ="error";
  95.         System.out.print("Enter a card: ");
  96.         String s = c.nextLine();
  97.         s = s.toUpperCase();
  98.         if(s.contains("2")){
  99.             rank = "Two";
  100.         }
  101.         if(s.contains("3")){
  102.             rank = "Three";
  103.         }
  104.         if(s.contains("4")){
  105.             rank = "Four";
  106.         }
  107.         if(s.contains("5")){
  108.             rank = "Five";
  109.         }
  110.         if(s.contains("6")){
  111.             rank = "Six";
  112.         }
  113.         if(s.contains("7")){
  114.             rank = "Seven";
  115.         }
  116.         if(s.contains("8")){
  117.             rank ="Eight";
  118.         }
  119.         if(s.contains("9")){
  120.             rank = "Nine";
  121.         }
  122.         if(s.contains("10")){
  123.             rank = "10";
  124.         }
  125.         if(s.contains("J")){
  126.             rank = "Jack";
  127.         }
  128.         if(s.contains("Q")){
  129.             rank = "Queen";
  130.         }
  131.         if(s.contains("K")){
  132.             rank = "King";
  133.         }
  134.         if(s.contains("A")){
  135.             rank = "Ace";
  136.         }
  137.         if(s.contains("C")){
  138.             suit = "Clubs";
  139.         }
  140.         if(s.contains("D")){
  141.             suit = "Diamonds";
  142.         }
  143.         if(s.contains("H")){
  144.             suit = "Hearts";
  145.         }
  146.         if(s.contains("S")){
  147.             suit = "Spades";
  148.         }
  149.         System.out.println(rank + " of " + suit);
  150.     }
  151. }
  152. 16.
  153. It returns the value of a factor every time it finds one in the for loop instead of the sum.
  154. It should first create a integer called factors before the for loop, and set it to 0
  155. In the n % i == 0 statement it should then increment the factor variable by one (factors++)
  156. at the outside of the for loop after it has finished, it should then return factors.
  157. 18.
  158. It evaluates to around 6.8 but with a lot of trailing zeroes because it is a double value, therefore something like 6.8000000.
Advertisement
Add Comment
Please, Sign In to add comment