Thursday, July 30, 2009

Help with java programming, working on current code and having a difficulty getting whats being asked to work?

1. Display values a,b, and c with descriptive caption


2. write static method named halve () that takes in a single double parameter and returns that value divided by 2 (an int.)


3. call halve () three times, passing it a,b and c and display each result with a descriptive caption








public class Mintest


{


public static void main( String [ ] args )


{


double a=3;


double b=4;


double c=5;





System.out.println( min( a, b, c ) );


}





public static double halve( double x, double y, double z )


{


return x/2, y/2, z/2 ? x : y : z;


}


}

Help with java programming, working on current code and having a difficulty getting whats being asked to work?
This example takes java 1.5+. What I want is the newer String.format() method. Consider....





double num = 2.0;


String x = String.format("the number: %3.2f", num);


% is the placeholder.


3.2 is the "width" of the number, the .2 is the fraction


f informs java if the number is floating point double or float


the string is within the quote marks


after the comma is the variable





======================





public class MinTest {





public static void main(String[] args) {


// to gain Object.methods() w/o using the


// keyword "static", I make the Object





new MinTest();


}





public MinTest() {


double a = 3;


double b = 4;


double c = 5;





System.out.println( min( a, b, c ) );








double[] nums = new double[] {a,b,c};





for (int i = 0; i %26lt; 3; i++) {


String h = String.format("the method() halve( %2.2f ) ",halve(nums[i]));


System.out.println( h );





}








}


private String min(double a, double b, double c ) {


double testDoub = 0.0;


testDoub = Math.min(a,b);


testDoub = Math.min(testDoub,c);


Double d = new Double(testDoub);


return String.format("the minimum double of the range {" +


"%2.2f,%2.2f,%2.2f} is: %3.2f", a,b,c,d);





}


private double halve( double value ) {


// java will take on faith that the incoming value is double


return value / 2;


}


}
Reply:Well, as far as understand from the problem, the wrong points in your program are these:





1. You should display the a,b and c values. So you should use System.out.println();





2. It is asked that the 'halve() function takes in a single double parameter' , and your halve() function takes 3 parameters!


And of course its return value should be of type int(not double)


3. Then you should call it 3 times and pass x, y and z to it and display them.





So it should be like this:


(notice that '+' is for displaying like this: "a=3 b=4 c=5")





public class Mintest


{


public static void main( String [ ] args )


{


double a=3;


double b=4;


double c=5;





System.out.println("a=" + a+ " b=" +b + " c="+c);


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


System.out.println(halve(b));


System.out.println(halve(c));


}








public static int halve( double x)


{


return x/2;


}


}





This is what the written problem asks you to do. I didn't understand what was the min() or other things in you program for!





Good luck.


No comments:

Post a Comment