Question

In: Computer Science

Question 3 A java source module contains the following class with the static methods main and...

Question 3

  1. A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2):

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 )

                        {

                                    .   .   .

                        }

            }

  1. Write the statement to call procedure1 in method main. (2 pts)
  2. Write the statement to call procedure2 in method main. ( 2 pts)
  1. The source module in section A also contains the following class Question3 with the static method procedure3. (15 pts)

class Question3

            {

                         static void procedure3(void )

                        {

                                    .    .    .

                        }

            }

  1. Write the body of procedure3 which multiplies the values in variable num1 by 5 and stores the result into variable result (note that both variables are static variables defined in the class TestQuestion3).
  2. Write the statement to call procedure3 in method main.
  3. Add to class Question3 the instance method procedure4 that receives as parameter, a one-dimensional array of integer values and then builds and returns another one-dimensional array such that the value of each element is 5 times the corresponding element of the array received as parameter.
  4. Write the statement that is used in method main to call method procedure4 with list1 as the argument and use list2 to receive to array returned by the method (list1 and list2 are defined in method main).

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.

Solutions

Expert Solution

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:


Related Solutions

Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT