This discussion is archived
2 Replies Latest reply: Apr 16, 2013 12:29 AM by gimbal2 RSS

Difference betn 2 codes of same output

1003161 Newbie
Currently Being Moderated
Hi,

I am new to Java language. Here, I have two Java code examples which prints out same output as expected.

Please let me know the basic differences of these two programs. And also tell me in which situations we should use this programs.

CODE NUMBER: 1_

public class ToStringDemo_1 {
     double height;
     double width;
     double depth;
     
     ToStringDemo_1(double arg1, double arg2, double arg3){
          height = arg1;
          width = arg2;
          depth = arg3;
     }
     
     public String getStringRepresentation(){
          return "Dimensions are "+height+" by "+width+" by "+depth+".";
     }
     
     public static void main(String[] args){
          ToStringDemo_1 stringDemo_1 = new ToStringDemo_1(10, 11, 12);
          
          System.out.println(stringDemo_1.getStringRepresentation());
     }
}



CODE NUMBER: 2_

public class ToStringDemo_2 {
     public String getStringRepresentation(double arg1, double arg2, double arg3){
          return "Dimensions are "+arg1+" by "+arg2+" by "+arg3+".";
     }
     
     public static void main(String[] args){
          ToStringDemo_2 stringDemo_2 = new ToStringDemo_2();
          System.out.println(stringDemo_2.getStringRepresentation(10, 11, 12));
     }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points