📌 Introduction
This document covers Array Concepts and Chatbot Applications in Java.
- Arrays store multiple values of the same data type in a single variable.
- Chatbots are programs that simulate human-like interactions.
1️⃣ Basic Array Concept
What is an Array?
An array is a data structure that stores multiple elements of the same type in a contiguous memory location.
Example Program: Basic Array Operations
package com.codingboot.arrays;
public class ArrayProgram {
public static void main(String[] args) {
// Declare and initialize an array
int marks[] = {100, 56, 67, 70, 90, 8, -4};
int total = 0;
int min = marks[0];
for (int i = 0; i < marks.length; i++) {
total = total + marks[i]; // Calculate total sum
if (marks[i] < min) {
min = marks[i]; // Find minimum element
}
}
System.out.println(“Total = ” + total);
System.out.println(“Average = ” + (total) / marks.length);
System.out.println(“Min = ” + min);
}
}
Explanation:
- Array Initialization:
{100, 56, 67, 70, 90, 8, -4}
- Calculate Total: Using a loop, sum all elements.
- Find Minimum: Compare each element to get the smallest value.
- Calculate Average: Divide sum by array length.
💻 Run Program & Output:
Input: (No input required)
Output:
Total = 387
Average = 55
Min = -4
2️⃣ Shopping ChatBot
Project Description:
This is a simple chatbot that helps users inquire about available products, their prices, and stock.
Example Program: Shopping ChatBot
package com.codingboot.arrays;
import java.util.Scanner;
public class ShoppingChatBot {
public static void main(String[] args) {
String products[] = {“Laptop”, “Mobile”, “Keyboard”, “Book”};
int price[] = {900, 200, 300, 500};
int stock[] = {2, 4, 5, 8};
System.out.println(“ShoppingBot: Welcome to our store! Type ‘list’ to see available products.”);
Scanner scanner = new Scanner(System.in);
while (true) {
boolean found = false;
System.out.print(“You: “);
String input = scanner.nextLine();
if (input.equalsIgnoreCase(“exit”)) {
System.out.println(“ShoppingBot: Thanks for visiting! Have a great day.”);
break;
} else if (input.contains(“list”) || input.contains(“products”)) {
for (String product : products) {
System.out.println(“- ” + product);
}
} else {
for (int i = 0; i < products.length; i++) {
if (input.equalsIgnoreCase(products[i])) {
System.out.println(“Here are the details for ” + input);
System.out.println(“Price = ” + price[i]);
System.out.println(“Stock = ” + stock[i]);
found = true;
break;
}
}
if (!found) {
System.out.println(“Sorry! I couldn’t find that product. Type ‘list’ to see available products.”);
}
}
}
}
}
Explanation:
- Predefined product list: Products, prices, and stock.
- User Interaction: Accepts product name as input.
- Displays product details: If found, shows price and stock.
- Exit Condition: User types “exit” to stop.
💻 Run Program & Output:
Input:
You: list
Output:
- Laptop
- Mobile
- Keyboard
- Book
Input:
You: Laptop
Output:
Here are the details for Laptop
Price = 900
Stock = 2
Input:
You: exit
Output:
ShoppingBot: Thanks for visiting! Have a great day.
3️⃣ Simple ChatBot Using Arrays
Project Description:
A chatbot that responds to predefined questions using an array.
Example Program: Simple ChatBot
package com.codingboot.arrays;
import java.util.Random;
import java.util.Scanner;
public class SimpleChatBotUsingArray {
public static void main(String[] args) {
String questions[] = {
"hello",
"hi",
"how are you",
"what is your name",
"what can you do",
"bye",
"who created you"
};
String answer[] = {
"Hello! How can I assist you?",
"Hi there! How can I help?",
"I'm just a chatbot, but I'm doing great!",
"I'm a simple Java chatbot.",
"I can answer simple queries.",
"Goodbye! Have a great day!",
"I was created by a Java developer."
};
String randomResponse[] = {
"I'm not sure how to respond to that.",
"I don't know, but I'm learning every day.",
"Can you rephrase your question?",
"That's interesting! Tell me more..."
};
Scanner scanner = new Scanner(System.in);
Random random = new Random();
boolean exit = false;
while (!exit) {
System.out.print("You: ");
String input = scanner.nextLine();
boolean isFound = false;
for (int i = 0; i < questions.length; i++) {
if (input.equalsIgnoreCase("bye")) {
System.out.println("ChatBot: Goodbye! Have a great day!");
exit = true;
break;
}
if (input.equalsIgnoreCase(questions[i])) {
System.out.println("ChatBot: " + answer[i]);
isFound = true;
}
}
if (!isFound) {
int randomIndex = random.nextInt(randomResponse.length);
System.out.println("ChatBot: " + randomResponse[randomIndex]);
}
}
}
}
Explanation:
- Predefined Questions & Answers: Uses arrays for chatbot responses.
- User Interaction: Accepts user input.
- Matching Questions: If input matches, responds accordingly.
- Random Response: If question is unknown, picks a random reply.
- Exit Condition: Stops when the user types “bye”.
💻 Run Program & Output:
Input:
You: hello
Output:
ChatBot: Hello! How can I assist you?
Input:
You: what is your name
Output:
ChatBot: I'm a simple Java chatbot.
Input:
You: unknown question
Output:
ChatBot: Can you rephrase your question?
Input:
You: bye
Output:
ChatBot: Goodbye! Have a great day!
📌 Summary
✅ Arrays store multiple elements of the same type.
✅ Basic Array Program: Finds total, average, and minimum value.
✅ Shopping ChatBot: Provides product details on user request.
✅ Simple ChatBot: Responds to user queries using predefined arrays.
🔹 These programs demonstrate real-world applications of arrays in Java. 🚀
Leave a Reply