Note that there are some explanatory texts on larger screens.

plurals
  1. POObject passed to the method in java
    primarykey
    data
    text
    <p>I'm try to simulate a mobile phone. I've created the following methods:</p> <ol> <li>load <br></li> <li>call <br></li> <li>sendTextMessage<br></li> <li>getLoad<br></li> <li>getTotalMinutesCalled<br></li> <li>changeRate<br></li> <li>printSummary<br></li> <li>passLoadTo<br></li> </ol> <p>My problem is that when I pass an object to the <code>passLoadTo()</code> method, and then print the summary of that object, it doesn't change the state of that object.</p> <p>Here is what I'm trying to do:</p> <pre><code>public class MobilePhone { private double credit; private int totalMinutes; private double ratePerCall; private int numOfText; MobilePhone() { credit = 0; totalMinutes = 0; ratePerCall = 6.50; numOfText = 0; } public void load(double pesos) { if(pesos &gt; 25) { credit = credit + pesos; } else { //System.out.println("The attempting to use this service is forbidden."); //System.out.println(); } } //other code public void printSummary() { System.out.printf("Credit left: P%.2f" ,getLoadLeft()); System.out.println(); System.out.println("Total call duration "+getTotalMinutesCalled()+" mins"); System.out.printf("Rate per call: P%.2f" ,ratePerCall); System.out.println(); System.out.println("Number of text message send: " +getNumTextMessage()); System.out.println(); } public void passLoadTo(MobilePhone dest, double amount) { if(credit &gt; amount) { credit = credit - amount; dest.load(amount); } else { System.out.println("fail"); System.out.println("The attempting to use this service is forbidden."); System.out.println(); } } } </code></pre> <p>this code is for testing purposes:</p> <pre><code>public class Main { public static void main(String[] args) { MobilePhone mp = new MobilePhone(); MobilePhone mp1 = new MobilePhone(); mp.load(30); mp.printSummary(); mp.changeRate(1); mp.printSummary(); mp.call(1); mp.printSummary(); System.out.println(mp.getTotalMinutesCalled()); mp.passLoadTo(mp1, 30); mp.printSummary(); mp1.printSummary(); } } </code></pre> <p>What i want is whenever i created a 2 mobile object(mp and mp1) and call mp.passLoadTo(mp1,1) method, the summary of the mp1 summary prints the load i added from mp to mp1</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload