Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. /**
  2. *
  3. * @author Melanie
  4. * This is the LabExample class - used to explore if and loop statements
  5. *
  6. */
  7.  
  8. public class LabExample {
  9.  
  10.  
  11. public int highestOfTwo(int a,int b){
  12. if (a>b) {
  13. return a;
  14. }else if(a<b) {
  15. return b;
  16. }else{
  17. return -1;
  18. }
  19. }
  20.  
  21.  
  22.  
  23. public String calculateGrade(int score) {
  24. if (score>=0 && score<40)
  25. {
  26. return "Fail";
  27. }else if(score>=40 && score<50){
  28. return "3rd";
  29. }else if(score>=50 && score <60) {
  30. return "2ii";
  31. }else if(score>=60 && score<70) {
  32. return "2i";
  33. }else if(score>=70 && score<=100) {
  34. return "1st";
  35. }else {
  36. return "Invalid mark";
  37. }
  38.  
  39. }
  40.  
  41.  
  42.  
  43. public String headsOrTails(String guess) {
  44. String answer="";
  45. int random=(int)(Math.random() * ((1 - 0) + 1)) + 0;
  46. if (random==1) {
  47. answer="heads";
  48. }else {
  49. answer="tails";
  50. }
  51. if (guess.equals(answer)){
  52. return "Correct: you guessed "+guess+" and I flipped "+answer+"";
  53. }else {
  54. return "Incorrect: you guessed "+guess+" and I flipped "+answer+"";
  55.  
  56. }
  57.  
  58. }
  59.  
  60.  
  61. public int sumFromOneToWhat(int top) {
  62. top=(top*(top+1))/2;
  63. return top;
  64. }
  65.  
  66.  
  67.  
  68. public int sumFromWhatToWhat(int bottom,int top) {
  69. int sum=0;
  70. for(int i=bottom;i<=top;i++){
  71. sum+=i;
  72. }
  73. return sum;
  74. }
  75.  
  76. }
  77. /////////////////////////////////////////////
  78. /**
  79. *
  80. * @author Melanie
  81. * This is the Second Salary class - used to explore if and loop statements
  82. *
  83. */
  84.  
  85.  
  86. public class Salary {
  87.  
  88.  
  89. public double salaryTax(double salary) {
  90. if(salary>=45000) {
  91. return salary*0.5;
  92. }else if(salary>=30000 && salary<45000){
  93. return salary*0.7;
  94. }else {
  95. return salary*0.85;
  96. }
  97.  
  98. }
  99.  
  100.  
  101. public double calculateNI(double gross,char NI) {
  102. switch(NI) {
  103. case 'A':
  104. gross=gross*0.88;
  105. break;
  106. case 'B':
  107. gross=gross*0.9415;
  108. break;
  109. case 'C':
  110. gross=gross*0.98;
  111. break;
  112. case 'a':
  113. gross=gross*0.88;
  114. break;
  115. case 'b':
  116. gross=gross*0.9415;
  117. break;
  118. case 'c':
  119. gross=gross*0.98;
  120. break;
  121. default:
  122. gross=gross;
  123.  
  124.  
  125. }
  126. return gross;
  127. }
  128.  
  129.  
  130.  
  131. public double salaryTotal(double salries[]) {
  132. double sum=0;
  133. for(double i : salries) {
  134. sum+=i;
  135. }
  136. return sum;
  137. }
  138.  
  139.  
  140.  
  141. public double salaryAverage(double salries[]) {
  142. double avr=0;
  143. for(double i : salries) {
  144. avr+=i;
  145. }
  146. return avr/salries.length;
  147. }
  148.  
  149.  
  150.  
  151. public double[] salaryIncrease(double salaries[]) {
  152. for(int i=0;i<salaries.length;i++) {
  153. salaries[i]=salaries[i]+salaries[i]*0.05;
  154. }
  155. return salaries;
  156. }
  157.  
  158.  
  159. }
  160. ////////////////////////////////////////////////////
  161. /**
  162. *
  163. * @author Melanie
  164. * This is the WordPlay class - used to explore if and loop statements
  165. *
  166. */
  167.  
  168. public class WordPlay {
  169.  
  170. public String whatComesFirst(String an1,String an2) {
  171. int ans=an1.compareToIgnoreCase(an2);
  172. String answer="";
  173. if (ans==0) {
  174. answer=""+an1+" is the same as "+an2+"";
  175. }else if (ans>0) {
  176. answer=""+an2+" comes before "+an1+" in the alphabet";
  177. }else if(ans<0) {
  178. answer=""+an1+" comes before "+an2+" in the alphabet";
  179. }
  180. return answer;
  181. }
  182.  
  183. public String backwardsString(String name) {
  184. StringBuilder input1 = new StringBuilder();
  185.  
  186. // append a string into StringBuilder input1
  187. input1.append(name);
  188.  
  189. // reverse StringBuilder input1
  190. input1 = input1.reverse();
  191. return input1.toString();
  192. }
  193.  
  194. public String[] addressBook(String names[],String numbers[]) {
  195. for(int i=0;i<names.length;i++) {
  196. names[i]=names[i]+" "+numbers[i];
  197. }
  198. return names;
  199. }
  200.  
  201. public String rockPaperScissors(String player) {
  202. String CPU="";
  203. int random=(int)(Math.random() * ((2 - 0) + 1)) + 0;
  204. if(random==0) {
  205. CPU="rock";
  206. }else if(random==1) {
  207. CPU="paper";
  208. }else if(random==2) {
  209. CPU="scissors";
  210. }
  211. int temp=CPU.compareToIgnoreCase(player);
  212. if(temp==0) {
  213. CPU="USER:"+player+" vs COMP:"+CPU+" it is a draw";
  214. }
  215. if(temp<0) {
  216. CPU="USER:"+player+" vs COMP:"+CPU+" COMPUTER wins";
  217. }
  218. if(temp>0) {
  219. CPU="USER:"+player+" vs COMP:"+CPU+" user wins";
  220. }
  221. System.out.println(CPU);
  222. return CPU;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement