Coders Packet

Simple Chatbot using Java

By Arya Bhattacharyya

A simple chatbot made using Java. This project requires no prior knowledge of Java and can be the very first project of students aspiring to become Java developers.

Explanation:

This project has no prerequisites, so this could be a great place to start with Java. The IDE(Integrated Development Environment), I used for this project was IntelliJ IDEA 2021.1.3 x64 Ultimate and Java SE 16.0.1. Both can be downloaded from the following links:

https://www.jetbrains.com/idea/download/#section=windows
https://www.oracle.com/in/java/technologies/javase-downloads.html

The goal of this project is to create a simple chatbot, which responds to the user's messages. So first, we need to import the 'Scanner' class inside the 'java.util' to take inputs from the user as follows: import java.util.Scanner; Next we need to create a class and inside it create a Scanner object to take the input.

public class Main {
    final static Scanner scanner = new Scanner(System.in);
}

Next, we create a function called greet() which takes two strings, the assistant's name and the birth year, respectively, as parameters. We will greet the user using this function.

static void greet(String assistantName, String birthYear) {
    System.out.println("Hello! My name is " + assistantName + ".");
    System.out.println("I was created in " + birthYear + ".");
    System.out.println("Please, remind me your name.");
}

Next, we create a function called remindName() which lets the program know the user's name.

static void remindName() {
    String name = scanner.nextLine();
    System.out.println("What a great name you have, " + name + "!");
}

After that, we create a function called guessAge(), which uses a simple algorithm to guess the age of the user.

static void guessAge() {
    System.out.println("Let me guess your age.");
    System.out.println("Enter remainders of dividing your age by 3, 5 and 7.");
    int rem3 = scanner.nextInt();
    int rem5 = scanner.nextInt();
    int rem7 = scanner.nextInt();
    int age = (rem3 * 70 + rem5 * 21 + rem7 * 15) % 105;
    System.out.println("Your age is " + age + "; that's a good time to start programming!");
}

Next, we create a function called count() to count to any number, the user please, using a for() loop

static void count() {
    System.out.println("Now I will prove to you that I can count to any number you want.");
    int num = scanner.nextInt();
    for (int i = 0; i <= num; i++) {
        System.out.printf("%d!\n", i);
    }
}

Next, we create a function called test(), which makes a small multiple-choice quiz to test the programming knowledge of the user.

static void test() {
    System.out.println("Let's test your programming knowledge.");
    System.out.println("Why do we use methods?\n1. To repeat a statement multiple times.\n2. To decompose a program into several small subroutines.\n3. To determine the execution time of a program.\n4. To interrupt the execution of a program.");
    while(true) {
        int choice = scanner.nextInt();
        if(choice == 4) {
            System.out.println("Correct Answer!");
            break;
        }
        else System.out.println("Please, try again.");
    }
}

Finally, we create an end() function to display a message, signifying the end of the program.

static void end() {
    System.out.println("Congratulations, have a nice day!");
}

We create a main() function to call all the functions in the class as follows:

public static void main(String[] args) {
    greet("SimpleBot", "2021");
    remindName();
    guessAge();
    count();
    test();
    end();
}

Compile and run the code and voilà!

 

Result:

Result

Download project

Reviews Report

Submitted by Arya Bhattacharyya (xxEasterGrymm)

Download packets of source code on Coders Packet