联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2019-10-27 02:49

Homework Assignment 2

Late homework assignments will not be accepted, unless you have a valid written excuse (medical, etc.). You must do

this assignment alone. No team work or "talking with your friends" will be accepted. No copying from the Internet.

Cheating means zero.

Create a new Eclipse workspace named "Assignment2_1234567890" on the desktop of your computer (replace

1234567890 with your student ID number). For each question below, create a new project in that workspace. Call

each project by its question number: "Question1", "Question2", etc. Answer all the questions below. At the end of

the assignment, create a ZIP archive of the whole workspace folder. The resulting ZIP file must be called

"Assignment2_1234567890.zip" (replace 1234567890 with your student ID number). Upload the ZIP file on

iSpace.

Question 1

Create four classes with the following UML diagrams:

+-----------------------------+

| BankAccount |

+-----------------------------+

| - money: int |

+-----------------------------+

| + BankAccount(int money) |

| + getMoney(): int |

| + setMoney(int money): void |

| + testBankAccount(): void |

+-----------------------------+

+------------------------------------------------+

| MobilePhone |

+------------------------------------------------+

| - number: int |

| - account: BankAccount |

+------------------------------------------------+

| + MobilePhone(int number, BankAccount account) |

| + getNumber(): int |

| + payMoney(int amount): boolean |

| + testMobilePhone(): void |

+------------------------------------------------+

A mobile phone has a phone number and is connected to a bank account. The owner of the mobile phone can use the

mobile phone to pay money: if amount is not negative and if the bank account connected to the mobile phone has

enough money in it then the money in the bank account is decreased by amount and the payMoney method must

return true, otherwise nothing changes for the bank account and the method must return false.

+-----------------------------+

| Chocolate |

+-----------------------------+

| - weight: double |

+-----------------------------+

| + Chocolate(double weight) |

| + getWeight(): double |

| + buy(int money): void |

| + eat(double amount): void |

| + testChocolate(): void |

+-----------------------------+

If the constructor of the Chocolate class is given a negative weight as argument then the weight must be changed to

0.0 Kg.

When buying chocolate, the weight of chocolate increases, with the price of chocolate being RMB 100 per 1.5 Kg. It is

not possible to buy a negative amount of chocolate, so in that case the buy method must print a message "Cannot

buy negative amount of chocolate" and nothing else happens.

It is not possible to eat more chocolate than there is chocolate, so in that case the eat method must print a message

"Cannot eat nonexistent chocolate, only XXX Kg available" (where XXX is replaced with the

current weight of chocolate) and nothing else happens.

+-----------------------------------+

| Student |

+-----------------------------------+

| - name: String |

| - phone: MobilePhone |

| - chocolate: Chocolate |

+-----------------------------------+

| + Student(String name, int money) |

| + getName(): String |

| + getChocolateWeight(): double |

| + hungry(int money): void |

| + testStudent(): void |

+-----------------------------------+

When a student is created, the student has 0.0 Kg of chocolate and a mobile phone which is connected to a bank

account with money in it. Use your student ID number as the phone number for the mobile phone.

When the student is hungry, the student first tries to use the mobile phone to pay the money amount. If the payment is

successful then the student buys the chocolate corresponding to the same money amount of the payment and then the

student eats half of the weight of chocolate that has just been bought (not half of the total weight of chocolate). If the

payment is not successful then the hungry method must print a message "Student is still hungry!" and

nothing else happens.

Each class has a static test method that contains tests for all the constructors and all the methods of the class. For each

class, test the simple methods first and the more complicated methods next. For each constructor and each method,

make sure that you test every possible case.

Add to your software a Start class with a main method that calls the test method of each of the four classes, to test

everything in your software.

Here are a few extra instructions:

? Give meaningful names to your variables so we can easily know what each variable is used for in your program.

? Put comments in your code (in English!) to explain WHAT your code is doing and also to explain HOW your

program is doing it.

? Make sure all your code is properly indented (formatted). Your code should be beautiful to read.

Failure to follow these instructions will result in you losing points.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp