Advertisement
veronikaaa86

PF Java - Basic Syntax

May 18th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. 01. Student Information
  2.  
  3. public class P01StudentInformation {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. String name = scanner.nextLine();
  8. int age = Integer.parseInt(scanner.nextLine());
  9. double grade = Double.parseDouble(scanner.nextLine());
  10.  
  11. System.out.printf("Name: %s, Age: %d, Grade: %.2f", name, age, grade);
  12. }
  13. }
  14. ==================================================================================
  15. 02. Passed
  16.  
  17. public class P02Passed {
  18. public static void main(String[] args) {
  19. Scanner scanner = new Scanner(System.in);
  20.  
  21. double grade = Double.parseDouble(scanner.nextLine());
  22.  
  23. if (grade >= 3) {
  24. System.out.println("Passed!");
  25. }
  26. }
  27. }
  28. ==================================================================================
  29. 03. Passed or Failed
  30.  
  31. public class P02Passed {
  32. public static void main(String[] args) {
  33. Scanner scanner = new Scanner(System.in);
  34.  
  35. double grade = Double.parseDouble(scanner.nextLine());
  36.  
  37. if (grade >= 3) {
  38. System.out.println("Passed!");
  39. } else {
  40. System.out.println("Failed!");
  41. }
  42. }
  43. }
  44. ==================================================================================
  45. 04. Back In 30 Minutes
  46.  
  47. public class P04BackIn30Minutes {
  48. public static void main(String[] args) {
  49. Scanner scanner = new Scanner(System.in);
  50.  
  51. int initHour = Integer.parseInt(scanner.nextLine());
  52. int initMin = Integer.parseInt(scanner.nextLine());
  53.  
  54. int allMinutes = (initHour * 60) + initMin + 30;
  55.  
  56. int hour = allMinutes / 60;
  57. int min = allMinutes % 60;
  58.  
  59. if (hour > 23) {
  60. hour = 0;
  61. }
  62.  
  63. System.out.printf("%d:%02d", hour, min);
  64. }
  65. }
  66. ==================================================================================
  67. 05. Month Printer
  68.  
  69. public class P05MonthPrinter {
  70. public static void main(String[] args) {
  71. Scanner scanner = new Scanner(System.in);
  72.  
  73. int num = Integer.parseInt(scanner.nextLine());
  74.  
  75. String month = "";
  76. switch (num) {
  77. case 1: month = "January";break;
  78. case 2: month = "February";break;
  79. case 3: month = "March";break;
  80. case 4: month = "April";break;
  81. case 5: month = "May";break;
  82. case 6: month = "June";break;
  83. case 7: month = "July";break;
  84. case 8: month = "August";break;
  85. case 9: month = "September";break;
  86. case 10: month = "October";break;
  87. case 11: month = "November";break;
  88. case 12: month = "December";break;
  89. default: month = "Error!";
  90. }
  91.  
  92. System.out.println(month);
  93. }
  94. }
  95. ==================================================================================
  96. 06. Foreign Languages
  97.  
  98. public class P06ForeignLanguages {
  99. public static void main(String[] args) {
  100. Scanner scanner = new Scanner(System.in);
  101.  
  102. String country = scanner.nextLine();
  103.  
  104. switch (country){
  105. case "England":
  106. case "USA":
  107. System.out.println("English");
  108. break;
  109. case "Spain":
  110. case "Argentina":
  111. case "Mexico":
  112. System.out.println("Spanish");
  113. break;
  114. default:
  115. System.out.println("unknown");
  116. }
  117. }
  118. }
  119. ==================================================================================
  120. 07. Theatre Promotion
  121.  
  122. public class P07TheatrePromotion {
  123. public static void main(String[] args) {
  124. Scanner scanner = new Scanner(System.in);
  125.  
  126. String day = scanner.nextLine();
  127. int age = Integer.parseInt(scanner.nextLine());
  128.  
  129. boolean isNotValid = false;
  130. int price = 0;
  131. if (age >= 0 && age <= 18){
  132. if (day.equals("Weekday")) {
  133. price = 12;
  134. } else if (day.equals("Weekend")) {
  135. price = 15;
  136. } else if (day.equals("Holiday")) {
  137. price = 5;
  138. }
  139. } else if (age > 18 && age <= 64) {
  140. if (day.equals("Weekday")) {
  141. price = 18;
  142. } else if (day.equals("Weekend")) {
  143. price = 20;
  144. } else if (day.equals("Holiday")) {
  145. price = 12;
  146. }
  147. } else if (age > 64 && age <= 122) {
  148. if (day.equals("Weekday")) {
  149. price = 12;
  150. } else if (day.equals("Weekend")) {
  151. price = 15;
  152. } else if (day.equals("Holiday")) {
  153. price = 10;
  154. }
  155. } else {
  156. isNotValid = true;
  157. }
  158.  
  159. if (isNotValid) {
  160. System.out.println("Error!");
  161. } else {
  162. System.out.printf("%d$", price);
  163. }
  164. }
  165. }
  166. ==================================================================================
  167. 08. Divisible by 3
  168.  
  169. public class P08DivisibleBy3 {
  170. public static void main(String[] args) {
  171.  
  172. for (int i = 3; i <= 100; i += 3) {
  173. System.out.println(i);
  174. }
  175. }
  176. }
  177. ==================================================================================
  178. 09. Sum of Odd Numbers
  179.  
  180. public class P09SumOfOddNumbers {
  181. public static void main(String[] args) {
  182. Scanner scanner = new Scanner(System.in);
  183.  
  184. int num = Integer.parseInt(scanner.nextLine());
  185.  
  186. int sum = 0;
  187. for (int i = 1; i <= num * 2; i++) {
  188. if (i % 2 != 0) {
  189. System.out.println(i);
  190. sum = sum + i;
  191. }
  192. }
  193.  
  194. System.out.printf("Sum: %d", sum);
  195. }
  196. }
  197. ==================================================================================
  198. 10. Multiplication Table
  199.  
  200. public class P10MultiplicationTable {
  201. public static void main(String[] args) {
  202. Scanner scanner = new Scanner(System.in);
  203.  
  204. int num = Integer.parseInt(scanner.nextLine());
  205.  
  206. for (int i = 1; i <= 10; i++) {
  207. System.out.printf("%d X %d = %d%n", num, i, num * i);
  208. }
  209. }
  210. }
  211. ==================================================================================
  212. 11. Multiplication Table 2.0
  213.  
  214. public class P11MultiplicationTable2 {
  215. public static void main(String[] args) {
  216. Scanner scanner = new Scanner(System.in);
  217.  
  218. int num = Integer.parseInt(scanner.nextLine());
  219. int times = Integer.parseInt(scanner.nextLine());
  220.  
  221. if (times <= 10) {
  222. for (int i = times; i <= 10; i++) {
  223. System.out.printf("%d X %d = %d%n", num, i, num * i);
  224. }
  225. } else {
  226. System.out.printf("%d X %d = %d%n", num, times, num * times);
  227. }
  228. }
  229. }
  230. ==================================================================================
  231. 12. Even Number
  232.  
  233. public class P12EvenNumber {
  234. public static void main(String[] args) {
  235. Scanner scanner = new Scanner(System.in);
  236.  
  237. int num = Integer.parseInt(scanner.nextLine());
  238.  
  239. while (num % 2 != 0) {
  240. System.out.println("Please write an even number.");
  241. num = Integer.parseInt(scanner.nextLine());
  242.  
  243.  
  244. // if (num % 2 == 0) {
  245. // System.out.printf("The number is: %d", Math.abs(num));
  246. // break;
  247. // } else {
  248. // System.out.println("Please write an even number.");
  249. // num = Integer.parseInt(scanner.nextLine());
  250. // }
  251. }
  252.  
  253. System.out.printf("The number is: %d", Math.abs(num));
  254. }
  255. }
  256. ==================================================================================
  257. 13. Refactor Sum of Odd Numbers
  258.  
  259. public class P13RefactorSumOfOddNumbers {
  260. public static void main(String[] args) {
  261. Scanner scanner = new Scanner(System.in);
  262.  
  263. int n = Integer.parseInt(scanner.nextLine());
  264. int sum = 0;
  265. for (int i = 0; i < n; i++) {
  266. System.out.println(2 * i + 1);
  267. sum += 2 * i + 1;
  268. }
  269. System.out.printf("Sum: %d%n", sum);
  270.  
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement