2 Replies - 404 Views - Last Post: 10 March 2010 - 05:22 AM Rate Topic: -----

#1 doha786  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

How to separate return value ?

Posted 10 March 2010 - 03:28 AM

Hi,

From the below code if i want to separate the return value and print separately what should i do ?
public class TestValue{
String print;

public String calculate(double a, double B)/>{
double add=a+b;
double subtract=a-b;
double multiply=a*b;
double division=a/b;

print = "Total="+add+"\nSubtract="+subtract+"\nMultiply="+multiply+"\nDivision="+division ;

return print ;

}

public static void main(String[] args) {
TestValue tv=new TestValue();
String st=tv.calculate(20,10);

System.out.print(tv);
}


i mean i just want to print out subtract only or multiply only without changing the String 'print'

Is it possible ??

This post has been edited by doha786: 10 March 2010 - 03:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to separate return value ?

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: How to separate return value ?

Posted 10 March 2010 - 04:26 AM

You can, but it HAS to be somewhere inside of the calculate() method because add, subtract, multiply, and division are all variable local to calculate and cannot be referenced in main, so if you want to print it:
1) print it in calculate.
public String calculate(double a, double B)/>{
    double add=a+b;
    double subtract=a-b;
    double multiply=a*b;
    double division=a/b;

    System.out.println(subtract);

    print = "Total="+add+"\nSubtract="+subtract+"\nMultiply="+multiply+"\nDivision="+division ;

    return print ;

}


2) Make the variables instance variables and make get/set values for it.
public class TestValue{
    String print;

    double add=a+b;
    double subtract=a-b;
    double multiply=a*b;
    double division=a/b;

    public String calculate(double a, double B)/>{

        print = "Total="+add+"\nSubtract="+subtract+"\nMultiply="+multiply+"\nDivision="+division ;

        return print ;

    }

    public int getSubtract() {
       return subtract;
    } 

    public static void main(String[] args) {
        TestValue tv=new TestValue();
        String st=tv.calculate(20,10);

        System.out.println(tv.getSubtract());

        System.out.print(tv);
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 doha786  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 26-March 09

Re: How to separate return value ?

Posted 10 March 2010 - 05:22 AM

thank you so much Dogstopper, i think i get it from you..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1