Welcome to the blog !!
It takes years of consistent practice to become an expert. So lets practice java programming regularly and Be Stronger Than Yesterday 💪👍
Before we start with another tutorial, make sure you have basic knowledge of java as well as Eclipse platform and if you still think you need to brush up the basics of java , please check my previous post here .
Today , we will learn to build Email Generator using some java concepts .
✅ Problem Statement:
The 'Java Tutor Company' provide mail-Id to their newly joined employees in the specific format.
Suppose Mr. John Nielsen join the company and his birth date is 15th Aug , the mail-id assigned to him is- john.nielsen.1508@javatutor.com.
Our task is to build a system which will help company to automate this task by generating mail-id after successfully completing registration process.
✅ Concepts of Java covered in this Project:
- Create Object
- Call methods using object
- Take Input and Display Output
- Use of Parameterized Constructor
- String Concatenation
📑 Let's Code :)
Note* : Pre-install Eclipse IDE on your machine.
For this project, we are going to create two classes - EmailGenerator and Main.
EmailGenerator class will contain all the functionality and Main class will contain Main method to access those functionalities.
Follow the Steps ....
Step 1: Open Eclipse IDE and Right click on File tab to create new Java Project.
Step 2: Once you click on Java Project , it will open a new prompt box . Give some name to your project and then click on Finish Button.
Step 3: Now , it will create two folders under your project name (here I give project name as java_project) JRE and SRC. Now right click on SRC → New → Package .
*All classes which we are going to create for our project must be in the same package so we have created package under project.
Give name to your package and hit Finish Button. This will create package for your project.
Step 4: Again right click on package you have created to create classes. Refer the below screenshot to create classes . Remember to repeat the same process twice as we need to create 2 classes for this project.
Give the class name as EmailGenerator and Main.
Step 5: Now we will complete EmailGenerator class.
This class will contain -
- Declaration of private members / variables.
- Parameterized Constructor.
- Getters and Setters for each variable.
- Method to generate Email-Id in specified format.
EmailGenerator.java 1.1 |
|
🔴 Concept to Learn :
Que - What is Parameterized Constructor ? What is its use ?
⇒ Constructor which accepts specific numbers of parameters is called Parameterized Constructor.(Refer img EmailGenerator 1.1) In the above code , we have pass 3 arguments or parameters to the constructor and again set the accepted values to private member variables. This can also be done with the help of Setters.
Parameterized Constructor is used to provide value to the objects.
Refer This - Parameterized Constructor
Que - How to Concatenate two strings in Java ?
⇒ We can concatenate two strings by two methods :-
A] Using + Operator :
+ Operator is used to add two strings. For ex: System.out.print("Hello"+"World"); will print HelloWorld.
(Refer newMail() method from EmailGenerator class)
B] Using concat () method :
str1.concat(str2) concatenates two strings. This will add str2 at the end of str1.
For ex: String str1 = "Hello"; String str2 = "World"; String str3 = str1.concat(str2);
system.out.print(str3);
This will print HelloWorld on the console.
Refer This to study more about String Concatenation.
Step 6: Now we will complete main method from Main class.
This class contains -
- Take input and access member variables and functions from EmailGenerator Class.
- Take input from user.
- Invoke parameterized constructor by creating object.
- Call method in EmailGenerator class using object.
- Display output.
Here is sample code for Main Class -
🔴 Concept to Learn :
Constructor :
- Block of code that initializes newly created object.
- New Keyword creates the object of class and invokes the constructor to initialize object.
- Consider ex- EmailGenerator obj = new EmailGenerator(); This will create object obj and invoke default constructor EmailGenerator() even if it is not written in your EmailGenerator.java file.
- Here in our code, EmailGenerator email = new EmailGenerator(firstName,lastName,dob); EmailGenerator() is the parameterized constructor which will invoked after creating object.(Refer Main.java 2.1 Line 24)
Step 7 : Save and Run the Project.
To run the project in Eclipse IDE , press Ctrl + F11 (Make sure you have run Main.java class)
Here is sample Output :
We are done with our another project !!
You can check the whole code here
All the Best 👍👍
Happy Learning 😀😀❗❗