Objective
Write a java program about the topic Scalar multiplication of a point in object-oriented program.
The Theory
Scalar multiplication of a point is a mathematical operation performed on a point in object-oriented programming, specifically in the context of geometric objects or vector spaces. It involves multiplying the coordinates of a point by a scalar value, resulting in a new point with modified coordinates.
Sample examples are given below.
- public class Point{
- private int x;
- private int y;
- Point() {
- this.x = 0;
- this.y = 0;
- }
- Point(int xInit, int yInit) {
- this.x = xInit;
- this.y = yInit;
- }
- private void setX(int xCoord) {
- this.x = xCoord;
- }
- private void setY(int yCoord) {
- this.y = yCoord;
- }
- private int getX() {
- return this.x;
- }
- private int getY() {
- return this.y;
- }
- private void print() {
- System.out.println("(" + this.x + "," + this.y+ ")");
- }
- private boolean isOrigin() {
- if((this.x==0)&&(this.y==0))
- return true;
- else
- return false;
- }
- public int whichQuadrant() {
- if(isOrigin())
- return "origin";
- else if((this.x < 0)&&(this.y < 0))
- return "quadrant 3";
- else if((this.x < 0)&&(this.y == 0))
- return "-ve X-Axis";
- else if((this.x == 0)&&(this.y < 0))
- return "-ve Y-Axis";
- else if((this.x > 0)&&(this.y > 0))
- return "quadrant 1";
- else if((this.x > 0)&&(this.y < 0))
- return "quadrant 4";
- else if((this.x < 0)&&(this.y > 0))
- return "quadrant 2";
- else if((this.x > 0)&&(this.y == 0))
- return "+ve X-Axis";
- else if((this.x == 0)&&(this.y > 0))
- return "+ve Y-Axis";
- }
- public void scalarMultiply(int c) {
- x = c * x;
- y = c * y;
- }
- public void scalarMultiplyNonMutable(int c) {
- Point s = new Point(c*x,c*y);
- return s;
- }
- }
- public class Driver {
- public static void main(String[]){
- Point p0 = new Point(3,8);
- p0.scalarMultiply(5);
- p0.print();
- Point p1 = new Point(4,5);
- Point q = p1.scalarMultiplyNonMutable(6);
- q.print();
- }
- }
The above code is written in Java and consists of two classes: Point and Driver. Let's break down the code step by step:
Point class:
- The Point class represents a point in a two-dimensional coordinate system.
- It has two private instance variables, x and y, which store the coordinates of the point.
- The class provides two constructors: a default constructor (Point()) that initializes the point at the origin (0, 0), and a parameterized constructor (Point(int xInit, int yInit)) that allows you to initialize the point with specific coordinates.
- Private methods like setX(), setY(), getX(), and getY() are provided to manipulate and retrieve the x and y values.
- The print() method is used to display the coordinates of the point in the format (x, y).
- The isOrigin() method checks if the point is located at the origin (0, 0) and returns a boolean value.
- The whichQuadrant() method determines in which quadrant the point is located or if it lies on the axes, and returns a string representing the quadrant or axis.
- The scalarMultiply() method takes an integer c as a parameter and multiplies the x and y coordinates of the point by c, effectively scaling the point.
- The scalarMultiplyNonMutable() method is similar to scalarMultiply(), but instead of modifying the coordinates of the existing point, it creates a new Point object s with the scaled coordinates and returns it.
Driver class:
- The Driver class contains the main() method, which serves as the entry point of the program.
- Inside the main() method:
- An instance of the Point class, p0, is created with initial coordinates (3, 8).
- The scalarMultiply() method is called on p0, multiplying its coordinates by 5 and updating the values.
- The print() method is called on p0 to display the updated coordinates.
- Another instance of the Point class, p1, is created with initial coordinates (4, 5).
- The scalarMultiplyNonMutable() method is called on p1, multiplying its coordinates by 6 and creating a new Point object, q, with the scaled coordinates.
- The print() method is called on q to display its coordinates.
- The execution starts from the main() method in the Driver class, and the program creates points, performs operations on them, and prints the result.
Learning Outcomes
- By implementing the scalar multiplication of a point in an object-oriented manner, the code becomes more structured, maintainable, and extensible. It allows for the creation of Point objects, encapsulates their data and behavior, and provides well-defined interfaces to manipulate and analyze the points, including scalar multiplication.
- Problem-solving skills: Scalar multiplication of a point is often a key component in solving various computational problems, such as transforming shapes or implementing algorithms. By mastering this concept, you develop problem-solving skills that can be applied to a wide range of programming tasks.