014-008-011

Implementing clone() with Deep Copy

Hard

Problem Description

Implementing clone() with Deep Copy

In this problem: You will implement the clone() method from the Object class in a ScoreBoard class that contains an array field, achieving a deep copy.

Learning Objective: Understand the difference between shallow and deep copy, and implement correct cloning for objects containing arrays

Overview

When overriding clone(), reference type fields (such as arrays) in a shallow copy share the same array as the original. A deep copy creates a new copy of the array, producing an independent object.

Specifications

  • ScoreBoard class: name (String), scores (int[]) fields
  • Implement the Cloneable interface
  • Override clone() with deep copy of scores array
  • Verify that original and clone scores are independent

Output Format

Original: Alice [90, 85, 92]
Clone: Alice [90, 85, 92]
After modification:
Original: Alice [90, 85, 92]
Clone: Alice [90, 100, 92]

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up