DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut

Trending

  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  1. DZone
  2. Coding
  3. Java
  4. Java Quiz 15: Improve Encapsulation of Your Code

Java Quiz 15: Improve Encapsulation of Your Code

Get caught up with the answer to the previous quiz on overloading methods, and try your hand at this quiz, which deals with improving the encapsulation of your code.

By 
Sar Maroof user avatar
Sar Maroof
·
Apr. 11, 18 · Survey/Contest
Likes (5)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

Before we start with this week's puzzle, here is the answer to Java Quiz 14: Overloading Methods.

The method writeValue(1, 2) invokes writeValue(int, int) because 1 and 2 are integers.
The method writeValue(int, int) doesn't exist. Therefore, writeValue(int, double)
is invoked instead of writeValue(int, Integer). The reason is that widening is preferred over boxing/unboxing. The correct answer is: D.

Here is the Java puzzle for today!

If the following code is compiled and run, it writes [David, Emma, Layla, Victoria] to the standard output.

By adding Victoria and removing Mike from the customer's list, it's clear that the class TestCustomer is able to modify the list.

How can we improve the encapsulation of the class Customer to achieve the following goals?

  1. Other classes should be allowed to display the customer's list.
  2. Other classes shouldn't be allowed to remove or add customers to the list.
import java.util.ArrayList;
 

public class TestCustomer
{
  public static void main(String[] args)
  {
    Customer customer = new Customer();
    customer.getCustomers().add("Victoria");
    customer.getCustomers().remove(2);
    System.out.print(customer.getCustomers());
  }
}


class Customer
{
  private ArrayList<String> customers;
  {
    customers = new ArrayList<>();
    customers.add("David");
    customers.add("Emma");
    customers.add("Mike");
    customers.add("Layla");
  }
  public ArrayList<String> getCustomers()
  {
    return customers;
  }
}

The correct answer and its explanation will be included in the next quiz in two weeks! For more Java quizzes, puzzles, and assignments, take a look at my site!

Java (programming language) Encapsulation (networking)

Opinions expressed by DZone contributors are their own.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!