Question

In: Computer Science

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     |
+------------------------------------------------+
| - 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

public class BankAccount {
   private int money;
  
   public BankAccount(int money) {
       this.money=money;
   }
  
   public int getMoney() {
       return money;
   }

   public void setMoney(int money) {
       this.money=money;
   }
  
   public static void testBankAccount() {
       BankAccount account=new BankAccount(200);
       System.out.println("Current amount::"+account.getMoney());
       account.setMoney(400);
       System.out.println("Current amount::"+account.getMoney());
      
   }
   /*
   * public static void main(String[] args) { BankAccount.testBankAccount(); }
   */
}

*************************************************************************************************************************

public class 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.
   */

   private int number;
   private BankAccount account;
  
   public MobilePhone(int number, BankAccount account) {
       this.number=number;
       this.account=account;
   }
  
   public int getNumber() {
       return number;
   }
  
   public boolean paymoney(int amount) {
       if(amount>0) {
           int newAmount=account.getMoney()-amount;
           if( newAmount>=0 && account.getMoney()>=newAmount) {
               account.setMoney(newAmount);
               return true;
              
           }else {
               System.out.println("Insufficient amount in account.");
           }
          
       }else {
           System.out.println("please enter positive numbers in amount");
       }
       return false;
   }
  
   public static void testMobilePhone() {
       BankAccount account =new BankAccount(300);
       MobilePhone phone=new MobilePhone(123456, account);
      
       System.out.println(phone.paymoney(500));
       System.out.println(phone.paymoney(200));
       System.out.println("balance ::"+account.getMoney());
       System.out.println(phone.paymoney(100));
       System.out.println("balance ::"+account.getMoney());
      
   }
   /*
   * public static void main(String[] args) { MobilePhone.testMobilePhone();
   *
   * }
   */
}
*******************************************************************************************************************************

public class Chocolate {
   /*
   * | 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 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.
   */
   private double weight;
  
   public Chocolate(double weight) {
       if( weight<(float)0.0) {
           weight=0.0;
       }
       this.weight=weight;
   }
  
   public double getWeight() {
       return weight;
   }
  
   public void buy(int money) {
       double standardPricePerkiloGram=(double) 1.5/ (double)100;
       if(money>0) {
       double calculatedWeightOfChocolates=standardPricePerkiloGram*money;
//System.out.println("calculatedWeightOfChocolates::"+calculatedWeightOfChocolates);
       if(calculatedWeightOfChocolates>weight) {
           System.out.println("Cannot buy negative amount of chocolate");
       }
       weight=weight-calculatedWeightOfChocolates;
       //System.out.println("weight now::"+weight);
       }else {
           System.out.println("please enter correct amount.");
       }
      
      
   }

   public void eat(double amount) {
       double standardPricePerkiloGram=(double) 1.5/ (double)100;
      
       double presentWeightOfChocolates=weight;
       double calculatedWeightOfChocolates=standardPricePerkiloGram*amount;
       if(calculatedWeightOfChocolates>presentWeightOfChocolates) {
           System.out.println("Cannot eat nonexistent chocolate, only "+presentWeightOfChocolates+" Kg available");
       }
      
      
   }
  
   public static void testChocolate() {
       Chocolate chocolate=new Chocolate(3.0);
       chocolate.buy(200);
       chocolate.eat(100);
      
   }
  
   /*
   * public static void main(String[] args) { Chocolate.testChocolate(); }
   */
  
}
*******************************************************************************************************************

public class Student {

  
   /*
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.

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.
   */
  
   private String name;
   private MobilePhone phone;
   private Chocolate chocolate;
  
   public Student(String name, int money) {
       int studentID=12345;
       this.name=name;
       this.chocolate=new Chocolate(0.0);
       BankAccount account=new BankAccount(money);
       this.phone=new MobilePhone(studentID, account);
   }
  
   public String getName() {
       return name;
   }
  
   public double getChocolateWeight() {
       return chocolate.getWeight();
   }
  
   /*
   * 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.
   */
  
  
   public void hungry(int money) {
       if(phone.paymoney(money)) {
           //System.out.println("payment done");
           chocolate.buy(money);
           //System.out.println("chocolate bought");
           double weightofChcoclateBought=chocolate.getWeight();
//System.out.println("weightofChcoclateBought::"+weightofChcoclateBought);
           double halfOfweightofChcoclateBought=weightofChcoclateBought/2;
           chocolate=new Chocolate(halfOfweightofChcoclateBought);
       }else{
           System.out.println("Student is still hungry!");
       }
      
   }
  
   public static void testStudent() {
       Student student=new Student("testStudent", 100);
       System.out.println("default chocolate weight::"+student.getChocolateWeight());
       student.chocolate=new Chocolate(3.0);
       System.out.println("chocolate weight::"+student.getChocolateWeight());
       student.hungry(100);
       System.out.println("chocolate weight::"+student.getChocolateWeight());//should be half of the chocolates bought
      
   }
  
   /*
   * public static void main(String[] args) { Student.testStudent(); }
   */
}
***************************************************************************************************************************

public class Start {
   public static void main(String[] args) {
       Student.testStudent();
       Chocolate.testChocolate();
       MobilePhone.testMobilePhone();
       BankAccount.testBankAccount();
   }

}

Here is the code for your question. the code is in accordance with every line specified in the question and is self-explanatory.

Just copy-paste the classes to any IDE of your preference and you are good to go. I have kept the Sysout in comments for you to understand it better.you can remove it later once you have gone through it.


Related Solutions

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 | +-----------------------------+ +------------------------------------------------+ |...
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 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...
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...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression in postfix notation and output its value.
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...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed … what you did to fix it. import java.util.Scanner; public static void main(String[] args) { // This program Converts grade Points into a Letter grade. int gradePoints == 00; // Declare variable and assign initial value System.out.printl("Enter Grade Points: "; //Input gradePoints; if ( gradePoints >= -42 ) { System.out.println("Grade = A"); } // if true, then ends here, if false drop to next...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
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...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT