Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. 4.18
  2. import java.util.Scanner;
  3. public class LabProgram
  4. {
  5. public static void main(String[] args)
  6. {
  7.  
  8. //Create the scanner class
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. //Declare a variable userString of type string.
  12. String userString;
  13.  
  14. //Declare a boolean variable result.
  15. boolean result = true;
  16.  
  17.  
  18. //Take the input from the user.
  19.  
  20. userString = sc.next();
  21.  
  22. //converts strings to sequence of characters.
  23. for(char ch: userString.toCharArray())
  24. {
  25.  
  26. //check whether the specified character is a digit.
  27. if(!Character.isDigit(ch))
  28. {
  29. result = false;
  30. }
  31. }
  32.  
  33. if(result)
  34. {
  35. System.out.println("yes");
  36. }
  37. else
  38. {
  39. System.out.println("no");
  40. }
  41. }
  42. }
  43. 4.19
  44. import java.util.Scanner;
  45.  
  46. public class LabProgram {
  47.  
  48. public static void main(String[] args) {
  49. Scanner in = new Scanner(System.in);
  50. String password = in.nextLine();
  51. String modified = "";
  52. for(int i = 0; i < password.length(); ++i) {
  53. if(password.charAt(i) == 'i') {
  54. modified += "!";
  55. } else if(password.charAt(i) == 'a') {
  56. modified += "@";
  57. } else if(password.charAt(i) == 'm') {
  58. modified += "M";
  59. } else if(password.charAt(i) == 'B') {
  60. modified += "8";
  61. } else if(password.charAt(i) == 'o') {
  62. modified += ".";
  63. } else {
  64. modified += password.charAt(i);
  65. }
  66. }
  67. modified += "q*s";
  68. System.out.println(modified);
  69. }
  70.  
  71. }
  72. 4.20
  73. import java.util.Scanner;
  74.  
  75. public class LabProgram {
  76.  
  77. public static String RemoveSpaces (String userString) {
  78. String result = "";
  79. for(int i = 0; i < userString.length(); ++i) {
  80. if(userString.charAt(i) != ' ') {
  81. result += userString.charAt(i);
  82. }
  83. }
  84. return result;
  85. }
  86.  
  87. public static void main(String[] args) {
  88. Scanner in = new Scanner(System.in);
  89. System.out.println(RemoveSpaces(in.nextLine()));
  90. }
  91.  
  92. }
  93. 4.21
  94. import java.util.Scanner;
  95.  
  96. public class LabProgram {
  97.  
  98. public static String RemoveSpaces (String userString) {
  99. String result = "";
  100. for(int i = 0; i < userString.length(); ++i) {
  101. if(userString.charAt(i) != ' ') {
  102. result += userString.charAt(i);
  103. }
  104. }
  105. return result;
  106. }
  107.  
  108. public static void main(String[] args) {
  109. Scanner in = new Scanner(System.in);
  110. System.out.println(RemoveSpaces(in.nextLine()));
  111. }
  112.  
  113. }
  114. 4.22
  115. import java.util.Scanner;
  116. public class LabProgram
  117. {
  118. public static void main(String[] args)
  119. {
  120. Scanner keyboard = new Scanner(System.in);
  121.  
  122.  
  123. int n = keyboard.nextInt();
  124.  
  125.  
  126. if(n < 20 || n > 98)
  127. {
  128. System.out.print("Input must be 20-98");
  129. System.out.println("");
  130. }
  131. else
  132. {
  133. System.out.print(n);
  134. while(n % 11 != 0)
  135. {
  136. n = n - 1;
  137. System.out.print(" " + n);
  138. }
  139. System.out.println(" ");
  140. }
  141.  
  142. keyboard.close();
  143. }
  144.  
  145. }
  146. 4.23
  147. import java.util.Scanner;
  148.  
  149. public class LabProgram {
  150.  
  151. public static void main(String[] args) {
  152. Scanner in = new Scanner(System.in);
  153. int start = in.nextInt(), end = in.nextInt();
  154. if (start > end) {
  155. System.out.println("Second integer can't be less than the first.");
  156. } else {
  157. while (start <= end) {
  158. System.out.print(start + " ");
  159. start += 10;
  160. }
  161. System.out.println();
  162. }
  163. }
  164. }
  165. 4.24
  166. import java.util.Scanner;
  167.  
  168. public class LabProgram {
  169.  
  170. public static void main(String[] args) {
  171. Scanner in = new Scanner(System.in);
  172. String s;
  173. while (true) {
  174. s = in.nextLine();
  175. if (s.equalsIgnoreCase("quit") || s.equalsIgnoreCase("q")) {
  176. break;
  177. }
  178. for (int i = s.length()-1; i >= 0; --i) {
  179. System.out.print(s.charAt(i));
  180. }
  181. System.out.println();
  182. }
  183. }
  184. }
  185. 4.25
  186. import java.util.Scanner;
  187. public class LabProgram {
  188. public static void main(String args[]){
  189. Scanner scanner = new Scanner(System.in);
  190.  
  191. String s = scanner.nextLine();
  192.  
  193. boolean flag = true;
  194.  
  195. // i is the starting index of the string s
  196. // j is the ending index of the string s
  197. int i = 0, j = s.length()-1;
  198.  
  199. // Checking the value if character at index i with the character at index j
  200. // If characters match, then move i to its next position and j t its previous position
  201. // If characters does not match then the string is not a palindrome.
  202. // In the case of does not match update the value of flag to false and break the loop
  203. while(i<j){
  204. if(s.charAt(i)==' '){
  205. i++;
  206. }
  207. else if(s.charAt(j)==' '){
  208. j--;
  209. }
  210. else {
  211. if(s.charAt(i)!=s.charAt(j)){
  212. flag = false;
  213. break;
  214. }
  215. i++;
  216. j--;
  217. }
  218. }
  219.  
  220. if (flag) {
  221. System.out.println(s+" is a palindrome");
  222. } else {
  223. System.out.println(s+" is not a palindrome");
  224. }
  225. }
  226. }
  227. 4.26
  228. import java.util.Scanner;
  229.  
  230. public class LabProgram {
  231.  
  232. public static void main(String[] args) {
  233. Scanner in = new Scanner(System.in);
  234. int x1, y1, n1, x2, y2, n2;
  235. x1 = in.nextInt();
  236. y1 = in.nextInt();
  237. n1 = in.nextInt();
  238.  
  239. x2 = in.nextInt();
  240. y2 = in.nextInt();
  241. n2 = in.nextInt();
  242.  
  243. for (int x = -10; x <= 10; x++) {
  244. for (int y = -10; y <= 10; y++) {
  245. if (x1*x + y1*y == n1 && x2*x + y2*y == n2) {
  246. System.out.println(x + " " + y);
  247. return;
  248. }
  249. }
  250. }
  251. System.out.println("No solution");
  252. }
  253. }
  254. 4.27
  255. import java.util.Scanner;
  256.  
  257. //declare class DrawRightTriangle
  258. public class DrawRightTriangle
  259. {
  260. //main method
  261. public static void main(String[] args)
  262. {
  263.  
  264. //Creating a scanner object to take the input from the user
  265. Scanner scnr = new Scanner(System.in);
  266. //Declaring variable to store the input
  267. char triangleChar;
  268. int triangleHeight;
  269.  
  270. //Prompting for a character that a user wants to print on the screen
  271. System.out.println("Enter a character:");
  272. triangleChar = scnr.next().charAt(0);
  273.  
  274. //Prompting for the height of the
  275. //triangle that a user wants to print
  276. System.out.println("Enter triangle height:");
  277. triangleHeight = scnr.nextInt();
  278. System.out.println("");
  279.  
  280. //Use nested for-loops to print the right angle traingle
  281. ///for the user specified character
  282. for(int i=0; i<triangleHeight; i++)
  283. {
  284. for(int j=0; j<=i; j++)
  285. {
  286. //This prints on the same line
  287. System.out.printf("%s ",triangleChar);
  288. }
  289. //print the newline for each row
  290. System.out.println();
  291. }
  292. }
  293. }
  294. 4.28
  295. import java.util.Scanner;
  296. public class DrawHalfArrow {
  297. public static void main(String[] args) {
  298. Scanner scnr = new Scanner(System.in);
  299. int arrowBaseHeight = 0;
  300. int arrowBaseWidth = 0;
  301. int arrowHeadWidth = 0;
  302.  
  303. System.out.println("Enter arrow base height:");
  304. arrowBaseHeight = scnr.nextInt();
  305.  
  306. System.out.println("Enter arrow base width:");
  307. arrowBaseWidth = scnr.nextInt();
  308.  
  309. System.out.println("Enter arrow head width:");
  310. arrowHeadWidth = scnr.nextInt();
  311.  
  312. while (arrowHeadWidth<=arrowBaseWidth) {
  313. System.out.println("arrow head width is not larger than arrow base width");
  314. System.out.println("Enter arrow head width:");
  315. arrowHeadWidth = scnr.nextInt();
  316. }
  317. System.out.println();
  318.  
  319. StringBuilder row = new StringBuilder();
  320. for (int x = 1; x <= arrowBaseWidth; x++) {
  321. row.append("*");
  322. }
  323. for (int i = 1; i <= arrowBaseHeight; i++) {
  324. System.out.println(row);
  325. }
  326.  
  327. int tmp = arrowHeadWidth;
  328. for (int y = 1; y <= arrowHeadWidth; y++) {
  329. for (int z = tmp; z > 0; z--) {
  330. System.out.print("*");
  331. }
  332. tmp -= 1;
  333. System.out.println();
  334. }
  335. }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement