So I made three separate classes:
NumberTester
: where all the test cases go
Number
: where you concatenate the int and double into the public String myStr, and
MyNumber
: where you make another method called getStr() and return the value of myStr.
Here are the codes below. My problem is when I concatenate the int and double into myStr and then run the NumberTester
's main method. I should be getting the output of 17, then 3.0, then 17, 3.0, then 173.0. However, I get null instead of 173.0. Could anyone help me solve this?
Output:
17
3.0
17 , 3.0
173.0
public class NumberTester
{
public static void main(String args[]){
MyNumber me = new MyNumber(17, 3.0);
System.out.println(me.num.myInt);
System.out.println(me.num.myDouble);
System.out.println(me.getBoth());
System.out.println(me.getStr());
}
}
public class Number
{
public int myInt;
public double myDouble;
public String myStr;
public Number(int k, double d){
myInt = k;
myDouble = d;
myStr = myInt + "" + myDouble;
}
public String toString(){
return (myInt + " , " + myDouble);
}
}
public class MyNumber
{
private int myInt;
private double myDouble;
public Number num;
public Number myStr;
public MyNumber(int i, double d){
myInt = i;
myDouble = d;
num = new Number(myInt, myDouble);
}
public Number getBoth(){
return num;
}
public Number getStr(){
return myStr;
}
}