Exercises from labs 2023-2024
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

128 строки
3.6 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 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. }