Exercises from labs 2023-2024
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

299 wiersze
9.2 KiB

  1. import java.util.Scanner;
  2. class Person {
  3. private int age;
  4. private String name;
  5. public int getAge() {
  6. return age;
  7. }
  8. public void setAge(int age) {
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String toString() {
  18. return this.getName();
  19. }
  20. }
  21. class DemoPerson {
  22. public static void main(String[] args) {
  23. System.out.println(String.format("Would you like to meet a person?"));
  24. Person simon = new Person();
  25. simon.setAge(38);
  26. simon.setName("Simon");
  27. System.out.println(String.format("Their name is %s and their age is %d",
  28. simon.getName(), simon.getAge()));
  29. String newName = getUserInput("What shall we change their name to?");
  30. simon.setName(newName);
  31. System.out.println(String.format("Their name is now %s, but their age remains %d",
  32. simon, simon.getAge())); // uses toString()
  33. }
  34. /** A simple wrapper to get some user input easily
  35. * @param prompt What to prompt user for
  36. * @return User's input
  37. */
  38. public static String getUserInput(String prompt) {
  39. Scanner userInput = new Scanner(System.in);
  40. System.out.println(String.format("%s", prompt));
  41. if (userInput.hasNextLine()) {
  42. return userInput.nextLine();
  43. } else {
  44. return "";
  45. }
  46. }
  47. }
  48. class BankAccount {
  49. private Person person;
  50. private int accountNumber;
  51. private double balance;
  52. private static int nextAccountNumber = 0;
  53. public BankAccount(Person person) {
  54. this.person = person;
  55. this.balance = 0.0;
  56. this.accountNumber = nextAccountNumber;
  57. nextAccountNumber++;
  58. }
  59. @Override
  60. public String toString() {
  61. return "BankAccount [person=" + person + ", accountNumber=" + accountNumber + ", balance=" + balance + "]";
  62. }
  63. public Person getPerson() {
  64. return person;
  65. }
  66. public int getAccountNumber() {
  67. return accountNumber;
  68. }
  69. public double getBalance() {
  70. return balance;
  71. }
  72. public void setBalance(double balance) {
  73. this.balance = balance;
  74. }
  75. public void deposit (double amount) {
  76. this.balance += amount;
  77. }
  78. public void withdraw(double amount) {
  79. // No overdraft facilities here!
  80. // wait, reading ahead, there are...
  81. // if (amount <= balance) {
  82. this.balance -= amount;
  83. //}
  84. }
  85. /** Transfer funds out of the account to another {@link BankAccount}
  86. * @param amount Amount to transfer
  87. * @param recipient The lucky beneficiary
  88. */
  89. public void transferFunds(double amount, BankAccount recipient) {
  90. this.withdraw(amount);
  91. recipient.deposit(amount);
  92. }
  93. }
  94. /**
  95. * Exercise 2.4 & 2.6 - create two bank account objects and print their account
  96. * numbers, which should be 0 and 1.
  97. *
  98. * Then 2.8 & 2.9 where we change it to use Person
  99. */
  100. class DemoBankAccount {
  101. public static void main(String[] args) {
  102. Person jerry = new Person();
  103. jerry.setName("Jerry");
  104. Person margo = new Person();
  105. margo.setName("Margo");
  106. BankAccount account1 = new BankAccount(jerry);
  107. BankAccount account2 = new BankAccount(margo);
  108. System.out.println(String.format("%s's account number: %d", account1.getPerson(), account1.getAccountNumber()));
  109. System.out.println(String.format("%s's account number: %d", account2.getPerson(), account2.getAccountNumber()));
  110. System.out.println(String.format("Jerry will deposit £10, Margo will deposit £25"));
  111. account1.deposit(10.00);
  112. account2.deposit(25.00);
  113. System.out.println(String.format("Accounts:\n\t%s\n\t%s\n", account1, account2));
  114. System.out.println(String.format("Jerry goes to the pub and needs some cash for that. Margo goes to buy new garden furniture."));
  115. account1.withdraw(5.00);
  116. account2.withdraw(15.00);
  117. System.out.println(String.format("Accounts:\n\t%s\n\t%s", account1, account2));
  118. System.out.println(".".repeat(80));
  119. System.out.println(String.format("Now we're going to set up two accounts for the one person.\n"));
  120. Person neighbour = new Person();
  121. neighbour.setName("Tom");
  122. BankAccount account3 = new BankAccount(neighbour);
  123. BankAccount account4 = new BankAccount(neighbour);
  124. System.out.println(String.format("Accounts:\n\t%s\n\t%s\n", account3, account4));
  125. System.out.println(String.format("%s deposits some money in each.", neighbour));
  126. account3.deposit(12.50);
  127. account4.deposit(3.33);
  128. System.out.println(String.format("Actually wait, the account wasn't set up in %s's name...", neighbour));
  129. neighbour.setName("Barbara");
  130. System.out.println(String.format("Accounts:\n\t%s\n\t%s\n", account3, account4));
  131. System.out.println(".".repeat(80));
  132. System.out.println(String.format("To help %s buy a new goat, %s lends them some money.",
  133. neighbour, margo));
  134. account2.transferFunds(10.00, account3);
  135. System.out.println(String.format("Accounts:\n\t%s\n\t%s\n\t%s\n", account2, account3, account4));
  136. }
  137. }
  138. class Student extends Person {
  139. private double gpa;
  140. public double getGpa() {
  141. return gpa;
  142. }
  143. public void setGpa(double gpa) {
  144. this.gpa = gpa;
  145. }
  146. @Override
  147. public String toString() {
  148. return "Student [gpa=" + gpa + ", name=" + this.getName() + "]";
  149. // Ah, we can't access private members of parent classes from the child class
  150. }
  151. }
  152. class Lecturer extends Person {
  153. private double salary;
  154. public double getSalary() {
  155. return salary;
  156. }
  157. public void setSalary(double salary) {
  158. this.salary = salary;
  159. }
  160. @Override
  161. public String toString() {
  162. return "Lecturer [salary=" + salary + ", name=" + this.getName() + "]";
  163. }
  164. }
  165. /**
  166. * Exercise 3.1, 3.2, 3.3 - implement Student and Lcturer classes
  167. *
  168. */
  169. class DemoStudentLecturer {
  170. public static void main(String[] args) {
  171. Student adrian = new Student();
  172. adrian.setName("Adrian");
  173. adrian.setGpa(3.5);
  174. Lecturer trefusis = new Lecturer();
  175. trefusis.setName("Donald");
  176. trefusis.setSalary(24199);
  177. System.out.println(String.format("At a particular Oxbridge college, we have two people"));
  178. System.out.println(String.format("%s\t\t&\t\t%s", adrian, trefusis));
  179. }
  180. }
  181. class SavingsAccount extends BankAccount {
  182. @Override
  183. public String toString() {
  184. return "SavingsAccount [interestRate=" + interestRate + "]";
  185. }
  186. private double interestRate;
  187. /** Set up savings account for the thrifty
  188. * @param person A {@link Person} object
  189. * @param interestRate Expressed as %
  190. */
  191. public SavingsAccount(Person person, double interestRate) {
  192. super(person);
  193. this.interestRate = interestRate;
  194. }
  195. @Override
  196. public void withdraw(double amount) {
  197. if (amount <= this.getBalance()) {
  198. this.setBalance(this.getBalance() - amount);
  199. }
  200. }
  201. public void addInterest() {
  202. this.setBalance(this.getBalance() + (this.getBalance() * (this.interestRate / 100)));
  203. }
  204. }
  205. class DemoSavingsAccount {
  206. public static void main(String[] args) {
  207. Person hotblack = new Person();
  208. hotblack.setName("Hotblack Desiato");
  209. SavingsAccount savings1 = new SavingsAccount(hotblack, 6);
  210. savings1.deposit(100.00);
  211. System.out.println(String.format("%s plays a gig with DA and gets paid", hotblack));
  212. System.out.println(String.format("Account:\n\t%s", savings1));
  213. System.out.println(".".repeat(80));
  214. System.out.println(
  215. String.format("%s has a PGGB and tries to take out £200 to cover the cleanup expenses!", hotblack));
  216. savings1.withdraw(200.00);
  217. System.out.println(String.format("Account:\n\t%s", savings1));
  218. System.out.println(String.format("Fortunately (?), his bank blocks the transaction."));
  219. System.out.println(".".repeat(80));
  220. System.out.println(String.format("After 1 year, %s's account accrues interest!", hotblack));
  221. savings1.addInterest();
  222. System.out.println(String.format("Account:\n\t%s", savings1));
  223. System.out.println(String.format("%s wonders what it would be like after 1000 years...", hotblack));
  224. System.out.println(".".repeat(80));
  225. for (int i=0; i < 999; i++) {
  226. savings1.addInterest();
  227. if (i % 20 == 0) {
  228. System.out.printf("%.2f\t", savings1.getBalance());
  229. }
  230. }
  231. System.out.println(String.format("...finally, after 1000 years spent dead for tax purposes, %s checks his account", hotblack));
  232. System.out.println(String.format("Account:\n\t%s", savings1));
  233. }
  234. }
  235. class PolymorphismTest {
  236. public static void main(String[] args) {
  237. Person telemachus = new Person();
  238. telemachus.setName("Τηλέμαχος");
  239. BankAccount b = new SavingsAccount(telemachus, 10);
  240. System.out.println(String.format("%s", b));
  241. }
  242. }