Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Passing Objects As Arguments

DOUBT In PASSING OBJECTS AS ARGUMENTS

ITS URGENT,Pl REPLY

I have doubts in the following program

import java.util.Scanner;

public class L55

{

public static void main(String args[])

{

String a="1234";

String b="1111";

//LongNum.lon(b);

LongNum la=new LongNum(a);

LongNum lb=new LongNum(b);

//LongNum lc=la.add(lb);

System.out.println(la.displayWithCommas());

System.out.println(lb.displayWithCommas());

//System.out.println("="+lc.displayWithCommas());

//System.out.println(" ");

System.out.println(la.toString());

System.out.println(lb.toString());

//System.out.println("="+lc.toString());

}

}

OUTPUT EXPECTED

22222222222
33333
----
22222255555
----

ANS

class LongNum

{

private String st;

//private char a;

//private int c;

public LongNum()

{;}

public LongNum(String s1)

{

st=s1;

}

public void setSt(String s1)

{

st=s1;

}

public char[] displayWithCommas()

{

StringBuffer str = new StringBuffer(st);

String d;

char []t=new char[4];

int []r=new int[4];

for(int i=0;i<=3;i++)

{

t[i]=str.charAt(i);

//System.out.println(t[i]);

//System.out.println(r[i]);

}

return t;

}

}

How to PROCEED FURTHER AFTER THIS

public int add(LongNum n)

{

int car=0;

int []r=new int[4];

char[] c=n.displayWithCommas();

char[] d=n.displayWithCommas();

for(int i=0;i<=3;i++)

{

r[i]=t[i]-'0';

//System.out.println(r[i]);

}

return t;

}
PL,HELP ME SOON, I HAVE A SUBMISSION
[2010 byte] By [geteas80] at [2007-11-11 8:49:48]
# 1 Re: Passing Objects As Arguments
What are your "doubts"? Is your program not compiling? What error messages are you getting?

Why is your "displayWithCommas" method returning a character array? Why not a string? You use StringBuilder - can't StringBuilder help you parse the input string to add a comma at the appropriate place?

Your add method will not be able to get access to any array t which is local to the displayWithCommas method.
nspils at 2007-11-11 22:34:19 >
# 2 Re: Passing Objects As Arguments
HI, THANKS FOR REPLYING, THANKU VERY MUCH FOR TAKING YOUR TIME, to solve my problem.THE PROGRAM WILL WORK IF I CHANGE THE LINE IN MAIN METHOD

LongNum lc=la.add(lb)

int lc=la.add(lb)

BUT WITH OUT CHANGING THE LINE, i want to make this program work

XX

//BLUE COLOR REPRESENTS QUESTION FOR THE PROBLEM

//WORKING WITH Numbers of Unlimited length

//Write the class LongNum so that the following code will produce the sum of the two values 'a' and 'b' input as strings.

SAMPLE RUN:

1
2222222
3333333333333333333333333333333333333333333333
=333333333333333333333333333333333335555555555

2
4567
9999
=14566

import java.util.Scanner;

public class Lab5
{

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

String a=sc.nextLine();

LongNum.lon(b);

String b=sc.nextLine();

LongNum la=new LongNum(a);

LongNum lb=new LongNum(b);

LongNum lc=la.add(lb);

System.out.println(la.displayWithCommas());

System.out.println(lb.displayWithCommas());

System.out.println("="+lc.displayWithCommas());

System.out.println(" ");

System.out.println(la.toString());

System.out.println(lb.toString());

System.out.println("="+lc.toString());

}

}

---------------
// I SHOULD NOT CHANGE THE QUESTION

//I WROTE THE FOLLOWING

class LongNumno
{
private String st;
//private char a;
//private int c;
public LongNum()
{;}
public LongNum(String s1)
{
st=s1;
}
public void setSt(String s1)
{
st=s1;
}
public char[] displayWithCommas()
{
StringBuffer str = new StringBuffer(st);
String d;
char []t=new char[4];
int []r=new int[4];
for(int i=0;i<=3;i++)
{
t[i]=str.charAt(i);
//System.out.println(t[i]);
//System.out.println(r[i]);
}
return t;
}
public int add(LongNum n)
{
int car=0;
int []r = new int[10];
char[]c = n.displayWithCommas();
StringBuffer strBuf = new StringBuffer();

for(int i=0;i<=3;i++)
{

r[i]=c[i]-'0';

strBuf=strBuf.append(r[i]);

}

String strAdd = strBuf.toString() + st ;

System.out.println("am here in String add : " + strAdd);

int a = Integer.parseInt(strBuf.toString());

int b = Integer.parseInt(st);

int intAdd = a + b ;

System.out.println("am here in integer add : " + intAdd);

return intAdd;
}
}
geteas80 at 2007-11-11 22:35:24 >
# 3 Re: Passing Objects As Arguments
displayWithCommas

is the method name not the literal meaning DISPLAY WITH COMMAS
geteas80 at 2007-11-11 22:36:17 >