Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA 1. Given the following class definition, what are the contents of the fields x and y of the object p ? class MyPoint {

JAVA

1.

Given the following class definition, what are the contents of the fields x and y of the object p ?

class MyPoint {

int x;

int y;

public MyPoint (int x, int y){

x = x;

y = y;

}

---------------------------------------

MyPoint p = new MyPoint( 100, 88 );

2. static and non-static members

What would happen if you tried to compile and run the following code ?

public class Driver

{

private int num;

public static void main (String [] args)

{

num = 10;

System.out.println( num has the value : + num );

}

}

3. When are two objects equal ?

Given the following class definition

class Sheep {

public String name; // yes yes, of course, data should always be

public int id; // private

public Sheep ( String name , int id ){

this.name = name;

this.id = id;

}

}

What would be the outcome of the following code snippet ?

Sheep a = new Sheep( bo, 101 );

Sheep b = new Sheep( beep, 999 );

Sheep c = b;

b.name = bo;

b.id = 101 ;

if( c == b ) System.out.println( c is the same as b );

else System.out.println( c is NOT the same as b );

if( a.equals( b ) System.out.println( a equals b );

else System.out.println( a does NOT equal b )

4. Instances and references

Given the previous definition of class Sheep expanded with a default constructor and a clone() method, we write:

Sheep a = new Sheep( bo, 101 );

Sheep b = new Sheep();

Sheep c = a;

Sheep[] flock1 ;

Sheep[] flock2 = new Sheep [5];

Sheep d = (Sheep) a.clone();

How many Sheep objects have been created ?

5. shallow VS deep copies

Here is a definition of a class Shepherd. A Shepherd has a name and two Sheep members.

class Shepherd {

public String name;

public Sheep s1;

public Sheep s2;

public Shepherd ( String name , Sheep s1, Sheep s2){

this.name = name;

this.s1 = s1;

this.s2 = s2;

}

}

Shepherd Joe = new Shepherd( Joe , new Sheep(Baa, 111), new Sheep(Bla, 222) );

Shepherd Jill = Joe;

Is Jill a shallow or deep copy of Joe ?

Jill.s1 = new Sheep( Jills Sheep, 999);

System.out.println( Joe.s1.name );

What will the above code output ?

5. Cloning

Write a default constructor and a clone() method for the class Shepherd defined above

6. Copy constructors

A copy constructor is a special kind of constructor that takes as a parameter an existing object of the same type and initializes the object under construction with the parameter objects fields.

For example, If the previously defined Shepherd class had a copy constructor, we could have written the following

Shepherd Jill = new Shepherd( Joe ); // where Joe is an already initialized object

This would initialize Jill with the values of Jills fields.

Write a copy constructor for the class Shepherd.

Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

When is it appropriate to show grace toward others?

Answered: 1 week ago