473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to select an input from a set of inputs?

281 100+
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.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); }

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....
Feb 1 '07 #1
18 1712
r035198x
13,262 8TB
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.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); }

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.
Feb 1 '07 #2
shana07
281 100+
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 :
Expand|Select|Wrap|Line Numbers
  1. int input[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
The TestDriver suppose to run a program under test - MyProgram.java.
Expand|Select|Wrap|Line Numbers
  1.  int a=1;                 
  2.  if(a<5)
  3.   { 
  4.     System.out.println("if-1: True");
  5.   if(a<3) {
  6.       System.out.println("if-2: True");
  7.             }                  
  8.   else {
  9.          System.out.println("if-2: False");
  10.          }
  11.       System.out.println("a:" +a);         
  12. }   
  13.    else 
  14.     {
  15.       System.out.println("if-1: False");
  16.      if(a<8)
  17.      {
  18.        System.out.println("if-3: True");
  19.      }
  20.   else {
  21.           System.out.println("if-3: False");
  22.  }                  
  23. System.out.println("a:" +a);
  24. }
  25.  
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
Feb 1 '07 #3
hirak1984
316 100+
May be this code will help you :

Expand|Select|Wrap|Line Numbers
  1.  public class A1 {
  2. public static void main(String args[])
  3. {
  4. A2 newA2=new A2();
  5.  int a[]={1,2,3,4,5,6,7,8,9};
  6. int i=a.length;
  7. for(int j=0;j<i;j++)
  8.  newA2.PrintA(a[j]);
  9. }
  10. }
  11.  
then the second class
Expand|Select|Wrap|Line Numbers
  1.  public class A2 {
  2. public void PrintA (int a)
  3. {
  4.  if(a<5)
  5.    { 
  6.      System.out.println("if-1: True");
  7.    if(a<3) {
  8.     System.out.println("if-2: True");
  9.              }         
  10.    else {
  11.           System.out.println("if-2: False");
  12.           }
  13.        System.out.println("a:" +a);   
  14.  }   
  15.     else 
  16.      {
  17.        System.out.println("if-1: False");
  18.       if(a<8)
  19.       {
  20.         System.out.println("if-3: True");
  21.       }
  22.    else {
  23.            System.out.println("if-3: False");
  24.   }      
  25.  System.out.println("a:" +a);
  26.  }
  27. }
  28. }
  29.  
Feb 1 '07 #4
horace1
1,510 Expert 1GB
are these seperate programs ? if so you could pass the parameter a as via the main() parameter list, e.g.
Expand|Select|Wrap|Line Numbers
  1.      int a=1;          // value to pass to MyProgram
  2.      // get runtime environment and execute child process
  3.      Runtime systemShell = Runtime.getRuntime();
  4.      Process output = systemShell.exec("java MyProgram " + a);
  5.  
Feb 1 '07 #5
r035198x
13,262 8TB
How about

Expand|Select|Wrap|Line Numbers
  1.  void something(int x) { 
  2. int a=x;     
  3. if(a<5)
  4.   { 
  5.     System.out.println("if-1: True");
  6.   if(a<3) {
  7.   System.out.println("if-2: True");
  8.             }       
  9.   else {
  10.          System.out.println("if-2: False");
  11.          }
  12.       System.out.println("a:" +a); 
  13. }   
  14.    else 
  15.     {
  16.       System.out.println("if-1: False");
  17.      if(a<8)
  18.      {
  19.        System.out.println("if-3: True");
  20.      }
  21.   else {
  22.           System.out.println("if-3: False");
  23. }   
  24. System.out.println("a:" +a);
  25. }
  26. }
  27.  
Now since all this is now in a method, you can now call the method with any argument you want starting 1 as
Expand|Select|Wrap|Line Numbers
  1.  something(1);
and for any other entry in your array using
Expand|Select|Wrap|Line Numbers
  1.  something(array[index]);
Will this work for you?
Feb 1 '07 #6
shana07
281 100+
May be this code will help you :
Yes, it really helps. Many thanks bro...
Feb 1 '07 #7
shana07
281 100+
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.
Feb 1 '07 #8
shana07
281 100+
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
Feb 1 '07 #9
shana07
281 100+
Okay. I have a question to be asked here.
MyProgram file now also NEED 'main'
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. public void something(int x) 
  2.  { 
  3.     int a=x; 
  4.  
  5.     if(a<5)
  6.     { 
  7.         System.out.println("if-1: True");
  8.       if(a<3) {
  9.       System.out.println("if-2: True");
  10.                 }       
  11.       else {
  12.              System.out.println("if-2: False");
  13.              }
  14.           System.out.println("a:" +a); 
  15.     }   
  16.    else 
  17.     {
  18.       System.out.println("if-1: False");
  19.      if(a<8)
  20.      {
  21.        System.out.println("if-3: True");
  22.      }
  23.   else {
  24.           System.out.println("if-3: False");
  25.     }   
  26.     System.out.println("a:" +a);
  27.     }
  28.   }
  29.  
Feb 1 '07 #10
r035198x
13,262 8TB
Okay. I have a question to be asked here.
MyProgram file now also NEED 'main'
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. public void something(int x) 
  2.     int a=x; 
  3.  
  4.     if(a<5)
  5.     { 
  6.         System.out.println("if-1: True");
  7.      if(a<3) {
  8.      System.out.println("if-2: True");
  9.                 }      
  10.      else {
  11.              System.out.println("if-2: False");
  12.              }
  13.          System.out.println("a:" +a); 
  14.     } 
  15. else 
  16.     {
  17.      System.out.println("if-1: False");
  18.      if(a<8)
  19.      {
  20.      System.out.println("if-3: True");
  21.      }
  22. else {
  23.          System.out.println("if-3: False");
  24.     } 
  25.     System.out.println("a:" +a);
  26.     }
  27. }
  28.  
You could make it static and call it directly:

Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) { 
  2.  int[] values = {1, 2, 3, 4};
  3. something(values[1]);
  4. }
  5. public static void something(int x) 
  6.     int a=x; 
  7.  
  8.     if(a<5)
  9.     { 
  10.         System.out.println("if-1: True");
  11.      if(a<3) {
  12.      System.out.println("if-2: True");
  13.                 }      
  14.      else {
  15.              System.out.println("if-2: False");
  16.              }
  17.          System.out.println("a:" +a); 
  18.     } 
  19. else 
  20.     {
  21.      System.out.println("if-1: False");
  22.      if(a<8)
  23.      {
  24.      System.out.println("if-3: True");
  25.      }
  26. else {
  27.          System.out.println("if-3: False");
  28.     } 
  29.     System.out.println("a:" +a);
  30.     }
  31. }
  32.  
Feb 2 '07 #11
shana07
281 100+
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?
Expand|Select|Wrap|Line Numbers
  1. How about this?
  2. class MyProgram
  3. {    
  4.  public static void main(String[] args) 
  5.  {     
  6.     MyProgram program = new MyProgram();
  7.     program.something(1);    
  8. }            
  9.  public void something(int x) 
  10. ..........
  11.  { 
But this is not passing int value from TestDriver...
herm...help please.
Feb 2 '07 #12
r035198x
13,262 8TB
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?
Expand|Select|Wrap|Line Numbers
  1. How about this?
  2. class MyProgram
  3. public static void main(String[] args) 
  4. {     
  5.     MyProgram program = new MyProgram();
  6.     program.something(1);    
  7. }           
  8. public void something(int x) 
  9. ..........
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.
Feb 2 '07 #13
shana07
281 100+
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)
Expand|Select|Wrap|Line Numbers
  1.  public static void main (String[] args) 
Class - TestDriver
Function: To pass ‘int a’ to MyProgram one by one from an array.
Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args) 
  2.   {
  3.           MyProgram myprogram =new Program();
  4.           myprogram.something(1);   
  5.           .....}
  6.  
Class – MyProgram
Function: fetch a value from the calling program. And this class as I mentioned also need main.
Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args) 
  2.   {      
  3.     /* here I don’t know what to put as it also needs main */
  4.    }
  5. void something(int x) 
  6. int a=x;     
  7. if(a<5)
  8.   { 
  9.     System.out.println("if-1: True");
  10.   if(a<3) {
  11.   System.out.println("if-2: True");
  12.             }       
  13.   else {
  14.          System.out.println("if-2: False");
  15.          }
  16.       System.out.println("a:" +a); 
  17. }   
  18.    else 
  19.     {
  20.       System.out.println("if-1: False");
  21.      if(a<8)
  22.      {
  23.        System.out.println("if-3: True");
  24.      }
  25.   else {
  26.           System.out.println("if-3: False");
  27. }   
  28. System.out.println("a:" +a);
  29. }
  30. }
  31.  
Feb 2 '07 #14
r035198x
13,262 8TB
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)
Expand|Select|Wrap|Line Numbers
  1.  public static void main (String[] args) 


Class - TestDriver

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

Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args) 
  2.  
  3. {
  4.  
  5. MyProgram myprogram =new Program();
  6.  
  7. myprogram.something(1); 
  8.  
  9. .....}
  10.  
  11.  
Class – MyProgram

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

Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args) 
  2.  
  3. {      
  4.  
  5. /* here I don’t know what to put as it also needs main */
  6.  
  7. }
  8.  
  9. void something(int x) 
  10.  
  11.  
  12. int a=x;     
  13.  
  14. if(a<5)
  15.  
  16.  
  17.     System.out.println(\"if-1: True\");
  18.  
  19. if(a<3) {
  20.  
  21. System.out.println(\"if-2: True\");
  22.  
  23.             }      
  24.  
  25. else {
  26.  
  27.          System.out.println(\"if-2: False\");
  28.  
  29.          }
  30.  
  31.      System.out.println(\"a:\" +a); 
  32.  
  33.  
  34. else 
  35.  
  36.     {
  37.  
  38.      System.out.println(\"if-1: False\");
  39.  
  40.      if(a<8)
  41.  
  42.      {
  43.  
  44.      System.out.println(\"if-3: True\");
  45.  
  46.      }
  47.  
  48. else {
  49.  
  50.          System.out.println(\"if-3: False\");
  51.  
  52.  
  53. System.out.println(\"a:\" +a);
  54.  
  55. }
  56.  
  57. }
  58.  
  59.  


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.
Feb 2 '07 #15
shana07
281 100+
Alright bro...Now I've understood. Meaning to say there is only one 'main' if involve two classes. Many thanks.
Feb 3 '07 #16
shana07
281 100+
Allow me to continue additional Q in this topic..need someone to guide me please.

Firstly, the classes & methods used are still the same - TestDriver.java & MyProgram.java. TestDriver is passing int value 'a' to MyProgram. Because a testing tool that currently I'm using needs TestDriver & MyProgram to have 'main' . How if I set an array of 'int a' stored in a file? Back to my first post - anything to do with serialization?

e.g.
First TestDriver pass int a=1 to MyProgram and do some testing stuff here. Then I need to select an int a (other) from the file (which doing by TestDriver) to run MyProgram...

Expand|Select|Wrap|Line Numbers
  1.        int[] values = {1, 2, 3, 4};
  2.     something(values[1]);
Feb 3 '07 #17
r035198x
13,262 8TB
Allow me to continue additional Q in this topic..need someone to guide me please.







Firstly, the classes & methods used are still the same - TestDriver.java & MyProgram.java. TestDriver is passing int value \\\'a\\\' to MyProgram. Because a testing tool that currently I\\\'m using needs TestDriver & MyProgram to have \\\'main\\\' . How if I set an array of \\\'int a\\\' stored in a file? Back to my first post - anything to do with serialization?







e.g.



First TestDriver pass int a=1 to MyProgram and do some testing stuff here. Then I need to select an int a (other) from the file (which doing by TestDriver) to run MyProgram...







Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. int[] values = {1, 2, 3, 4};
  5.  
  6.  
  7.  
  8.     something(values[1]);






You can write the array to a file if you want but see if this will not work here.







Expand|Select|Wrap|Line Numbers
  1.  class A { 
  2.  
  3.  
  4.  
  5. int[] a = new int[20]; //class A contains an array that is required by class B
  6.  
  7.  
  8.  
  9. }
  10.  
  11.  




Then



Expand|Select|Wrap|Line Numbers
  1.  class B { 
  2.  
  3.  
  4.  
  5. int[] x;
  6.  
  7.  
  8.  
  9. B(A a) { //Pass an A to B
  10.  
  11.  
  12.  
  13. x = a.a;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. something(x[1]);//now use the array from A in B.
  21.  
  22.  
Feb 5 '07 #18
shana07
281 100+
I've tried your suggestion as above. got error but let it be first...I'll inform again about it.
I have one Q here, I ran a program with an error:
Expand|Select|Wrap|Line Numbers
  1. class Mode has no static field {1
my code is:
Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.  
  4.  public static void main(String[] args) 
  5.  {
  6.      int a = TestDriver.getInt( 0, 4); //FETCHING FROM ONE CLASS
  7.  
  8.      if(a<5)
  9.     { 
  10.         System.out.println("if-1: True");
  11.        ..........
  12.  ......
  13.  
What's that mean....
thank u
Feb 6 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
7
by: | last post by:
I can't get IE 6 to read the values in my <SELECT..> data entry fields. Netscape 7 and Opera see them, and IE will pass the values to the database, but the javascript validation script gets a null...
3
by: Adam Toline | last post by:
In reference to the following: http://www.bellecose.com/form.htm At the top of each column there is a box for "All". When one is checked I need to check all of (and only) those boxes...
3
by: Doug | last post by:
Hi I posted an hour ago from a different pc - about calling a form from another form. now i am having trouble passing the value from a text box (called tb.entryData), from the form called...
6
by: A_M_IS | last post by:
Hello group, hope to anybodys help on my temporary blackout. (Using Access 2003 on XP Win.) I know how to create and edit temporary query recordset, then I can set this data source as my form...
6
by: Vortexmind | last post by:
Hi all I was wondering if this is possible in Javascript. I want to make a bookmarklet. When a user launches it, it tells the user to select an element in the page (for example a textarea) with...
3
by: gregpinero | last post by:
Would someone mind tell me what I am doing wrong here? I want to find all of the input elements within a div tag with id newID and blank out the values in the inputs. Nothing seems to happen when...
18
by: Rose4msm | last post by:
hello can any one please help me i have a test and i need to study the java scripts, i know that i have to use arrays but im still a bit confuse the question is to ask the user how many data items...
4
by: shapper | last post by:
Hello, I am trying to style my form inputs, textareas and selects as follows: input, select, textarea { border: solid 6px #ECF0F9; color: #252525; font: normal 0.75em Verdana, Geneva,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.