In: Computer Science
This the question about the java and please show the detail comment and code of each class.Thank you.
And the "+" means "public" the "-" means "private" and testBankAccount(): void testMobilePhone(): void testChocolate(): void all are static
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.
Start.java
===========
package student;
public class Start {
// main method to run the program
public static void main(String[] args) {
// testing BankAccount class
BankAccount.testBankAccount();
// testing MobilePhone class
MobilePhone.testMobilePhone();
// testing Chocolate class
Chocolate.testChocolate();
// testing Student class
Student.testStudent();
}
}
============
BankAccount.java
============
package student;
public class BankAccount {
// member variables
private int money; // current amount of money in the
bank account
// constructor
public BankAccount(int money) {
// set bank account money to the
given amount
this.money = money;
}
// getter method
// returns current amount of money left in the
account
public int getMoney() {
return money;
}
// setter method
// reset money in the account to given amount
public void setMoney(int money) {
this.money = money;
}
// static method to test the BankAccount class
public static void testBankAccount() {
// print message for each method
tested
System.out.println("Testing
BankAccount:");
int amount = 200;
System.out.println("Testing
constructor with parameter: amount=" + amount);
BankAccount ba = new
BankAccount(amount);
System.out.println("getMoney()
returns: " + ba.getMoney());
amount = 400;
System.out.println("Testing
seMoney() with parameter: amount=" + amount);
ba.setMoney(amount);
System.out.println("getMoney()
returns: " + ba.getMoney());
System.out.println("finish testing
BankAccounut");
System.out.println();
}
}
=============
MobilePhone.java
=============
package student;
public class MobilePhone {
// member variables
private int number; // phone number
private BankAccount account; // mobile phone is
connected to this Bank Account
// constructor
public MobilePhone(int number, BankAccount account)
{
// initialize member variables to
the given data
this.number = number;
this.account = account;
}
// getter method
// returns phone number of the mobile phone
public int getNumber() {
return number;
}
// use the mobile phone to pay money
// returns true if payment is done otherwise return
false
// payment is done when given amount is not negative
and bank account
// has enough money
public boolean payMoney(int amount) {
// check if amount is
negative
if(amount < 0) {
return
false;
}
else {
// amount is not
negative
// now check if
bank account has enough money to pay the bill
if(account.getMoney()<amount) {
return false;
}
else {
// account has enough money
// decrease money from bank account
account.setMoney(account.getMoney()-amount);
return true;
}
}
}
// static method to test Mobile Phone class
public static void testMobilePhone() {
// print each method call
System.out.println("Testing
MobilePhone class:");
int number = 958476235;
int amount = 200;
System.out.println("Creating
BankAccount ac with amount: " + amount);
BankAccount ac = new
BankAccount(amount);
System.out.println("Testing
constructor with parameters: number=" + number + ", account
ac");
MobilePhone mphone = new
MobilePhone(number, ac);
System.out.println("money left in
account: " + ac.getMoney());
System.out.println("getNumber()
returns: " + mphone.getNumber());
System.out.println("payMoney(150)
returns: " + mphone.payMoney(150));
System.out.println("money left in
account: " + ac.getMoney());
System.out.println("payMoney(150)
returns: " + mphone.payMoney(150));
System.out.println("finish testing
MobilePhone");
System.out.println();
}
}
===========
Chocolate.java
===========
package student;
public class Chocolate {
// member variable
private double weight; // available chocolate
weight
// constructor
public Chocolate(double weight) {
// check if given weight is
negative
if(weight < 0.0) {
// set weight to
0.0
this.weight =
0.0;
}
else {
// initialize
member variable to the given amount
this.weight =
weight;
}
}
// getter method
// returns current weight of chocolate
public double getWeigth() {
return weight;
}
// buying chocolate increase the weight of
chocolate
// takes money as parameter and increase weight of
chocolate by 1.5 for each 100 money
public void buy(int money) {
// check if money is negative
if(money < 0) {
// print error
message
System.out.println("Cannot buy negative amount of
chocolate");
}
else {
// calculate the
weight of chocolate that is bought
double
bought_chocolate = (1.5*money)/100.0;
// add the
bought amount in to the current chocolate weight
weight = weight
+ bought_chocolate;
}
}
// eating chocolate reduces the weight of chocolate
available
// this method takes a double as amount and reduce
that amount
// from the weight of chocolate if weight is greater
then the amount given
public void eat(double amount) {
// check if given amount is
negative
if(amount < 0.0) {
// print error
message
System.out.println("Cannot eat negative amount of
chocolate");
}
else {
// check if
given amount of chocolate is available to eat
if(weight <
amount) {
// print error message
// and inform currently available
chocolate
System.out.println("Cannot eat nonexistent
chocolate, only " +
weight + " Kg available");
}
else {
// let student eat the chocolate
// reduce amount from weight
weight = weight - amount;
}
}
}
// static method to test Chocolate class
public static void testChocolate() {
// print each method call
System.out.println("Testing
Chocolate:");
double amt = 2.5;
System.out.println("Testing
constructor with parameter: weight=" + amt);
Chocolate c = new
Chocolate(amt);
System.out.println("getWeight()
returns: " + c.getWeigth());
int amount = 250;
System.out.println("buying
Chocolate with amount= -120");
c.buy(-120);
System.out.println("buying
Chocolate with amount=" + amount);
c.buy(amount);
System.out.println("getWeight()
returns: " + c.getWeigth());
double eating = 3.5; // too much
chocolate being eaten
System.out.println("eating
chocolate of amount = -0.5");
c.eat(-0.5);
System.out.println("getWeight()
returns: " + c.getWeigth());
System.out.println("eating
chocolate of amount=" + eating);
c.eat(eating);
System.out.println("getWeight()
returns: " + c.getWeigth());
System.out.println("eating
chocolate of amount=" + eating);
c.eat(eating);
System.out.println("finish testing
Chocolate");
System.out.println();
}
}
==========
Student.java
==========
package student;
public class Student {
// member variables
private String name; // name of student
private MobilePhone phone; // mobile phone of
student
private Chocolate chocolate; // current amount of
chocolate that student have
private static int ID = 86575234; // student Id it
increase by 1 for each student created
// constructor
public Student(String name, int money) {
// create a bank account with given
money and link it to student's phone
// use student Id as phone
number
phone = new MobilePhone(Student.ID,
new BankAccount(money));
// assign name of student
this.name = name;
// assign amount of chocolate to
student
// at start its 0.0 Kg
chocolate = new
Chocolate(0.0);
// increase Id
ID++;
}
// getter methods
// returns students name
public String getName() {
return name;
}
// returns current amount of chocolate student have in
Kg
public double getChocolateWeight() {
return chocolate.getWeigth();
}
// when student is hungry, student tries to buy
chocolate
// then student eats half of the amount bought
public void hungry(int money) {
// check if student have the given
amount of money in account
if(phone.payMoney(money)) {
// get chocolate
left to student before he/she buys more chocolate
double
chocolate_before_buy = chocolate.getWeigth();
// student buys
chocolate with given amount of money
chocolate.buy(money);
// get total
amount of chocolate student have now
double
total_chocolate = chocolate.getWeigth();
// calculate
currently bought chocolate
double
bought_chocolate = total_chocolate - chocolate_before_buy;
// student eats
half the amount bough
double eaten =
bought_chocolate/2.0;
// remove eaten
chocolate from total weight
chocolate.eat(eaten);
}
else {
// payment
failed , no Money!!
// print
message
System.out.println("Student is still hungry!");
}
}
// static method to test Student class
public static void testStudent() {
// print each method call
System.out.println("Testing
Student:");
String sname = "Mike";
int amount = 300;
System.out.println("Testing
constructor with parameters: name=" + sname +
", money=" + amount);
Student stdnt = new Student(sname,
amount);
System.out.println("getName()
returns " + stdnt.getName());
System.out.println("Student have
chocolate in Kg: " + stdnt.getChocolateWeight());
int amt = 180;
System.out.println("calling
hungry(amt) amt=" + amt);
stdnt.hungry(amt);
System.out.println("Student have
chocolate in Kg: " + stdnt.getChocolateWeight());
System.out.println("calling
hungry(amt) amt=" + amt);
stdnt.hungry(amt);
System.out.println("Student have
chocolate in Kg: " + stdnt.getChocolateWeight());
System.out.println("finish testing
student");
System.out.println();
}
}
if you have any doubts or problem in program let me know.