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...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
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...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module public static void score_search(int s,int score[]) { // Initialise flag as 0 int flag=0; // Looping till the end of the array for(int j=0;j<10;j++) { // If the element is found in the array if(s==score[j]) { // Update flag to 1 flag=1; } } // In case flag is 1 element is found if(flag==1) { System.out.println("golf score found"); } // // In case flag...
Java homework question: Explain how class (static) variables and methods differ from their instance counterparts. Give...
Java homework question: Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT