w3resource

Creating a Java Point Class with Overloaded Constructors

Java Constructor: Exercise-8 with Solution

Overloading Constructors with Different Data Types
Write a Java program to create a class called Point with instance variables x and y. Implement overloaded constructors:

  • One constructor takes int parameters.
  • Another constructor takes double parameters.
  • Print the values of the variables for each constructor.

Sample Solution:

Java Code:

Point.java

// Define the Point class
public class Point {
    // Private instance variables
    private int x;
    private int y;

    // Constructor that takes int parameters
    public Point(int x, int y) {
        // Initialize instance variables
        this.x = x;
        this.y = y;
    }

    // Constructor that takes double parameters
    public Point(double x, double y) {
        // Initialize instance variables by casting double to int
        this.x = (int) x;
        this.y = (int) y;
    }

    // Method to print the values of x and y
    public void printPoint() {
        System.out.println("Point (x, y): (" + x + ", " + y + ")");
    }

    // Main method to test the Point class
    public static void main(String[] args) {
        // Create a Point object using the int constructor
        Point point1 = new Point(4, 5);
        // Print the values of point1
        point1.printPoint();

        // Create a Point object using the double constructor
        Point point2 = new Point(4.5, 5.5);
        // Print the values of point2
        point2.printPoint();
    }
}

Output:

Point (x, y): (4, 5)
Point (x, y): (4, 5)

Explanation:

  • Define the Point class:
    • The Point class is defined with the keyword class.
  • Private instance variables:
    • The Point class has two private instance variables: x and y, both of type int.
  • Constructor that takes int parameters:
    • A constructor Point(int x, int y) is defined.
    • Inside this constructor, the instance variables x and y are initialized with the provided integer parameters.
  • Constructor that takes double parameters:
    • Another constructor Point(double x, double y) is defined.
    • Inside this constructor, the instance variables x and y are initialized with the provided double parameters, cast to int.
  • Method to print the values of x and y:
    • The printPoint method is defined to print the values of the instance variables x and y.
  • Main method:
    • The main method is defined to test the Point class.
    • Using int constructor: A Point object (point1) is created using the constructor that takes int parameters. The values of point1 are printed using the printPoint method.
    • Using double constructor: A Point object (point2) is created using the constructor that takes double parameters. The values of point2 are printed using the printPoint method.

Note on Constructors:

In the above exercise, the constructors for the Point class work by:

  • Overloaded Constructors: Providing multiple constructors allows the class to be instantiated with different parameter types (int and double), offering flexibility in object creation.
  • Data Initialization: Each constructor initializes the instance variables x and y according to the provided parameters, demonstrating the concept of constructor overloading in Java. This ensures that the Point class can handle different types of input while maintaining consistent initialization logic.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Constructor Previous: Creating a Java Car Class with Parameterized Constructor and Default Values.
Java Constructor Next: Creating a Java Classroom Class with Array Initialization.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/constructor/java-constructor-exercise-8.php