Advertisement
MemeMast3r

Chapter 6 Exercises

Oct 6th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. Chapter 6 Exercises
  2.  
  3. 1. while: A loop structure, which executes a set of statements over and over again based on a condition.
  4. do-while: In the do-while statement the condition is not evaluated until after the first execution of the loop.
  5.  
  6.  
  7. 2.
  8. package review.prompter;
  9. import java.util.Scanner;
  10. public class ReviewPrompter {
  11. public static void main(String[] args) {
  12.  
  13. int min, max, num;
  14.  
  15. Scanner input = new Scanner(System.in);
  16. System.out.print("Enter the minimum number: ");
  17. min = input.nextInt();
  18. System.out.print("Enter the maximum number: ");
  19. max = input.nextInt();
  20. System.out.print("Enter a number between " + min +" and " + max +": ");
  21. num = input.nextInt();
  22.  
  23. while(num >= max || num <= min){
  24. System.out.print("Wrong! Try again: ");
  25. num = input.nextInt();
  26. }
  27. }
  28. }
  29.  
  30.  
  31. 3. A Sentinel is a constant that stores a value that is used to signify that a loop should stop iterating.
  32.  
  33.  
  34. 4.
  35. package review_percentpassing;
  36. import java.util.Scanner;
  37. public class Review_PercentPassing {
  38. public static void main(String[] args) {
  39. int score;
  40. int scoreValues = 0;
  41. int passing = 0;
  42. double avg;
  43. final int SENTINEL = 0;
  44. Scanner input = new Scanner(System.in);
  45.  
  46. System.out.print("Enter a score out of 100 (Enter 0 to finish): ");
  47. score = input.nextInt();
  48. while(score != SENTINEL){
  49. scoreValues += 1;
  50. if(score >= 70){
  51. passing += 1;}
  52. System.out.print("Enter another score: ");
  53. score = input.nextInt();}
  54. input.close();
  55. avg=(double)passing / (double)scoreValues;
  56. System.out.printf("%2.2f %2s\n",avg * 100 , " Percent Passing");
  57. }
  58. }
  59.  
  60.  
  61. 5.
  62. package review_oddsum;
  63. import java.util.Scanner;
  64. public class Review_OddSum {
  65. public static void main(String[] args) {
  66. int num;
  67. int num2 = 1;
  68. int oddSum = 0;
  69. Scanner input = new Scanner(System.in);
  70.  
  71. System.out.print("Enter a number: ");
  72. num = input.nextInt();
  73.  
  74. while (num2 != num){
  75. num2 ++;
  76. if (num2 % 2 == 1){
  77. oddSum ++;
  78. }
  79. }
  80. System.out.print("Odd sum is : " + oddSum);
  81. }
  82. }
  83.  
  84.  
  85. 6.
  86. package chapter6.pkg6;
  87. import java.util.Scanner;
  88. public class Chapter66 {
  89. public static void main(String[] args) throws Exception {
  90. for ( int i=0; i<80; i++ ){
  91. if ( i%10 == 0 ){
  92. System.out.print("Parkside Robotics is cool. \t");
  93. Thread.sleep(200);}
  94. }
  95. }
  96. }
  97.  
  98.  
  99. 7.
  100. package stringdemo;
  101. import java.util.Scanner;
  102. public class StringDemo {
  103. public static void main(String[] args) {
  104. String phrase, threeLetters;
  105. int phraseLength;
  106. int mid;
  107. Scanner input = new Scanner(System.in);
  108.  
  109. System.out.print("Enter text that contains at least three characters: ");
  110. phrase = input.nextLine();
  111. input.close();
  112.  
  113. phraseLength = phrase.length();
  114. mid = phraseLength / 2;
  115.  
  116. threeLetters = phrase.substring(mid - 1, mid + 2);
  117. System.out.println("Middle three characters are: "
  118. + threeLetters);
  119. }
  120. }
  121.  
  122.  
  123. 8. a) The position of a character in string is called its index
  124. b) A string is said to be immutable because it cannot be changed
  125. c) A string without an assigned value
  126. d) Generated from calling a methof from a String object
  127. e) The Unicode Standard is a 16-bit encoding system that assigns a value for each character and symbol of every language
  128.  
  129.  
  130. 9.
  131. package review_accountsetup;
  132. import java.util.Scanner;
  133. public class Review_AccountSetup {
  134. public static void main(String[] args) {
  135. String username, password;
  136. int passLength;
  137. Scanner input = new Scanner(System.in);
  138.  
  139. System.out.print("Enter a username: ");
  140. username = input.nextLine();
  141. System.out.print("Enter a password that is at least 8 characters: ");
  142. password = input.nextLine();
  143. passLength = password.length();
  144.  
  145. while (passLength < 8){
  146. System.out.print("Enter a password that is at least 8 characters: ");
  147. password = input.nextLine();
  148. passLength = password.length();}
  149.  
  150. username = username.toLowerCase();
  151. password = password.toLowerCase();
  152.  
  153. System.out.println("Your username is "+ username + " and your password is " + password);
  154. }
  155. }
  156.  
  157.  
  158. 10.
  159. package formalgreeting;
  160. import java.util.Scanner;
  161. public class FormalGreeting {
  162. public static void main(String[] args) {
  163. String name, nameL;
  164. Scanner input = new Scanner(System.in);
  165.  
  166. System.out.print("Enter your name, including your title: ");
  167. name = input.next();
  168. nameL = name.toLowerCase();
  169.  
  170. if ("mr.".equals(nameL) || "mr".equals(nameL)){
  171. System.out.println("Hello, sir.");
  172. }else{
  173. if ("ms.".equals(nameL) || "ms".equals(nameL) || "mrs.".equals(nameL) || "mrs.".equals(nameL) || "miss".equals(nameL)){
  174. System.out.println("Hello, ma'am.");
  175. }else{
  176. System.out.println("Hello, " + name);}}
  177. }
  178. }
  179.  
  180.  
  181. 11.
  182. #5 60 times
  183.  
  184. #6 x = 0
  185.  
  186. #11 a) 10
  187. b) "my "
  188. c) "my string."
  189. d) There would be an error that would be fixed by adding () after "toUpperCase"
  190. e) "my string."
  191.  
  192. #12 a) false
  193. b) true
  194. c) 24
  195. d) 0
  196. e) -8
  197. f) 9
  198. g) 9
  199. h) true
  200. i) true
  201.  
  202.  
  203. 12.
  204. #18 Monogram
  205.  
  206. package monogram;
  207. import java.util.Scanner;
  208. public class Monogram {
  209. public static void main(String[] args) {
  210. String first, middle, last;
  211. Scanner input = new Scanner(System.in);
  212.  
  213. System.out.print("Enter yuor first name: ");
  214. first = input.nextLine();
  215. first = first.toLowerCase();
  216. System.out.print("Enter your middle initial: ");
  217. middle = input.nextLine();
  218. middle = middle.toLowerCase();
  219. System.out.print("Enter your last name: ");
  220. last = input.nextLine();
  221. last = last.toUpperCase();
  222. input.close();
  223.  
  224. System.out.println("Your monogram is: " + first.substring(0,1) + last.substring(0,1) + middle.substring(0,1));
  225. }
  226. }
  227.  
  228.  
  229. #19 RemoveString
  230.  
  231. package removestring;
  232. import java.util.Scanner;
  233. public class RemoveString {
  234. public static void main(String[] args) {
  235. String sentence, remove, sentence2;
  236. Scanner input = new Scanner(System.in);
  237.  
  238. System.out.print("Enter a sentence: ");
  239. sentence = input.nextLine();
  240. System.out.print("Enter a string: ");
  241. remove = input.nextLine();
  242. input.close();
  243.  
  244. sentence2 = sentence.replace(remove + "","");
  245. System.out.println(sentence2);
  246. }
  247. }
  248.  
  249.  
  250. #21 GroupAssignment
  251.  
  252. package groupassignment;
  253. import java.util.Scanner;
  254. public class GroupAssignment {
  255. public static void main(String[] args) {
  256. String first, last;
  257. int group1, group2;
  258. Scanner input = new Scanner(System.in);
  259.  
  260. System.out.print("Enter your first name: ");
  261. first = input.nextLine();
  262. System.out.print("Enter your last name: ");
  263. last = input.nextLine();
  264. input.close();
  265.  
  266. group1 = last.compareToIgnoreCase("i");
  267. if (group1 < 1){
  268. System.out.println(first + " " + last + " is assigned to Group 1");}
  269. else{
  270. group2 = last.compareToIgnoreCase("s");
  271. if (group2 < 1){
  272. System.out.println(first + " " + last + " is assigned to Group 2");}
  273. else{
  274. System.out.println(first + " " + last + " is assigned to Group 3");}
  275. }
  276. }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement