Advertisement
programfund

Programming revision

Jul 16th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. %% revision part 1
  2. public class ResistorCodes {
  3.  
  4. public static void main(String[] args) {
  5. ResistorCodes p1 = new ResistorCodes();
  6.  
  7. System.out.println(p1.ResistorValue("red","red","orange") + " ohms");
  8. System.out.println(p1.ResistorValue("violet","green","yellow") + " ohms");
  9. System.out.println(p1.ResistorValue("black","brown","brown") + " ohms");
  10.  
  11.  
  12. }
  13.  
  14.  
  15.  
  16. private int ResistorValue(String col1, String col2, String col3) {
  17.  
  18. int total = 0;
  19. total = (int) ((getVal(col1)*10 + getVal(col2))*Math.pow(10, getVal(col3)));
  20. return total;
  21.  
  22.  
  23. }
  24.  
  25. private int getVal(String colour) {
  26. if (colour == "black")
  27. return 0;
  28. else if (colour == "brown")
  29. return 1;
  30. else if (colour == "red")
  31. return 2;
  32. else if (colour == "orange")
  33. return 3;
  34. else if (colour == "yellow")
  35. return 4;
  36. else if (colour == "green")
  37. return 5;
  38. else if (colour == "blue")
  39. return 6;
  40. else if (colour == "violet")
  41. return 7;
  42. else if (colour == "grey")
  43. return 8;
  44. else if (colour == "white")
  45. return 9;
  46. else
  47. return 0;
  48.  
  49. }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. %% revision part 2
  56. public class Chess {
  57.  
  58. public static void main(String[] args) {
  59. Chess c1 = new Castle(1,2);
  60. System.out.println(c1);
  61. Chess c2 = new Knight(3,4);
  62. System.out.println(c2);
  63.  
  64. }
  65.  
  66. }
  67.  
  68. class Castle extends Chess {
  69.  
  70. private int x;
  71. private int y;
  72.  
  73. @Override
  74. public String toString() {
  75. return "Castle at position: " + x + "," + y;
  76. }
  77.  
  78. public Castle(int i, int j) {
  79. this.x = i;
  80. this.y = j;
  81. }
  82.  
  83. }
  84.  
  85. class Knight extends Chess {
  86. private int x;
  87. private int y;
  88.  
  89. @Override
  90. public String toString() {
  91. return "Knight at position: " + x + "," + y;
  92. }
  93.  
  94. public Knight(int i, int j) {
  95. this.x = i;
  96. this.y = j;
  97. }
  98.  
  99. }
  100.  
  101. %% revision part 3
  102. import java.io.FileInputStream;
  103. import java.io.FileNotFoundException;
  104. import java.util.Scanner;
  105.  
  106. public class stringToInt {
  107.  
  108. public static void main(String[] args) throws FileNotFoundException {
  109. stringToInt st1 = new stringToInt();
  110. st1.runDemo();
  111.  
  112.  
  113. }
  114.  
  115. private void runDemo() throws FileNotFoundException {
  116. int result = 0;
  117. Scanner sc = new Scanner(new FileInputStream("file.txt"));
  118. while (sc.hasNextLine()){
  119. String val1 = sc.nextLine();
  120. String val2 = sc.nextLine();
  121. String op = sc.nextLine();
  122. char oper = op.charAt(0);
  123.  
  124. if(oper == '+'){
  125. result = Integer.parseInt(val1) + Integer.parseInt(val2);
  126. System.out.println(result);
  127. }
  128.  
  129. else if(oper == '-'){
  130. result = Integer.parseInt(val1) - Integer.parseInt(val2);
  131. System.out.println(result);
  132. }
  133.  
  134. else if(oper == '*'){
  135. result = Integer.parseInt(val1) * Integer.parseInt(val2);
  136. System.out.println(result);
  137. }
  138.  
  139. else if(oper == '/'){
  140. result = Integer.parseInt(val1) / Integer.parseInt(val2);
  141. System.out.println(result);
  142. }
  143.  
  144. }
  145. sc.close();
  146.  
  147.  
  148. }
  149.  
  150. }
  151.  
  152.  
  153. %%revision part 4
  154.  
  155. import java.text.DecimalFormat;
  156.  
  157. public class Change {
  158.  
  159. public static void main(String[] args) {
  160. Change cg = new Change();
  161. cg.changeBack(350);
  162. cg.changeBack(510);
  163. cg.changeBack(125);
  164.  
  165. }
  166.  
  167.  
  168.  
  169. private void changeBack(double cost) {
  170. try{
  171. if (cost > 500)
  172. throw new NotEnoughMoneyException();
  173. else if (cost <= 500){
  174. double money = 5.00;
  175. double decimalCost = cost / 100.0;
  176. double change = money - decimalCost;
  177. DecimalFormat dm = new DecimalFormat("0.00");
  178.  
  179. System.out.println("You bought an item for $" + dm.format(decimalCost));
  180. System.out.println("Your change is $" + dm.format(change) + ":");
  181.  
  182. int count = 0;
  183. if (change >= 2){
  184. count = (int) (change/2);
  185. change = change - 2*count;
  186. }
  187. System.out.println(count + " two dollar coins");
  188.  
  189.  
  190. count = 0;
  191. if (change >= 1){
  192. count = (int) (change/1);
  193. change = change - 1*count;
  194. }
  195. System.out.println(count + " one dollar coins");
  196.  
  197.  
  198.  
  199. count = 0;
  200. if (change >= 0.5){
  201. count = (int) (change/0.5);
  202. change = change - 0.5*count;
  203. }
  204. System.out.println(count + " fifty cent coins");
  205.  
  206.  
  207.  
  208. count = 0;
  209. if (change >= 0.2){
  210. count = (int) (change/0.2);
  211. change = change - 0.2*count;
  212. }
  213. System.out.println(count + " twenty cent coins");
  214.  
  215.  
  216.  
  217. count = 0;
  218. if (change >= 0.1){
  219. count = (int) (change/0.1);
  220. change = change - 0.1*count;
  221. }
  222. System.out.println(count + " ten cent coins");
  223.  
  224.  
  225.  
  226. change = Double.parseDouble(dm.format(change));
  227.  
  228. count = 0;
  229. if (change >= 0.05){
  230. count = (int) (change/0.05);
  231. change = change - 0.05*count;
  232. }
  233. System.out.println(count + " five cent coins");
  234.  
  235. System.out.println();
  236. }
  237. }
  238. catch (NotEnoughMoneyException e){
  239. System.out.println(e.getMessage());
  240.  
  241. }
  242.  
  243. }
  244.  
  245. }
  246.  
  247. class NotEnoughMoneyException extends Exception {
  248.  
  249. /**
  250. *
  251. */
  252. private static final long serialVersionUID = 4257723934124110055L;
  253.  
  254.  
  255. public NotEnoughMoneyException()
  256. {
  257. super("You Do Not Have Enough Money!\n");
  258. }
  259. public NotEnoughMoneyException(String message)
  260. {
  261. super(message);
  262. }
  263.  
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement