Defining Printers Printing the Output

Our Objective 

To implement a program that Defining Printers Printing the Output. 

 

The Theory 

Class: A class is a blueprint or a template for creating objects in OOP. It defines the properties (attributes) and behaviors (methods) that objects of the class will possess. Here are some key points about classes:

Attributes: Attributes are variables that store data within a class. They represent the characteristics or properties of objects created from the class.

Methods: Methods are functions defined within a class. They define the behaviors or actions that objects of the class can perform.

Object: An object is an instance of a class. It is a concrete, real-world entity created based on the class blueprint. You can create multiple objects from the same class.

Encapsulation: Encapsulation is one of the four fundamental principles of OOP (the others being inheritance, polymorphism, and abstraction). It is the concept of bundling data (attributes) and the methods that operate on that data (behaviors) into a single unit, which is the class. Encapsulation provides several benefits:

Data Hiding: By defining attributes as private (usually by using naming conventions like _variable_name or using properties), you can control access to them from outside the class. This prevents direct modification of class data and enforces data integrity.

Modularity: Encapsulation promotes modularity by encapsulating related data and methods within a class. This makes code more organized and easier to maintain.

Flexibility: It allows you to change the internal implementation of a class without affecting the code that uses the class. This is known as information hiding.

Sample example is given below:


public class Point{
 private int x;
 private int y;
 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+ ")");
 }
}
public class Driver {
 public static void main(String[]){
  Point p = new Point();
  p.setX(2);
  p.setY(3);
  p.print();
 }
}:

  1. Create a class named "Point." 
  2. Declare private integer variables 'x' and 'y' to store the coordinates of the point. 
  3. Define a private method named "setX" that takes an integer parameter 'xCoord' and sets the value of 'x' to 'xCoord'. 
  4. Define a private method named "setY" that takes an integer parameter 'yCoord' and sets the value of 'y' to 'yCoord'. 
  5. Define a private method named "getX" that returns the value of 'x'. 
  6. Define a private method named "getY" that returns the value of 'y'. 
  7. Define a private method named "print" that prints the coordinates of the point in the format "(x, y)" using the values of 'x' and 'y'. 
  8. Create a class named "Driver" with a public static method named "main" that takes a String array as a parameter. 
  9. Inside the "main" method: 
  10. Create an instance of the "Point" class named "p". 
  11. Call the "setX" method of the "p" object and pass 2 as the argument to set the value of 'x'. 
  12. Call the "setY" method of the "p" object and pass 3 as the argument to set the value of 'y'. 
  13. Call the "print" method of the "p" object to print the coordinates of the point. 
  14. End the main method. 
  15. End the Driver class. 

 

Learning Outcomes

  • Object-Oriented Programming: The code illustrates the principles of object-oriented programming by defining a Point class with private instance variables (x and y) and encapsulating them with getter and setter methods. 
  • Encapsulation: The private instance variables x and y are encapsulated within the Point class, meaning they are not directly accessible from outside the class. Instead, getter and setter methods (getX, getY, setX, setY) are provided to control access to these variables. 
  • Data Hiding: By making the instance variables private, the code achieves data hiding, which prevents direct modification of the x and y values from external code.