How to select an input from a set of inputs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    How to select an input from a set of inputs?

    Hi All,
    I do really need to consult you guys about my java assignment.
    Please guide me how to make an algorithm to select an input from a set of inputs?

    Suppose I have a program p (a), an input a=1, a set of inputs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. This is the program:

    int a=1
    if(a<5) {
    System.out.prin tln("if-1: True");
    if(a<3) {
    System.out.prin tln("if-2: True"); }
    else {System.out.pri ntln("if-2: False"); }
    System.out.prin tln("a:" +a); }
    else {
    System.out.prin tln("if-1: False");
    if(a<8)
    {
    System.out.prin tln("if-3: True"); }
    else {
    System.out.prin tln("if-3: False"); }
    System.out.prin tln("a:" +a); }

    How am I supposed to do?
    Anything to do with set those inputs is stored in a file and the program first read the file to get all candidates into a list?

    Note: Because later I need to select an input from the set to force a different branch of execution.

    Thanks guys....
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Hi All,
    I do really need to consult you guys about my java assignment.
    Please guide me how to make an algorithm to select an input from a set of inputs?

    Suppose I have a program p (a), an input a=1, a set of inputs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. This is the program:

    int a=1
    if(a<5) {
    System.out.prin tln("if-1: True");
    if(a<3) {
    System.out.prin tln("if-2: True"); }
    else {System.out.pri ntln("if-2: False"); }
    System.out.prin tln("a:" +a); }
    else {
    System.out.prin tln("if-1: False");
    if(a<8)
    {
    System.out.prin tln("if-3: True"); }
    else {
    System.out.prin tln("if-3: False"); }
    System.out.prin tln("a:" +a); }

    How am I supposed to do?
    Anything to do with set those inputs is stored in a file and the program first read the file to get all candidates into a list?

    Note: Because later I need to select an input from the set to force a different branch of execution.

    Thanks guys....
    Try explaining again and if you need to post code, please do so using code tags.

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Ops sorry mode...
      Let me explain again my problem.

      Let say I have a test driver - TestDriver.java which stores and a set of test inputs such this :
      Code:
      int input[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
      The TestDriver suppose to run a program under test - MyProgram.java.
      Code:
       int a=1;	  	 	  
       if(a<5)
        { 
          System.out.println("if-1: True");
        if(a<3) {
        	System.out.println("if-2: True");
                  }		   	   
        else {
               System.out.println("if-2: False");
               }
            System.out.println("a:" +a);		 
      }   
         else 
          {
            System.out.println("if-1: False");
           if(a<8)
           {
             System.out.println("if-3: True");
           }
        else {
                System.out.println("if-3: False");
       }				  
      System.out.println("a:" +a);
      }
      First I run the program using int a = 1; until finish execution.
      Then I want TestDriver to pass other int value (from input[ ]) to MyProgram and same until finish execution.

      My questions here are:
      1. How am I suppose to pass other int values to MyProgram after finish the first execution?
      2. Is there anything to do with serialization - write and read those set of inputs?

      Please advise..Many thanks

      beginner to java,
      Shana

      Comment

      • hirak1984
        Contributor
        • Jan 2007
        • 316

        #4
        May be this code will help you :

        Code:
         public class A1 {
        public static void main(String args[])
        {
        A2 newA2=new A2();
         int a[]={1,2,3,4,5,6,7,8,9};
        int i=a.length;
        for(int j=0;j<i;j++)
         newA2.PrintA(a[j]);
        }
        }
        then the second class
        Code:
         public class A2 {
        public void PrintA (int a)
        {
         if(a<5)
           { 
        	 System.out.println("if-1: True");
           if(a<3) {
        	System.out.println("if-2: True");
        			 }		 
           else {
        		  System.out.println("if-2: False");
        		  }
        	   System.out.println("a:" +a);   
         }   
        	else 
        	 {
        	   System.out.println("if-1: False");
        	  if(a<8)
        	  {
        		System.out.println("if-3: True");
        	  }
           else {
        		   System.out.println("if-3: False");
          }	  
         System.out.println("a:" +a);
         }
        }
        }

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          are these seperate programs ? if so you could pass the parameter a as via the main() parameter list, e.g.
          Code:
               int a=1;          // value to pass to MyProgram
               // get runtime environment and execute child process
               Runtime systemShell = Runtime.getRuntime();
               Process output = systemShell.exec("java MyProgram " + a);

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            How about

            Code:
             void something(int x) { 
            int a=x; 	
            if(a<5)
              { 
            	System.out.println("if-1: True");
              if(a<3) {
              System.out.println("if-2: True");
            			} 	  
              else {
            		 System.out.println("if-2: False");
            		 }
            	  System.out.println("a:" +a); 
            }   
               else 
            	{
            	  System.out.println("if-1: False");
            	 if(a<8)
            	 {
            	   System.out.println("if-3: True");
            	 }
              else {
            		  System.out.println("if-3: False");
            }   
            System.out.println("a:" +a);
            }
            }
            Now since all this is now in a method, you can now call the method with any argument you want starting 1 as
            Code:
             something(1);
            and for any other entry in your array using
            Code:
             something(array[index]);
            Will this work for you?

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              May be this code will help you :
              Yes, it really helps. Many thanks bro...

              Comment

              • shana07
                Contributor
                • Jan 2007
                • 280

                #8
                Originally posted by horace1
                are these seperate programs ? if so you could pass the parameter a as via the main() parameter list, e.g.
                Yes it is separate program..also could but I prefer simpler ..
                Thanks to you.

                Comment

                • shana07
                  Contributor
                  • Jan 2007
                  • 280

                  #9
                  Originally posted by r035198x
                  How about
                  Now since all this is now in a method, you can now call the method with any argument you want starting 1 as
                  Will this work for you?
                  It works great. thank you.
                  At this moment I chose this sample code because my intention here is to pass an int value at a time and monitor the execution until finish (something to do with testing).
                  And the first sample code given above also useful for me....shall use it then.

                  Thanks

                  ~Cheers~
                  Shana

                  Comment

                  • shana07
                    Contributor
                    • Jan 2007
                    • 280

                    #10
                    Okay. I have a question to be asked here.
                    MyProgram file now also NEED 'main'
                    Code:
                    public static void main (String[] args)
                    .

                    My TestDriver file is also having 'main'. How should I put this method in?
                    This method is from the third sample code above.

                    Code:
                    public void something(int x) 
                     { 
                    	int a=x; 
                    		
                    	if(a<5)
                    	{ 
                    		System.out.println("if-1: True");
                    	  if(a<3) {
                    	  System.out.println("if-2: True");
                    				} 	  
                    	  else {
                    			 System.out.println("if-2: False");
                    			 }
                    		  System.out.println("a:" +a); 
                    	}   
                       else 
                    	{
                    	  System.out.println("if-1: False");
                    	 if(a<8)
                    	 {
                    	   System.out.println("if-3: True");
                    	 }
                      else {
                    		  System.out.println("if-3: False");
                    	}   
                    	System.out.println("a:" +a);
                    	}
                      }

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by shana07
                      Okay. I have a question to be asked here.
                      MyProgram file now also NEED 'main'
                      Code:
                      public static void main (String[] args)
                      .

                      My TestDriver file is also having 'main'. How should I put this method in?
                      This method is from the third sample code above.

                      Code:
                      public void something(int x) 
                      { 
                      	int a=x; 
                       
                      	if(a<5)
                      	{ 
                      		System.out.println("if-1: True");
                      	 if(a<3) {
                      	 System.out.println("if-2: True");
                      				} 	 
                      	 else {
                      			 System.out.println("if-2: False");
                      			 }
                      		 System.out.println("a:" +a); 
                      	} 
                      else 
                      	{
                      	 System.out.println("if-1: False");
                      	 if(a<8)
                      	 {
                      	 System.out.println("if-3: True");
                      	 }
                      else {
                      		 System.out.println("if-3: False");
                      	} 
                      	System.out.println("a:" +a);
                      	}
                      }
                      You could make it static and call it directly:

                      Code:
                       public static void main(String[] args) { 
                       int[] values = {1, 2, 3, 4};
                      something(values[1]);
                      }
                      public static void something(int x) 
                      { 
                      	int a=x; 
                       
                      	if(a<5)
                      	{ 
                      		System.out.println("if-1: True");
                      	 if(a<3) {
                      	 System.out.println("if-2: True");
                      				} 	 
                      	 else {
                      			 System.out.println("if-2: False");
                      			 }
                      		 System.out.println("a:" +a); 
                      	} 
                      else 
                      	{
                      	 System.out.println("if-1: False");
                      	 if(a<8)
                      	 {
                      	 System.out.println("if-3: True");
                      	 }
                      else {
                      		 System.out.println("if-3: False");
                      	} 
                      	System.out.println("a:" +a);
                      	}
                      }

                      Comment

                      • shana07
                        Contributor
                        • Jan 2007
                        • 280

                        #12
                        Meaning to say it isn't possible for me to pass int value from other file (TestDriver - is passing values to MyProgram to run the execution)
                        like what you wrote before when both classes do need main?
                        Code:
                        How about this?
                        class MyProgram
                        {    
                         public static void main(String[] args) 
                         { 	
                        	MyProgram program = new MyProgram();
                        	program.something(1);	
                        } 	 	  
                         public void something(int x) 
                        ..........
                         {
                        But this is not passing int value from TestDriver...
                        herm...help please.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by shana07
                          Meaning to say it isn't possible for me to pass int value from other file (TestDriver - is passing values to MyProgram to run the execution)
                          like what you wrote before when both classes do need main?
                          Code:
                          How about this?
                          class MyProgram
                          { 
                          public static void main(String[] args) 
                          { 	
                          	MyProgram program = new MyProgram();
                          	program.something(1);	
                          } 		  
                          public void something(int x) 
                          ..........
                          {
                          But this is not passing int value from TestDriver...
                          herm...help please.
                          Alright now let's have all the programs posted here and a description of what you want to do.

                          Comment

                          • shana07
                            Contributor
                            • Jan 2007
                            • 280

                            #14
                            Thanks...Let say I have two classes - MyProgram & TestDriver now which these two files need main (something to do with testing tool requirement which need main in the program)
                            Code:
                             public static void main (String[] args)
                            Class - TestDriver
                            Function: To pass ‘int a’ to MyProgram one by one from an array.
                            Code:
                            public static void main (String[] args) 
                              {
                                      MyProgram myprogram =new Program();
                                      myprogram.something(1);   
                                      .....}
                            Class – MyProgram
                            Function: fetch a value from the calling program. And this class as I mentioned also need main.
                            Code:
                            public static void main (String[] args) 
                              { 	 
                                /* here I don’t know what to put as it also needs main */
                               }
                            void something(int x) 
                            { 
                            int a=x; 	
                            if(a<5)
                              { 
                            	System.out.println("if-1: True");
                              if(a<3) {
                              System.out.println("if-2: True");
                            			} 	  
                              else {
                            		 System.out.println("if-2: False");
                            		 }
                            	  System.out.println("a:" +a); 
                            }   
                               else 
                            	{
                            	  System.out.println("if-1: False");
                            	 if(a<8)
                            	 {
                            	   System.out.println("if-3: True");
                            	 }
                              else {
                            		  System.out.println("if-3: False");
                            }   
                            System.out.println("a:" +a);
                            }
                            }

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by shana07
                              Thanks...Let say I have two classes - MyProgram & TestDriver now which these two files need main (something to do with testing tool requirement which need main in the program)
                              Code:
                               public static void main (String[] args)


                              Class - TestDriver

                              Function: To pass ‘int a’ to MyProgram one by one from an array.

                              Code:
                              public static void main (String[] args) 
                              
                              {
                              
                              MyProgram myprogram =new Program();
                              
                              myprogram.something(1); 
                              
                              .....}
                              Class – MyProgram

                              Function: fetch a value from the calling program. And this class as I mentioned also need main.

                              Code:
                              public static void main (String[] args) 
                              
                              { 	 
                              
                              /* here I don’t know what to put as it also needs main */
                              
                              }
                              
                              void something(int x) 
                              
                              { 
                              
                              int a=x; 	
                              
                              if(a<5)
                              
                              { 
                              
                              	System.out.println(\"if-1: True\");
                              
                              if(a<3) {
                              
                              System.out.println(\"if-2: True\");
                              
                              			} 	 
                              
                              else {
                              
                              		 System.out.println(\"if-2: False\");
                              
                              		 }
                              
                              	 System.out.println(\"a:\" +a); 
                              
                              } 
                              
                              else 
                              
                              	{
                              
                              	 System.out.println(\"if-1: False\");
                              
                              	 if(a<8)
                              
                              	 {
                              
                              	 System.out.println(\"if-3: True\");
                              
                              	 }
                              
                              else {
                              
                              		 System.out.println(\"if-3: False\");
                              
                              } 
                              
                              System.out.println(\"a:\" +a);
                              
                              }
                              
                              }


                              Why do you have to use two classes? Put the main method and the something method in the same class. Only one main method is run.

                              Comment

                              Working...