Monday, May 24, 2010

I need help in java ((please))?

i have a prablom in that program


i already finsh the interface


but i have a prablom in the library





and here it is


public class supermarket2


{


public void DisplayDiscount( String d )


{





double x ;


double a ;


double b ;


double c ;





x= n *0.04;





if ( d == "saturday" %26amp;%26amp; d == "sunday" )


{


System.out.printf( "the dicount is 4% for all purchases", x );


}





else if ( d == "monday" %26amp;%26amp;d=="tuesdaY" %26amp;%26amp; d== "wednesday" %26amp;%26amp; d=="thursday" %26amp;%26amp; d == "friday")


{


System.out.printf( "",ComputeDiscount );


} }





public void ComputeDiscount( double n )


{








if ( n %26lt;= 100 %26amp;%26amp; n %26lt; 200)


{


System.out.printf( "the discount is" ,a);


}


else if ( n %26gt;= 201 %26amp;%26amp; n%26lt;=500)


{


System.out.printf( "the discount is" ,b);


}





else if (n %26gt;= 501)


{


System.out.printf( "the discount is" ,c );


}


}


}


my question is how to make the answer for the second if statement the other method (ComputeDiscount)

I need help in java ((please))?
I notice you have a number of problems.





1) You cannot check if two strings are equal to each other with == you need to use string1.equals(string2). Since strings are objects the double equals sign will compare the memory addresses.





2) You're not using printf correctly (although the code will still run). To print doubles in a printf you need to put %d in place of where the number will be inserted the way you have it no numbers will be printed.





3) A variable declared in a method only exists within that method. So where you use 'n' in DisplayDiscount and 'a','b', and 'c' in ComputeDiscount, you're trying to use variables that do not exist within those methods.





4) In DisplayDiscount your if statements wouldn't make sense even if == could check equality for Strings. Notice that you have this line of code


if(d == "saturday" %26amp;%26amp; d == "sunday")


but it is impossible for a string to be both saturday and sunday. What you need to do is use OR instead of AND so the code should look like


if(d.equalsIgnoreCase("saturday") || d.equalsIgnoreCase("sunday")





5)ComputeDiscount does not return a value so in the code where you have printf("",ComputeDiscount). Even after you change the printf to include a number and add parenthesis with a value to ComputeDiscount so that it looks like this printf("%d",ComputeDiscount(3)) it still will have no value to print.





6)This is more of a style issue and your code will run just as well without it but the Java naming conventions say that method names should start with a lower case letter so it should be computeDiscount(double n) and displayDiscount(String d)


No comments:

Post a Comment