1

This seems to be a very simple problem, but I am not sure that I am using these constructors correctly. If you could take a look and tell me why when I try to print this data, it displays (what I assume to be) the data location. I can't think of a way to get it to print out the name, freezing point, boiling point, and number of units. Any suggestions on making this more efficient are welcome too.

This is the first class

public class ChemicalInventory {

    public static void main(String[] args) {

    listInventory();
    }


    public static void listInventory(){
        Chemical Ethanol        = new Chemical("Ethanol",         "-173",   "172",   "1575");
        Chemical Oxygen         = new Chemical("Oxygen",          "-363",   "-306",  "1000");
        Chemical Water          = new Chemical("Water",           "32",     "212",   "5000");
        Chemical Benzene        = new Chemical("Benzene",         "41.9",   "176.2", "2750");
        Chemical EthyleneGlycol = new Chemical("Ethylene Glycol", "8.78",   "378",   "1900");

        System.out.println("Current Chemical Inventory: ");
        System.out.println(Ethanol);
        System.out.println(Oxygen);
        System.out.println(Water);
        System.out.println(Benzene);
        System.out.println(EthyleneGlycol);
    }



}

This is the Second class.

public class Chemical {

    private String chemName;
    private String chemFreezingPoint;
    private String chemBoilingPoint;
    private String chemUnitNumbers;


    public Chemical(String name, String freezingPoint, String boilingPoint, String unitNumbers){
    chemName = name;
    chemFreezingPoint = freezingPoint;
    chemBoilingPoint = boilingPoint;
    chemUnitNumbers = unitNumbers;
    }


    //String[][] chemArray = new String [5][4];
    String[][] chemArray = {{"Ethanol","-173","172","1575"},{"Oxygen","-363","-306","1000"},{"Water","32","212","5000"},
                            {"Benzene","41.9","176.2","2750"},{"Ethylene Glycol","8.78","378","1900"}};

}

This is what I get when I run the program:

Current Chemical Inventory: 
Chemical@15db9742
Chemical@6d06d69c
Chemical@7852e922
Chemical@4e25154f
Chemical@70dea4e
X45352
  • 9
  • 3

3 Answers3

1

Because currently you are printing the reference to the Object. Try something like this:

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

This should give you the data inside the Object.

Also, you can override the toString() method and write your own implementation.

Something like this:

public class Chemical {

    private String chemName;
    private String chemFreezingPoint;
    private String chemBoilingPoint;
    private String chemUnitNumbers;


    public Chemical(String name, String freezingPoint, String boilingPoint, String unitNumbers){
    chemName = name;
    chemFreezingPoint = freezingPoint;
    chemBoilingPoint = boilingPoint;
    chemUnitNumbers = unitNumbers;
    }


    //String[][] chemArray = new String [5][4];
    String[][] chemArray = {{"Ethanol","-173","172","1575"},{"Oxygen","-363","-306","1000"},{"Water","32","212","5000"},
                            {"Benzene","41.9","176.2","2750"},{"Ethylene Glycol","8.78","378","1900"}};

     public String toString(){
        return // Return the string in the format you want here... 
     }

}
0

You need to override toString() method like this:

@Override
public String toString() {
    return "Chemical [chemName=" + chemName + ", chemFreezingPoint=" + chemFreezingPoint + ", chemBoilingPoint="
            + chemBoilingPoint + ", chemUnitNumbers=" + chemUnitNumbers + "]";
}

You can format the return statement anyway you like.

Your constructor has no problems it is absolutely fine.

Faraz
  • 6,025
  • 5
  • 31
  • 88
0

Your constructors are fine. There's actually nothing broken here. In java every class has a default toString() method, that prints out, well, what you're seeing there. YOu get it for free but it's not very informative.

Try adding this to your Chemical class:

public String toString(){
 return "I am a chemical:" + name;
}

and expand as needed.

Steve B.
  • 55,454
  • 12
  • 93
  • 132
  • That does override. I did not add the annotation because it would compile without it, and since this is clearly a beginner I wanted to keep it simple. – Steve B. Feb 17 '16 at 02:48
  • Faraz, overriding is from the method signature, not the annotation. I promise you that this works. After all, annotations are a java5 (or maybe 6, I forget) language addition, overriding existed before that). In my IDE I get warnings from missing @Override annotations. – Steve B. Feb 17 '16 at 02:51
  • Thank you all for showing this to me, my program works fine now and I also know that I need to read more on the toString. I really do appreciate the help! – X45352 Feb 17 '16 at 02:57