|
| 1 | +package sample; |
| 2 | + |
| 3 | +//This program will test the constructor method and "this" keyword. |
| 4 | + |
| 5 | +class People { |
| 6 | + |
| 7 | + // Need to use the set methods to set the values for the variables. |
| 8 | + // Else, you can provide the values while creating the variables. |
| 9 | + // There cannot be ny code inside the class. |
| 10 | + |
| 11 | + // The following are called "Instance variable". |
| 12 | + String name; |
| 13 | + int age; |
| 14 | + double weight; |
| 15 | + double height; |
| 16 | + String address; |
| 17 | + |
| 18 | + // Using constructors. |
| 19 | + // Constructors should have the same name as the class name. |
| 20 | + // Constructors will be called by default whenever a new object of the class is created. |
| 21 | + // Constructors can have multiple parameters. |
| 22 | + // Constructors don't need to mention any return parameters like void, int, etc. |
| 23 | + // One constructor can call another constructor on its |
| 24 | + // first line by passing a different set of parameters. |
| 25 | + |
| 26 | + // Default constructor |
| 27 | + public People() { |
| 28 | + System.out.println("Default constructor setting default values."); |
| 29 | + this.name = "Rocky Bhai"; |
| 30 | + this.age = 42; |
| 31 | + this.weight = 78.0; |
| 32 | + this.height = 5.4; |
| 33 | + this.address = "Bhandup West, Mumbai"; |
| 34 | + } |
| 35 | + |
| 36 | + // Constructor with one argument |
| 37 | + public People(String name) { |
| 38 | + System.out.println("Constructor with one argument: Name"); |
| 39 | + this.name = name; |
| 40 | + } |
| 41 | + |
| 42 | + // Constructor with three arguments |
| 43 | + public People(String name, int age, double height) { |
| 44 | + // Will call the default constructor - People() |
| 45 | + // Calling another constructor should always be done on the first line. |
| 46 | + this(); |
| 47 | + |
| 48 | + System.out.println("Constructor with three arguments: Name, age and height"); |
| 49 | + this.name = name; |
| 50 | + this.age = age; |
| 51 | + this.height = height; |
| 52 | + } |
| 53 | + |
| 54 | + // Constructor with two arguments |
| 55 | + public People(double weight, String address) { |
| 56 | + // Calling another constructor with three arguments. |
| 57 | + this("Sachin Tendulkar", 36, 5.7); |
| 58 | + this.weight = weight; |
| 59 | + this.address = address; |
| 60 | + } |
| 61 | + |
| 62 | + // Setters: Using the set method to set the value of the variable. |
| 63 | + |
| 64 | + public void setName(String name) { |
| 65 | + this.name = name; |
| 66 | + } |
| 67 | + |
| 68 | + public void setAge(int age) { |
| 69 | + // this.age refers to the age variable which is set at the class level. |
| 70 | + // "this" keyword is used when two variables have the same name. |
| 71 | + this.age = age; |
| 72 | + } |
| 73 | + |
| 74 | + public void setWeight(double weight) { |
| 75 | + // weight is a local variable. |
| 76 | + // Local variable is providing value to instance variable. |
| 77 | + this.weight = weight; |
| 78 | + } |
| 79 | + |
| 80 | + public void setHeight(double height) { |
| 81 | + this.height = height; |
| 82 | + } |
| 83 | + |
| 84 | + public void setAddress(String address) { |
| 85 | + this.address = address; |
| 86 | + } |
| 87 | + |
| 88 | + // Getters: Using the get method to give out value of the variable. |
| 89 | + |
| 90 | + public String getName() { |
| 91 | + return this.name; |
| 92 | + } |
| 93 | + |
| 94 | + public int getAge() { |
| 95 | + // this.age refers to the age variable which is set at the class level. |
| 96 | + // "this" keyword is used when two variables have the same name. |
| 97 | + return this.age; |
| 98 | + } |
| 99 | + |
| 100 | + public double getWeight() { |
| 101 | + return this.weight; |
| 102 | + } |
| 103 | + |
| 104 | + public double getHeight() { |
| 105 | + return height; // Ignoring this keyword as it is referring to the variable within the class. |
| 106 | + } |
| 107 | + |
| 108 | + public String getAddress() { |
| 109 | + return address; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +public class ConstructorMethod01 { |
| 114 | + |
| 115 | + public static void main(String[] args) { |
| 116 | + |
| 117 | + // This will invoke the default constructor. |
| 118 | + People p1 = new People(); |
| 119 | + p1.name = "Thomas Edison"; |
| 120 | + p1.setAge(32); |
| 121 | + p1.setWeight(65); |
| 122 | + p1.setHeight(5.5); |
| 123 | + p1.setAddress("Lokhandwala Complex, Mumbai, India"); |
| 124 | + |
| 125 | + System.out.printf("Name: %s \n", p1.getName()); |
| 126 | + System.out.printf("Age: %d (years) \n", p1.getAge()); |
| 127 | + System.out.printf("Weight : %.2f (Kgs) \n", p1.getWeight()); |
| 128 | + System.out.printf("Height : %.2f (Feet) \n", p1.getHeight()); |
| 129 | + System.out.printf("Address: %s \n", p1.getAddress()); |
| 130 | + System.out.println("-----------------------"); |
| 131 | + |
| 132 | + // This will invoke the constructor with one argument. |
| 133 | + People p2 = new People("Dwyane Johnson"); |
| 134 | + p2.setAge(45); |
| 135 | + p2.setWeight(86.3); |
| 136 | + p2.setHeight(6.3); |
| 137 | + p2.setAddress("Block 415, Bedok North, Singapore - 460415"); |
| 138 | + |
| 139 | + // This will print "Dwyane Johnson". |
| 140 | + System.out.printf("Name: %s \n", p2.getName()); |
| 141 | + System.out.printf("Age: %d (years) \n", p2.getAge()); |
| 142 | + System.out.printf("Weight : %.2f (Kgs) \n", p2.getWeight()); |
| 143 | + System.out.printf("Height : %.2f (Feet) \n", p2.getHeight()); |
| 144 | + System.out.printf("Address: %s \n", p2.getAddress()); |
| 145 | + System.out.println("-----------------------"); |
| 146 | + |
| 147 | + // This will invoke the default constructor. |
| 148 | + People p3 = new People(); |
| 149 | + // This will print default values set by the constructor. |
| 150 | + System.out.printf("Name: %s \n", p3.getName()); |
| 151 | + System.out.printf("Age: %d (years) \n", p3.getAge()); |
| 152 | + System.out.printf("Weight : %.2f (Kgs) \n", p3.getWeight()); |
| 153 | + System.out.printf("Height : %.2f (Feet) \n", p3.getHeight()); |
| 154 | + System.out.printf("Address: %s \n", p3.getAddress()); |
| 155 | + System.out.println("-----------------------"); |
| 156 | + |
| 157 | + // This will invoke the constructor with 3 arguments |
| 158 | + // but will set the remaining values as default |
| 159 | + // by calling the default constructor. |
| 160 | + |
| 161 | + People p4 = new People("Scarlett Johansson", 23, 5.1); |
| 162 | + System.out.printf("Name: %s \n", p4.getName()); |
| 163 | + System.out.printf("Age: %d (years) \n", p4.getAge()); |
| 164 | + System.out.printf("Weight : %.2f (Kgs)", p4.getWeight()); |
| 165 | + System.out.printf(" ---> Default value \n"); |
| 166 | + System.out.printf("Height : %.2f (Feet) \n", p4.getHeight()); |
| 167 | + System.out.printf("Address: %s ", p4.getAddress()); |
| 168 | + System.out.printf(" ---> Default value \n"); |
| 169 | + System.out.println("-----------------------"); |
| 170 | + |
| 171 | + People p5 = new People(62.5, "Los Angeles, United States of America."); |
| 172 | + System.out.printf("Name: %s ", p5.getName()); |
| 173 | + System.out.printf(" ---> Value set by another constructor \n"); |
| 174 | + System.out.printf("Age: %d (years) ", p5.getAge()); |
| 175 | + System.out.printf(" ---> Value set by another constructor \n"); |
| 176 | + System.out.printf("Weight : %.2f (Kgs) \n", p5.getWeight()); |
| 177 | + System.out.printf("Height : %.2f (Feet) ", p5.getHeight()); |
| 178 | + System.out.printf(" ---> Value set by another constructor \n"); |
| 179 | + System.out.printf("Address: %s \n", p5.getAddress()); |
| 180 | + System.out.println("-----------------------"); |
| 181 | + } |
| 182 | +} |
0 commit comments