1

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;
    }
    
}
Druckles
  • 3,161
  • 2
  • 41
  • 65
vumedfsdvb
  • 11
  • 1
  • if i understood correctly, you have to return 173.0 from 17 and 3.0, you can plus them, like this ""+myInt+myDouble that will give you 173.0, in your toString method of Number class you can return myStr variable, you do not need >> (myInt + " , " + myDouble); this.and you have to return num variable in MyNumber class instead of myStr. – George Weekson Oct 25 '20 at 18:11

2 Answers2

0

Fix of your Number class.

 public String toString(){
       return myStr;
  }

use above method instead of

 public String toString(){
   return (myInt + " , " + myDouble);
}

this one.

fix of your MyNumber class

public Number getStr(){
    
    
 return num;
    
}

return num, because your myStr of Number is null, you do not use it at all. thats why you have null in console. it is not initialized. read java primitive types and default values. Objects are null if they do not have values assigned. primitive values have default values if they do not have values assigned. link that can help

George Weekson
  • 483
  • 3
  • 13
0

You didn't assign anything to myStr in MyNumber so it assigned null by JVM when MyNumber created. (Initialization process)

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);
    myStr = new Number(myInt, myDouble);
 }

 public Number getBoth() {
    return num;
 }

 public String getStr() {
    return myStr.getMyStr();

 }
}


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 String getMyStr() {
    return myStr;
 }

}

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());
 }
}

this will work.

Inanc Cakil
  • 306
  • 2
  • 9