Question

In: Computer Science

This the question about the java and please show the detail comment and code of each...

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.

Solutions

Expert Solution

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.


Related Solutions

This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
This problem is about java program and please show the detail comment and code in each...
This problem is about java program and please show the detail comment and code in each class. Thank you! Create four classes with the following UML diagrams: (The "-" means private and the testBankAccount() testMobilePhone() testChocolate() testStudent() all static +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ | MobilePhone     | +------------------------------------------------+ | -...
This is the question about the java problem, please give the detail comment and code of...
This is the question about the java problem, please give the detail comment and code of each class. Please write tests for all the code of all the classes Thank you Create a class Mammal with the following UML diagrams: +---------------------------------+ | Mammal | +---------------------------------+ | - name: String | +---------------------------------+ | + Mammal(String name) | | + getName(): String | | + isCookable(): boolean | | + testMammal(): void | (this method is static ) +---------------------------------+ The isCookable method...
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Code the following in bash and run. Please show full code and don’t forget to comment...
Code the following in bash and run. Please show full code and don’t forget to comment your code. Make a code that takes any list of numbers and calculates and displays the mean, median and mode.
Language: C and comment code please PThreads The goal of this homework is to show an...
Language: C and comment code please PThreads The goal of this homework is to show an understanding of pthreads and C. Assignment: Write a C program to use the bitwise operations with each bitwise operation contained within its own thread. Each thread should take 1 parameter and will only modify global values. First, create global integers x and y, then, in the main function you will read in values for x and y from the user and then create the...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding. We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :) Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT