Exercises from labs 2023-2024
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

week23.java 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 String name;
  50. private int accountNumber;
  51. private double balance;
  52. private static int nextAccountNumber = 0;
  53. public BankAccount(String name) {
  54. this.name = name;
  55. this.balance = 0.0;
  56. this.accountNumber = nextAccountNumber;
  57. nextAccountNumber++;
  58. }
  59. @Override
  60. public String toString() {
  61. return "BankAccount [name=" + name + ", accountNumber=" + accountNumber + ", balance=" + balance + "]";
  62. }
  63. public int getAccountNumber() {
  64. return accountNumber;
  65. }
  66. public String getName() {
  67. return name;
  68. }
  69. public double getBalance() {
  70. return balance;
  71. }
  72. public void deposit (double amount) {
  73. this.balance += amount;
  74. }
  75. public void withdraw(double amount) {
  76. // No overdraft facilities here!
  77. if (amount <= balance) {
  78. this.balance -= amount;
  79. }
  80. }
  81. }
  82. /**
  83. * Exercise 2.4 & 2.6 - create two bank account objects and print their account
  84. * numbers, which should be 0 and 1.
  85. *
  86. * I forget the name for the implementation detail but a static member is shared across class objects / instances?
  87. */
  88. class DemoBankAccount {
  89. public static void main(String[] args) {
  90. BankAccount account1 = new BankAccount("Jerry");
  91. BankAccount account2 = new BankAccount("Margo");
  92. System.out.println(String.format("%s's account number: %d", account1.getName(), account1.getAccountNumber()));
  93. System.out.println(String.format("%s's account number: %d", account2.getName(), account2.getAccountNumber()));
  94. System.out.println(String.format("Jerry will deposit £10, Margo will deposit £25"));
  95. account1.deposit(10.00);
  96. account2.deposit(25.00);
  97. System.out.println(String.format("Accounts:\n\t%s\n\t%s", account1, account2));
  98. System.out.println(String.format("Jerry goes to the pub and needs some cash for that. Margo goes to buy new garden furniture."));
  99. account1.withdraw(5.00);
  100. account2.withdraw(15.00);
  101. System.out.println(String.format("Accounts:\n\t%s\n\t%s", account1, account2));
  102. }
  103. }