Answer to Question #166754 in Java | JSP | JSF for Chandra sena reddy

Question #166754

Mobile

You are given an incomplete

Mobile class.A Mobile object created using the

Mobile class should have the properties like brand, ram, battery, isOnCall, and song.Implement the

Mobile class to initialize the mentioned properties and add the following methods,

Method:

charging

Description: When this method is called, it should set the value of the battery to 100, if the battery is already 100 then log "Mobile Fully Charged" and call removeCharging


method:

removeCharging

Description:

It should log "Please remove charging"


method:

playMusic

Description:

It should log a text with the song, as shown in the sample output.


method:

stopMusic

Description:

It should log "Music Stopped"


method:

makeCall

Description:

When this method is called, it should set the value of the isOnCall to true and log "Calling ..."


method:

endCall

Description:

When this method is called, it should log "No ongoing call to end" if isOnCall is false, else log "Call Ended" and set the value of the isOnCall to false

Input

  • The first line of input contains a string brand
  • The second line of input contains a string ram
  • The third line of input contains a number battery
  • The fourth line of input contains a string song
  • The fifth line of input contains a boolean isOnCall

Output

  • The first line of output is a string containing
  • battery before charging, as shown in the sample outputs
  • The second line of output is a string based on
  • battery, as shown in the sample outputs
  • The third line of output is a string containing
  • song, as shown in the sample outputs
  • The fourth line of output is a string "Music stopped"
  • The fifth line of output is a string "No ongoing call to end" or "Call Ended"
  • The sixth line of output is a string "Calling..."
  • The seventh line of output is a string "Call Ended"

Constraints

0 <=

battery <= 100


sample input

Apple

2 GB

90

Waka Waka

false


sample output

Mobile charged 90%

Mobile charged 100%

Playing Waka Waka song

Music stopped

No ongoing call to end

Calling...

Call Ended



1
Expert's answer
2021-02-26T16:15:00-0500
public class Phone {
   //private fields
   private String label;
   private String phonNum;
  
   //constructor: with 2 parameters
   public Phone(String label, String ph){
       this.label = label;
       this.phonNum = ph;
   }
  
   //constructor: with another phone instance
   public Phone(Phone phone){
       if(phone!=null){
           this.phonNum = phone.phonNum;
           this.label = phone.label;
       }
   }

   /**
   * @return the label
   */
   public String getLabel() {
       return label;
   }

   /**
   * @return the phonNum
   */
   public String getPhonNum() {
       return phonNum;
   }
  
}




import java.util.ArrayList;
/**
* Class: ContactInfo: It maintains phone numbers for a name
*/
public class ContactInfo {
   //private fields
   private String name;
   private ArrayList<Phone> phoneNums;
  
   /**
   * default constructor
   */
   public ContactInfo(){
       this.name = "unknown";//set to unknown
       this.phoneNums = new ArrayList<Phone>();//create empty list
   }
  
   /**
   * Parameterized constructor: ContactInfo
   * @param name
   * @param phoneNumber
   */
   public ContactInfo(String name,ArrayList<Phone> phoneNumber){
       this.name = name;
       this.phoneNums = new ArrayList<Phone>();
       //deep copy
       if(phoneNumber!=null && !phoneNumber.isEmpty()){
           for(Phone p : phoneNumber){
               this.phoneNums.add(p);
           }
       }
   }
   /**
   * getter for name field
   * @return
   */
   public String getName(){
       return this.name;
   }
   /**
   * getter for phoneNums
   * @return
   */
   public ArrayList<Phone> getPhones(){
       return this.phoneNums;
   }
  
   /**
   * Add a phone to phoneNums
   * It will return true if addition is successful
   * But it return false if similar label or phone number already ests in list
   * without adding it to the list
   * @param p
   * @return
   */
   public boolean addPhone(Phone p){
       boolean isAdded = false;  
       if(!isPhoneExists(p)){//check if isPhoneExists is false
           this.phoneNums.add(p);//add p
           isAdded = true;//set boolean field to true
       }
       return isAdded;
   }
  
   /**
   * Remove phoneNum from the list of phones
   * Returns true if successful
   * If the list is empty or there is no such phoneNum then it will
   * return false, without removing anything
   * @param phonNum
   * @return
   */
   public boolean removePhone(String phonNum){
       int index = getIndexOfPhoneNumber(phonNum);//get the index of given phoneNum in list
       if(this.phoneNums.isEmpty() || index==-1)//index will be -1 if phoneNum not in list
           return false;
       this.phoneNums.remove(index);//otherwise it will remove from the list
       return true;
   }
  
   /**
   * helper method to return in which indec the phoneNum exists
   * If not found will return -1
   * @param phoneNum
   * @return
   */
   private int getIndexOfPhoneNumber(String phoneNum){
       int index = -1;
       if(!this.phoneNums.isEmpty()){
           for(int i = 0; i < this.phoneNums.size(); i++){
               Phone p = this.phoneNums.get(i);
               if(p.getPhonNum().equals(phoneNum)){
                   index = i;
                   break;
               }
           }
       }
       return index;
   }
  
   /**
   * Helper method to retun false if phone with simalalr label
   * or phone number exists in the list
   * true otherwise
   * @param phone
   * @return
   */
   private boolean isPhoneExists(Phone phone){
       boolean isExist = false;
       if(phone!=null && !this.phoneNums.isEmpty()){
           for(Phone p : this.phoneNums){
               if(p.getLabel().equals(phone.getLabel()) ||
                       p.getPhonNum().equals(phone.getPhonNum())){
                   isExist = true;
                   break;
               }
           }
       }
       return isExist;
   }
  
}




package Program13;

import java.util.ArrayList;

/**
*This lass will test different methods of Phone and ContactInfo classes
*/

public class TestClass {
   public static void main(String[] args){
       System.out.println("*****Testing Phone Class*************");
       Phone p1 = new Phone("iphone","(513)000-0000");
       System.out.println("p1.getLabel()-->"+p1.getLabel());
       System.out.println("p1.getPhonNum()-->"+p1.getPhonNum());
      
       Phone p2 = new Phone("samsung","(513)111-1111");
       System.out.println("p2.getLabel()-->"+p2.getLabel());
       System.out.println("p2.getPhonNum()-->"+p2.getPhonNum());
      
       Phone p3 = new Phone(p2);
       System.out.println("p3.getLabel()-->"+p3.getLabel());
       System.out.println("p3.getPhonNum()-->"+p3.getPhonNum());
      
       //p1.setLabel("mobile"): This will not work since there is no public method in Phone class with this name
       System.out.println("*****Testing ContactInfo Class*************");
      
       ContactInfo pp1 = new ContactInfo();
       System.out.println( pp1.getName());
       System.out.println(pp1.getPhones());
      
       Phone p4 = new Phone("mobile","(555)444-4444");
       ArrayList<Phone> list1 = new ArrayList<Phone>();
       boolean status = list1.add(p4);
       System.out.println(status);
      
       pp1 = new ContactInfo("Jim Carey",list1);
       System.out.println(pp1);
      
       System.out.println( pp1.getName());
       System.out.println(pp1.getPhones());
      
       for(Phone phone: pp1.getPhones()){
           System.out.println(phone.getLabel()+ " "+ phone.getPhonNum());
       }
      
       status = pp1.addPhone(new Phone("home","(666)666-6666"));
       System.out.println(status);
      
       for(Phone phone: pp1.getPhones()){
           System.out.println(phone.getLabel()+ " "+ phone.getPhonNum());
       }
      
       Phone p5 = new Phone("main","(666)666-6666");
       System.out.println(p5);
      
       status = pp1.addPhone(p5);
       System.out.println(status);
      
       status = pp1.addPhone(new Phone("home","(123)000-8723"));
       System.out.println(status);
      
       status = pp1.removePhone("(000)000-0000");
       System.out.println(status);
      
       status = pp1.removePhone("(555)444-4444");
       System.out.println(status);
      
       for(Phone phone: pp1.getPhones()){
           System.out.println(phone.getLabel()+ " "+ phone.getPhonNum());
       }
      
      
      
      
   }
}







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog