Friday, July 31, 2009

Any ideea on this java program?

Write a program that has two classes:


Triple0000


three private instance variables: first, second, third


Methods:


public void setValue(int i, double value):  if i =1, set first to the newValue, if i=2 set second to the newValue; if i=3, set third to the newValue.


public void setValues(double firstValue, double secondValue, double thirdValue)


public getValue(int i): i =1, get value of first, i=2 get value of second; i=3, get value of thrid.


public static double min( double a , double b, double c): try to use Math.min method


public static double max() (double a , double b, double c): try to use Math.max method


public double getMin(): try to use the.min method you defined


public double getMax(): try to use the above max method you defined


public double sum(): return the sum of the tree instances


public String toString(): return a string. If first=3, second = 5, third =7, then it generates the following string


Triple[3, 5, 7]


Any other methods you like

Any ideea on this java program?
I never figured out what a second class was needed


for, so I just implemented "Triple000". There's


nothing "clever" here; just a straight-forward


implementation of the problem specs. There's a


"main()" which exercises most of the methods


(either directly or indirectly).











public class Triple000


{


  double first, second, third;





  


  public void setValue(int i, double value)


  {


    if (i==1)


      first = value;


    else if (i==2)


      second = value;


    else if (i==3)


      third = value;


  }


  public void setValues(


    double firstValue,


    double secondValue,


    double thirdValue)


  {


    first = firstValue;


    second = secondValue;


    third = thirdValue;


  }





  public void Triple000()


  {


    setValues(0.0, 0.0, 0.0);


  }





  public double getValue(int i)


  {


    if (i==1)


      return first;


    else if (i==2)


      return second;


    else if (i==3)


      return third;


    else


      return -1.0; /* exception? */


  }





  public static double min(double a,double b,double c)


  {


    return Math.min(Math.min(a,b), c);


  }





  public static double max(double a,double b,double c)


  {


    return Math.max(Math.max(a,b), c);


  }





  public double getMax()


  {


    return max(first, second, third);


  }





  public double sum()


  {


    return first+second+third;


  }





  public String toString()


  {


    return "Triple[" + first + ", "


         + second + ", " + third + "]";


  }





  public static void main(String argv[])


  {


    Triple000 a = new Triple000();


    //  0, 0, 0





    a.setValues(1.0, 2.0, 3.0);


    //  1, 2, 3





    a.setValue(1, 10.0);


    // 10, 2, 3





    a.setValue(3, a.min(5.0, a.getValue(1), 7.0));


    // 10, 2, 5





    a.setValue(1, a.sum() - a.getMax());


    //  7, 2, 5





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


  }


}








Then to compile and run it:





    $ javac Triple000.java





    $ java Triple000


    Triple[7.0, 2.0, 5.0]





.

nobile

No comments:

Post a Comment