QuestionQuestion

1. What is the output of the following Java program? Explain in terms of how parameters are passed in Java.
import java.awt.*;
class PointParameters
{
          public static void main(String [] args)
         {
                int x = 1, y = 1;
                Point p = new Point(x, y), q = new Point(x, y);
               doubleScale(x, y, p, q);
               System.out.println( "(x,y) = " + new Point(x, y) +
               " p = " + p + " q = " + q);
          }
         private static void doubleScale(int x, int y, Point p, Point q)
          {
                x *= 2;
                y *= 2;
                p.x *= 2;
                p.y *= 2;
               q = new Point(x, y);
          }
}

Suppose that a similar program was written in C# in which all the parameters were ref parameters. What would the output of that program be?

2. Examine the following C++ program, in which a IntList class is defined that contains an overloaded [] operator. What is the output of this program?
#include <iostream>
using namespace std;
class IntList
{
      private:
      int list[1];
      public:
      IntList() {list[0] = 0;}
      int& operator[] (const int index) {return list[index];}
};

int main()
{
       IntList list;
       cout << list[0] << endl;
       list[0] = 1;
       cout << list[0] << endl;
       return 0;
}

Notice that the overloaded [] operator returns a reference. Change the [] operator to return a value instead and explain what happens in that case. Explain why the ability to return by reference is essential to be able to implement such an operator. Can anything similar be done in Java?

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

Homework about Passing Parameters in Java and C#
    $20.00 for this solution

    PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

    Find A Tutor

    View available Java Programming Tutors

    Get College Homework Help.

    Are you sure you don't want to upload any files?

    Fast tutor response requires as much info as possible.

    Decision:
    Upload a file
    Continue without uploading

    SUBMIT YOUR HOMEWORK
    We couldn't find that subject.
    Please select the best match from the list below.

    We'll send you an email right away. If it's not in your inbox, check your spam folder.

    • 1
    • 2
    • 3
    Live Chats