Advertisement
zainarfi00

Chapter 4 Exercises

Oct 21st, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. /**
  2. * Pg 305 Number 2
  3. *
  4. * Zain Arfi
  5. * @version (a version number or a date)
  6. */
  7. public class Pg305_2
  8. {
  9. public static void main(String [] args)
  10. {
  11. String x = "hello";
  12. int y = 3;
  13. Count(x,y);
  14. }
  15. public static void Count( String x, int y)
  16. {
  17. for(int i = 1; i <= y; i++)
  18. {
  19. System.out.print(x);
  20. }
  21. }
  22. }
  23.  
  24.  
  25. Pg 305 Number 4
  26. import java.util.*;
  27. public class Pg305_4
  28. {
  29. public static void main(String[] args)
  30. {
  31. Scanner console = new Scanner(System.in);
  32. System.out.println("What is the number of the month?");
  33. int x = console.nextInt();
  34. Days(x);
  35. }
  36. public static void Days(int x)
  37. {
  38. if(x == 1)
  39. {
  40. System.out.println("31");
  41. }
  42. else if(x == 2)
  43. {
  44. System.out.println("28");
  45. }
  46. else if(x == 3)
  47. {
  48. System.out.println("31");
  49. }
  50. else if(x == 4)
  51. {
  52. System.out.println("30");
  53. }
  54. else if(x == 5)
  55. {
  56. System.out.println("31");
  57. }
  58. else if(x == 6)
  59. {
  60. System.out.println("30");
  61. }
  62. else if(x == 7)
  63. {
  64. System.out.println("31");
  65. }
  66. else if(x == 8)
  67. {
  68. System.out.println("31");
  69. }
  70. else if(x == 9)
  71. {
  72. System.out.println("30");
  73. }
  74. else if(x == 10)
  75. {
  76. System.out.println("31");
  77. }
  78. else if(x == 11)
  79. {
  80. System.out.println("30");
  81. }
  82. else if(x == 12)
  83. {
  84. System.out.println("31");
  85. }
  86. }
  87. }
  88.  
  89.  
  90. Pg 306 Number 6
  91. /**
  92. * Pg 306 # 6
  93. *
  94. * Zain Arfi
  95. * @version (a version number or a date)
  96. */
  97. public class Pg306_6
  98. {
  99. public static void printRange(int x, int y)
  100. {
  101. for(int number = x; number <= y; number++)
  102. {
  103. System.out.println(x);
  104. }
  105. }
  106. }
  107.  
  108.  
  109. /**
  110. * Pg 307 Number 10
  111. *
  112. * Zain Arfi
  113. * @version (a version number or a date)
  114. */
  115. public class Pg306_10
  116. {
  117. public static void printGPA(int a, int b, int c, int d)
  118. {
  119. int e = (a+b+c+d)/4;
  120. System.out.println("Maria's grade is" + e);
  121. }
  122. }
  123.  
  124.  
  125. /**
  126. * Pg 307 Number 12
  127. *
  128. * Zain Arf
  129. * @version (a version number or a date)
  130. */
  131. public class Pg306_12
  132. {
  133. public static void printTriType(int a, int b, int c)
  134. {
  135. if(a == b)
  136. {
  137. System.out.println("isoceles");
  138. }
  139. else if(b == c)
  140. {
  141. System.out.println("isoceles");
  142. }
  143. else if(a == b && b == c)
  144. {
  145. System.out.println("equilateral");
  146. }
  147. else
  148. {
  149. System.out.println("scalene");
  150. }
  151. }
  152. }
  153.  
  154.  
  155.  
  156.  
  157. /**
  158. * Pg 308 Number 16
  159. *
  160. * Z@in @rfi
  161. * @version (a version number or a date)
  162. */
  163. public class Pg308_16
  164. {
  165. public static void printPalindrome(String a)
  166. {
  167. for(int i=0; i <= a.length(a) + 1; i++)
  168. {
  169. if(a.charAt(i) == a.charAt(a.length(a)-i))
  170. {
  171. System.out.println("This is a palindrome!!");
  172. }
  173. else
  174. {
  175. System.out.println("This is not a palindrome!!");
  176. }
  177. }
  178. }
  179. }
  180.  
  181.  
  182.  
  183. Pg 308 Number 20
  184. public class Pg308_20
  185. {
  186. public static void numUnique(int x, int y, int z)
  187. {
  188. if (x != y && x!=z && y != z)
  189. {
  190. System.out.println("3");
  191. }
  192. else if( x == y && y != z && x != z)
  193. {
  194. System.out.println("2");
  195. }
  196. else if(x == z && x != y && y != z)
  197. {
  198. System.out.println("2");
  199. }
  200. else if(y == z && x != z && y != x)
  201. {
  202. System.out.println("2");
  203. }
  204. else if (y == z && z == x && y == x)
  205. {
  206. System.out.println("0");
  207. }
  208. else
  209. {
  210. System.out.println("1");
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement