{"id":3969,"date":"2024-09-11T20:49:45","date_gmt":"2024-09-11T15:49:45","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3969"},"modified":"2024-09-11T20:49:49","modified_gmt":"2024-09-11T15:49:49","slug":"understanding-parameterized-functions-in-a-class-java","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/09\/11\/understanding-parameterized-functions-in-a-class-java\/","title":{"rendered":"Understanding Parameterized Functions in Java"},"content":{"rendered":"\n<p>In this tutorial, we will explore the concept of <strong>parameterized functions<\/strong> in a class by using a simple example of a Calculator class. This will help in understanding how to pass values (parameters) to a class and its methods, manage attributes, and perform operations such as addition, subtraction, and multiplication. We will walk through the code step by step to understand its structure and working. <a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/object-oriented-programing-oop\/\" target=\"_blank\" rel=\"noopener\" title=\"\">You can visit the detailed tutorial here. <\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is a Parameterized Function?<\/strong><\/h3>\n\n\n\n<p>A parameterized function is a function that takes input values, known as parameters, to perform some operations. These parameters are passed when calling the function. By making functions parameterized, we can reuse the same function for different inputs, making the code more flexible and dynamic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Class with Parameterized Functions: Calculator Example<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s begin by defining a <code>Calculator<\/code> class that can perform basic arithmetic operations (sum, subtract, and multiplication) on two numbers. We will use <strong>parameters<\/strong> to pass these numbers into the class methods.<\/p>\n\n\n\n<p>Here\u2019s the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Calculator {\n\n    \/\/ Attributes of the class\n    private int a;\n    private int b;\n\n\n    \/\/ Method for sum\n    public int sum(int a, int b) {\n        return a + b;\n    }\n\n    \/\/ Method for subtraction\n    public int subtract(int a, int b) {\n        return a - b;\n    }\n\n    \/\/ Method for multiplication\n    public int multiply(int a, int b) {\n        return a * b;\n    }\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating an instance of the Calculator class with values for a and b\n        Calculator calc = new Calculator();\n\n        \/\/ Calling methods and printing the results\n        System.out.println(\"Sum: \" + calc.sum(10, 5));\n        System.out.println(\"Subtraction: \" + calc.subtract(10, 5));\n        System.out.println(\"Multiplication: \" + calc.multiply(10, 5));\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step-by-Step Explanation<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Class Declaration<\/strong><\/h4>\n\n\n\n<p>The <code>Calculator<\/code> class is declared with two private attributes <code>a<\/code> and <code>b<\/code>, representing the two numbers on which the arithmetic operations will be performed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Calculator {\n    private int a;\n    private int b;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Methods for Arithmetic Operations<\/strong><\/h4>\n\n\n\n<p><strong>Sum<\/strong>: This method returns the sum of the two attributes <code>a<\/code> and <code>b<\/code>. <code>public int sum(int a, int b) { return a + b; }<\/code><\/p>\n\n\n\n<p><strong>Subtraction<\/strong>: This method returns the result of subtracting <code>b<\/code> from <code>a<\/code>. <code>public int subtract(int a, int b) { return a - b; }<\/code><\/p>\n\n\n\n<p><strong>Multiplication<\/strong>: This method multiplies <code>a<\/code> and <code>b<\/code> and returns the result. <code>public int multiply(int a, int b) { return a * b; }<\/code><\/p>\n\n\n\n<p>These methods do not take any parameters because they use the class attributes <code>a<\/code> and <code>b<\/code>, which have already been initialized by the constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Main Method<\/strong><\/h3>\n\n\n\n<p>The <code>main<\/code> method is where the execution of the program begins. Here, we create an instance of the <code>Calculator<\/code> class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Calculator calc = new Calculator();<\/code><\/pre>\n\n\n\n<p>Once the object is created, we can call the methods to perform the operations. For example, to calculate the sum of <code>10<\/code> and <code>5<\/code>, we call the <code>sum()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(\"Sum: \" + calc.sum(10, 5));<\/code><\/pre>\n\n\n\n<p>Similarly, we call the <code>subtract()<\/code> and <code>multiply()<\/code> methods to perform subtraction and multiplication:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(\"Subtraction: \" + calc.subtract(10, 5));\nSystem.out.println(\"Multiplication: \" + calc.multiply(10, 5));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Program Execution and Output<\/strong><\/h3>\n\n\n\n<p>When the program is run, it will display the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 15\nSubtraction: 5\nMultiplication: 50<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>sum(10, 5)<\/code> method adds <code>10 + 5<\/code> and prints <code>15<\/code>.<\/li>\n\n\n\n<li>The <code>subtract(10, 5)<\/code> method subtracts <code>5<\/code> from <code>10<\/code> and prints <code>5<\/code>.<\/li>\n\n\n\n<li>The <code>multiply(10, 5)<\/code> method multiplies <code>10 * 5<\/code> and prints <code>50<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Points to Note:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Parameterized methods<\/strong> allow us to pass values to functions, making the code reusable for different inputs.<\/li>\n\n\n\n<li><strong>Class attributes<\/strong> store the values that can be used across different methods within the class.<\/li>\n\n\n\n<li>The <strong>main method<\/strong> is where the object is created, and the methods of the class are called to perform specific tasks.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><a href=\"https:\/\/www.canva.com\/design\/DAGQdp1V4Zg\/xGjgSt_4kEf96q8Yn2ZqqQ\/view?utm_content=DAGQdp1V4Zg&amp;utm_campaign=designshare&amp;utm_medium=link&amp;utm_source=editor\" target=\"_blank\" rel=\"noopener\" title=\"\">Visit the presentation slides here. <\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will explore the concept of parameterized functions in a class by using a simple example of a Calculator class. This will help in understanding how to pass values (parameters) to a class and its methods, manage attributes, and perform operations such as addition, subtraction, and multiplication. We will walk through the code step by step to understand its structure and working. You can visit the detailed tutorial here. What is a Parameterized Function? A parameterized function&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/09\/11\/understanding-parameterized-functions-in-a-class-java\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3972,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[602],"tags":[],"class_list":["post-3969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-object-oriented-programing-oop"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/09\/Understanding-Parameterized-Functions-in-Java-jpg.webp?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-121","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=3969"}],"version-history":[{"count":5,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3969\/revisions"}],"predecessor-version":[{"id":3975,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3969\/revisions\/3975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3972"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}