Scalar Multiplication of a Point

# 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.

1. public class Point{
2.  private int x;
3.  private int y;
4.  Point() {
5.   this.x = 0;
6.   this.y = 0;
7.  }
8.  Point(int xInit, int yInit) {
9.   this.x = xInit;
10.   this.y = yInit;
11.  }
12.  private void setX(int xCoord) {
13.   this.x = xCoord;
14.  }
15.  private void setY(int yCoord) {
16.   this.y = yCoord;
17.  }
18.  private int getX() {
19.   return this.x;
20.  }
21.  private int getY() {
22.   return this.y;
23.  }
24.  private void print() {
25.   System.out.println("(" + this.x + "," + this.y+ ")");
26.  }
27.  private boolean isOrigin() {
28.   if((this.x==0)&&(this.y==0))
29.    return true;
30.   else
31.    return false;
32.  }
34.   if(isOrigin())
35.    return "origin";
36.   else if((this.x < 0)&&(this.y < 0))
38.   else if((this.x < 0)&&(this.y == 0))
39.    return "-ve X-Axis";
40.   else if((this.x == 0)&&(this.y < 0))
41.    return "-ve Y-Axis";
42.   else if((this.x > 0)&&(this.y > 0))
44.   else if((this.x > 0)&&(this.y < 0))
46.   else if((this.x < 0)&&(this.y > 0))
48.   else if((this.x > 0)&&(this.y == 0))
49.    return "+ve X-Axis";
50.   else if((this.x == 0)&&(this.y > 0))
51.    return "+ve Y-Axis";
52.  }
53.  public void scalarMultiply(int c) {
54.   x = c * x;
55.   y = c * y;
56.  }
57.  public void scalarMultiplyNonMutable(int c) {
58.   Point s = new Point(c*x,c*y);
59.   return s;
60.  }
61. public class Driver {
62.  public static void main(String[]){
63.   Point p0 = new Point(3,8);
64.   p0.scalarMultiply(5);
65.   p0.print();
66.   Point p1 = new Point(4,5);
67.   Point q = p1.scalarMultiplyNonMutable(6);
68.   q.print();
69.  }

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.