In: Computer Science
Question 3
public class TestQuestion3
{
static int result, num1 = 10;
public static void Main( String [ ] args )
{
int [ ] list1 = { 2, 4, 6, 8, 10}, list2;
. . .
}
static void procedure1( void )
{
. . .
}
void procedure2( void )
{
. . .
}
}
class Question3
{
static void procedure3(void )
{
. . .
}
}
Question 4 (6 pts)
Write a program that is executed with a command line argument consisting of an integer value and a double precision value (example: java <program-name> 50 10.25). This program multiplies the two values and prints the result.
Question 3:
Program code:
public class TestQuestion3
{
static int result, num1 = 10;
public static void main(String [] args)
{
int [ ] list1 = { 2, 4, 6, 8, 10}, list2;
procedure1();
//Static method calling inside the class
TestQuestion3 obj=new
TestQuestion3(); //Creation of object
of same class
obj.procedure2();
//Normal method calling inside the class
Question3.procedure3();
//Static method calling outside the class
list2=Question3.procedure4(list1);
//Static method calling outside the class
System.out.println("\nEntered list is...:");
//Printing arrays
for(int i=0;i<list1.length;i++)
System.out.print("\t"+list1[i]);
System.out.println("\nList returned from procedure 4 is..:");
for(int i=0;i<list1.length;i++)
System.out.print("\t"+list2[i]);
}
static void procedure1() //Body of procedure 1
{
System.out.println("Procedure1 is called....");
}
void procedure2() //Body of procedure 2
{
System.out.println("Procedure2 is called.....");
}
}
class Question3
{
static void
procedure3()
//Body of procedure 3
{
System.out.println("Procedure3 is called....");
}
static int[] procedure4( int array[]) //Body of procedure 4
{
System.out.println("Procedure4 is called....");
int array2[]=new int[array.length];
for(int i=0;i<array.length;i++)
array2[i]=array[i]*5;
return array2;
}
}
Output:
Question 4:
Program code:
public class Question4
{
public static void main(String [] args)
{
int integerVal=Integer.parseInt(args[0]);
double doubleVal=Double.parseDouble(args[1]),res;
res=integerVal*doubleVal;
System.out.println(integerVal+" * "+doubleVal+" = "+res);
}
}
Output: