minimum value of four numbe Program is running correctly but it has not a pass testing :/ . what should be pass testing in this program :/ . help needed .

public class Solution
{
    public static int min(int a, int b, int c, int d)
    {
        min(a,b);
        min(a,b);
        min(a,b);

        int m = 0 ;
        if (a>b) m = b ;
        else if (a<b) m = a ;

        int m2 = 0 ;
        if (c<d) m2 = c ;
        else if (c>d) m2 = d ;

        int x = 0 ;
        if (m < m2) x = m ;
        else if (m > m2) x = m2 ;

        return x ;

    }

    public static int min(int a, int b)
    {
        int result = 0 ;
        if (a>b) result = b ;
        else if (a<b) result = a ;

        return result ;

    }

    public static void main(String[] args) throws Exception
    {
        System.out.println( min(-20, -10) );
        System.out.println( min(-20, -10, -30, -40) );
        System.out.println( min(-20, -10, -30, 40) );
    }
}

asked 12 Mar '15, 17:06

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311424
accept rate: 0%

edited 13 Mar '15, 07:12

Elleonora%20Carry's gravatar image

Elleonora Carry ♦♦
275


public static int min(int a, int b) {

    int result = 0 ;
    if (a>b) result = b ;
    else if (a < b) result = a ;
    return result ;
}

If you use this method for a = 2 and b = 2, it returns 0. It is wrong.

if method for two numbers works fine, you can make like this:

public static int min(int a, int b, int c, int d) { 
 return  min( min(a,b) , min(c,d) );
}
link

answered 12 Mar '15, 19:18

RabenokDmitry's gravatar image

RabenokDmitry
1263
accept rate: 14%

edited 13 Mar '15, 07:14

Elleonora%20Carry's gravatar image

Elleonora Carry ♦♦
275

problem is still same by doing this alsooo .

(13 Mar '15, 11:06) Muhammad Has...

I think that you can use method min with two parametrs in method min with four parametrs. For example search min between a and b, search min between c and d. And search min between results which you get before. Three times you use method with two parametrs and get min between four parametrs.

In your porgramme if a=b then always m = 0.

In your porgramme if c=d then always m2 = 0.

link

answered 12 Mar '15, 17:58

RabenokDmitry's gravatar image

RabenokDmitry
1263
accept rate: 14%

code starts from here . i forgot to wirte this function . and i didnt understand what you said in you answear . public class Solution { public static int min(int a, int b, int c, int d) {

(12 Mar '15, 18:11) Muhammad Has...

don't check extra condition in else block just return what is remaining for instance if b is less than a then it returns b if it is not then what is remaining ? simply it is understood that the remaining variable will be the minimum means there is no need to check it again and whenever a and b both are equal then your program will return b and it will be ok .... use the logic like this....

if (a>b) return b ; else return a ;

and one more thing don't create extra variable like (m or m2 or result) you can simply return the varibles a and b directly to the function....

link

answered 12 Jun '15, 12:39

Karan%20Sachrani's gravatar image

Karan Sachrani
1
accept rate: 0%

My solution is:

public static int min(int a, int b, int c, int d)
        {
            //add your code here
            return (min(a,b)< min(c,d)?min(a,b):min(c,d));
        }

        public static int min(int a, int b)
        {
            //add your code here
            return  (a)<(b) ?a:b;
        }

use the ?: operator. it is shorter.

For the topic starter:
1) comment out rows 5,6,7
2) when you check the second condition in the ELSE statement you check it with the ">" operator. But actually there can be equal numbers. so instead

 else if (c>d) m2 = d ;

you should write

else m2 = d ;
link

answered 21 May '16, 10:19

Cyr's gravatar image

Cyr
11
accept rate: 0%

edited 21 May '16, 10:22

public class Solution { public static int min(int a, int b, int c, int d) {

    if(a<b && a<c && a<d)
    return a;
    else if(b<a && b<c && b<d)
    return b;
    else if(c<a && c<b && c<d)
    return c;
    else if (d<a && d<b && d<c)
    return d;
    else {
        return min(a,b);
    }

    //add your code here

}

public static int min(int a, int b)
{
    if(a<b)
    return a;
    else {
        return b;
    }
        //add your code here

}

public static void main(String[] args) throws Exception
{
    System.out.println( min(-20, -10) );
    System.out.println( min(-20, -10, -30, -40) );
    System.out.println( min(-20, -10, -30, 40) );
}

}

link

answered 21 Jan '17, 19:59

Ralph%20Regan's gravatar image

Ralph Regan
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×69
×53
×17

Asked: 12 Mar '15, 17:06

Seen: 2,438 times

Last updated: 21 Jan '17, 19:59