Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
  2. _________________
  3.  
  4. // Geek.java
  5.  
  6. public class Geek {
  7. private String name;
  8. private int cntAnswered;
  9. public Geek(String name) {
  10. this.name = name;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public int getNumberOfQuestions()
  16. {
  17. return cntAnswered;
  18. }
  19. public boolean isEven(int num)
  20. {
  21. cntAnswered++;
  22. if(num%2==0)
  23. {
  24. return true;
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30.  
  31. }
  32.  
  33. public String multiConcat(String test,int count)
  34. {
  35. cntAnswered++;
  36. String newStr="";
  37. for(int i=0;i<count;i++)
  38. {
  39. newStr+=test;
  40. }
  41. return newStr;
  42. }
  43. public int sumRange(int num1,int num2)
  44. {
  45. cntAnswered++;
  46. int sum=0;
  47. if(num1==num2)
  48. {
  49. sum=0;
  50. }
  51. else if(num1>num2)
  52. {
  53. sum=0;
  54. }
  55. else
  56. {
  57. for(int i=(num1+1);i<num2;i++)
  58. {
  59. sum+=i;
  60. }
  61. }
  62. return sum;
  63. }
  64. public boolean isSorted(int num1,int num2,int num3)
  65. {
  66. cntAnswered++;
  67. if(num1<num2 && num1<num2 && num2<num3)
  68. {
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. public int countA(String test)
  77. {
  78. cntAnswered++;
  79. int cnt=0;
  80. for(int i=0;i<test.length();i++)
  81. {
  82. if(test.charAt(i)=='a' || test.charAt(i)=='A')
  83. {
  84. cnt++;
  85. }
  86. }
  87. return cnt;
  88. }
  89. public boolean isPrime(int n)
  90. {
  91. cntAnswered++;
  92. // If the user entered number is '2' return true
  93. if (n == 2)
  94. return true;
  95. for (int i = 2; i * i <= n; i++) {
  96. if (n % i == 0)
  97. return false;
  98. }
  99. return true;
  100.  
  101. }
  102. public int countDigits(int num)
  103. {
  104. cntAnswered++;
  105. int t = num;
  106. int remainder,cnt=0;
  107.  
  108. while (t != 0)
  109. {
  110. remainder = t % 10;
  111. cnt++;
  112. t = t / 10;
  113. }
  114. return cnt;
  115. }
  116.  
  117. }
  118.  
  119. ___________________________
  120.  
  121. // Test.java
  122.  
  123. import java.util.Scanner;
  124.  
  125. public class Test {
  126.  
  127. public static void main(String[] args) {
  128. char choice;
  129. /*
  130. * Creating an Scanner class object which is used to get the inputs
  131. * entered by the user
  132. */
  133. Scanner sc = new Scanner(System.in);
  134. Geek g=null;
  135. while(true)
  136. {
  137. //Getting the input entered by the user
  138. System.out.println("Command Options");
  139. System.out.println("---------------------------");
  140. System.out.println("a: Get name");
  141. System.out.println("b : Num of questions asked");
  142. System.out.println("c : Is it even");
  143. System.out.println("d : Multi Concat");
  144. System.out.println("e : Range between two integers");
  145. System.out.println("f : isSorted");
  146. System.out.println("g : count a");
  147. System.out.println("h : count digits");
  148. System.out.println("i : is it prime");
  149. System.out.println("? : Display");
  150. System.out.println("q : quit");
  151. System.out.println("Please enter a command or type ?");
  152. choice=sc.next(".").charAt(0);
  153.  
  154. switch(choice)
  155. {
  156. case 'a':
  157. case 'A':
  158. {
  159. sc.nextLine();
  160. String name=sc.nextLine();
  161. g=new Geek(name);
  162. continue;
  163. }
  164. case 'b':
  165. case 'B':
  166. {
  167. System.out.println(g.getNumberOfQuestions());
  168. continue;
  169. }
  170. case 'c':
  171. case 'C':
  172. {
  173. System.out.print("Enter a number :");
  174. int num=sc.nextInt();
  175. boolean b=g.isEven(num);
  176. if(b)
  177. {
  178. System.out.println(num+" is even.");
  179. }
  180. else
  181. {
  182. System.out.println(num+" is not even.");
  183. }
  184. continue;
  185. }
  186. case 'd':
  187. case 'D':
  188. {
  189. String str;
  190. int cnt;
  191. System.out.print("Enter a String :");
  192. str=sc.next();
  193. System.out.print("Enter an integer :");
  194. cnt=sc.nextInt();
  195. System.out.println(g.multiConcat(str,cnt));
  196. continue;
  197. }
  198. case 'e':
  199. case 'E':
  200. {
  201. int first,second;
  202. System.out.print("Enter the first number :");
  203. first=sc.nextInt();
  204. System.out.print("Enter the second number :");
  205. second=sc.nextInt();
  206.  
  207. System.out.println("Sum of integers of "+first+" and "+second+" is "+g.sumRange(first,second));
  208.  
  209.  
  210. continue;
  211. }
  212. case 'f':
  213. case 'F':
  214. {
  215. int a,b,c;
  216. System.out.print("Enter three number :");
  217. a=sc.nextInt();
  218. b=sc.nextInt();
  219. c=sc.nextInt();
  220. boolean bool=g.isSorted(a,b,c);
  221. if(bool)
  222. {
  223. System.out.println("Sorted!");
  224. }
  225. else
  226. {
  227. System.out.println("Not Sorted!");
  228. }
  229. continue;
  230. }
  231. case 'g':
  232. case 'G':
  233. {
  234. String str;
  235. System.out.print("Enter a string :");
  236. str=sc.next();
  237. System.out.println("The String \""+str+"\" has "+g.countA(str)+" a(s)");
  238.  
  239. continue;
  240. }
  241. case 'h':
  242. case 'H':
  243. {
  244. System.out.print("Enter an integer :");;
  245. int num=sc.nextInt();
  246. System.out.println("Number of digits in "+num+" is "+g.countDigits(num));
  247. continue;
  248. }
  249. case 'i':
  250. case 'I':
  251. {
  252. int num;
  253. System.out.print("Enter an number :");;
  254. num=sc.nextInt();
  255. System.out.println(num+" is not prime.");
  256. continue;
  257. }
  258. case '?':
  259. {
  260. continue;
  261. }
  262. case 'q':
  263. case 'Q':
  264. {
  265. break;
  266. }
  267.  
  268. default:{
  269.  
  270. }
  271.  
  272. }
  273.  
  274. break;
  275. }
  276.  
  277.  
  278.  
  279.  
  280.  
  281. }
  282.  
  283. }
  284.  
  285. _____________________________
  286.  
  287. Output:
  288.  
  289. Command Options
  290. ---------------------------
  291. a: Get name
  292. b : Num of questions asked
  293. c : Is it even
  294. d : Multi Concat
  295. e : Range between two integers
  296. f : isSorted
  297. g : count a
  298. h : count digits
  299. i : is it prime
  300. ? : Display
  301. q : quit
  302. Please enter a command or type ?
  303. a
  304. Williams
  305. Command Options
  306. ---------------------------
  307. a: Get name
  308. b : Num of questions asked
  309. c : Is it even
  310. d : Multi Concat
  311. e : Range between two integers
  312. f : isSorted
  313. g : count a
  314. h : count digits
  315. i : is it prime
  316. ? : Display
  317. q : quit
  318. Please enter a command or type ?
  319. b
  320. 0
  321. Command Options
  322. ---------------------------
  323. a: Get name
  324. b : Num of questions asked
  325. c : Is it even
  326. d : Multi Concat
  327. e : Range between two integers
  328. f : isSorted
  329. g : count a
  330. h : count digits
  331. i : is it prime
  332. ? : Display
  333. q : quit
  334. Please enter a command or type ?
  335. c
  336. Enter a number :67
  337. 67 is not even.
  338. Command Options
  339. ---------------------------
  340. a: Get name
  341. b : Num of questions asked
  342. c : Is it even
  343. d : Multi Concat
  344. e : Range between two integers
  345. f : isSorted
  346. g : count a
  347. h : count digits
  348. i : is it prime
  349. ? : Display
  350. q : quit
  351. Please enter a command or type ?
  352. d
  353. Enter a String :Fun
  354. Enter an integer :4
  355. FunFunFunFun
  356. Command Options
  357. ---------------------------
  358. a: Get name
  359. b : Num of questions asked
  360. c : Is it even
  361. d : Multi Concat
  362. e : Range between two integers
  363. f : isSorted
  364. g : count a
  365. h : count digits
  366. i : is it prime
  367. ? : Display
  368. q : quit
  369. Please enter a command or type ?
  370. q
  371.  
  372.  
  373. _______________Could you plz rate me well.Thank You
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement