Question

JAVA 1.            Given the following class definition, what are the contents of the fields x and...

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( “Jill’s 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 object’s 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 Jill’s fields.

Write a copy constructor for the class Shepherd.

Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please do understand my concern, as per Chegg guidelines we are supposed to answer the first question if there is more than one question in a post, your downvote effects my career a lot, if I get a downvote for a wrong answer or incomplete answer I will accept it. Please give a positive rating

the value of x will be 100 and y will be 88

Add a comment
Know the answer?
Add Answer to:
JAVA 1.            Given the following class definition, what are the contents of the fields x and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • In java Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following...

    In java Problem: Using the ‘Name.java’ and ‘Student.java’ files, expand both classes to include the following methods: A ‘copy’ constructor, A ‘clone’ method, A ‘finalize’ method, A ‘dispose’ method, and A ‘hashCode’ method. A ‘compareTo’ method Test the upgraded classes using the application ‘TestStudentName.java’ located in ‘Chapter01_Files.zip’. Be sure to include output messages in both the ‘finalize’ method and the ‘dispose’ method. Chapter01 File 'TestStudentName.java' below. // ----------------------------------------------- // TestStudentName.java // ----------------------------------------------- public class TestStudentName {    public static void...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Use inheritance to create a new class AudioRecording based on Recording class that: it will retain...

    Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...

  • Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private...

    Java Programming Answer 60a, 60b, 60c, 60d. Show code & output. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT