Posts

C++ program to display the calendar

               This blog creates a c++program to display the calendar. It prints a month calendar. How to implement the program? First, include the built-in header files and namespace std. Next, check the leap year of the year. It checks the year is divided by 4,100 and 400, then it is leap year. Function daysOfMonth() assigns each month’s days value. For February month,check for leap year and assign the value as 29. Zeller’s Congruence is used to find the weekday. Finally, main() function gets the input as month and year and prints the calendar. Code: #include <iostream> #include <iomanip> using namespace std; // leap year checking function bool checkLeap(int yr) {     return (yr % 4 == 0 && yr % 100 != 0) || (yr % 400 == 0); } // days of the month int daysOfMonth(int yr, int month) {     int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};    ...

Simple Chatbot Application in C++

              Chatbot is a chatting application which reads the user input, find the appropriate response from the response database . It retrieves it from the database and give it as response to the user. Major steps are given below. Read the input from the user. Search it with the database. Finds the proper answer. Reply it back to the user. Let us create a Simple Chatbot application in C++. There are two functions in this program. One is main() and next one is toLowerCase(String s1). ‘toLowerCase(String s1)’ makes the string to lowercase letters. ‘main()’ function is used to read the input from the user. ‘getLine()’ is used to read the input. Based on the inputs, the request is searched according to the options. Finally,it displays the output. Here, we named the ChatBot name as Chaty. Code: #include <iostream> #include <string> #include <algorithm> using namespace std; //This function makes the ...

Quiz Program in C++

               Let us create a game project in c++. It is a quiz program, which asks the player and make them choose one out of four options. It saves the score and finally it displays the result. C++ code: #include <iostream> #include <string> using namespace std; int main() {     int score = 0;     string ans;     cout << "Welcome to the Game of Quiz!\n";     cout << "--------------------------\n\n";     // first question     cout << "Q1: What is the capital of India?\n";     cout << "a) New Delhi \nb) Madras \nc) Pondicherry \nd) Mumbai \n";     cout << "choose Your answer: ";     cin >> ans;     if (ans == "a" || ans == "A") {         cout << "Yes. You are Correct!\n\n"; ...

Library Management in C++

       Library management includes book management and member management. Book management includes the member variables as id,title,author and a Boolean variable isIssued. It has the following member functions. getBookId(), getBookTitle(),getBookstatus(),issueBook() and returnBook(). Member management includes membered,name. getId(),getName() are member functions. In the main() function, create the objects for above classes. Call the function to get the output. C++ Program : #include <iostream> #include <vector> #include <string> using namespace std; class Book {     int b_id;     string b_title;     string b_author;     bool b_isIssued; public:     Book(int i, string t, string a) : b_id(i), b_title(t), b_author(a), b_isIssued(false) {}     int getBookId() { return b_id; }     string getBookTitle() { return b_ti...

C++ program to implement Student Attendance System

              Let us create a student class and attendance class to implement student attendance system. Member variables : s_rollno, s_name,s_attendance. Create a constructor to initialise the values. MarkIt() is a function to mark the attendance. displayIt() is a function to display the student details. Attendance system is a class to calculate the attendance. ‘add_StudentDetails()’ is used to add the details of the student. ‘markIt()’ -it is used to mark the attendance. ‘final_display()’- it displays the attendance. ‘main()’ function is used to create the object and call the functions. Using switch case , the operations are selected and executed accordingly. Code: #include <iostream> #include < vector > #include < string > using namespace std; // create a class for student class School_Student { private:     int s_rollNo;     string s_name;   ...

‘volatile’ keyword in java programming

                 ‘ volatile ’ is a keyword used in java. It provides the changes that can be visible across the multiple threads . Most recent value is applicable for all threads. Features: Visibility across the multiple threads. Even though Caching, atomicity cannot be done, it is light weight and overhead is reduced. It makes latest value is read/written is visible. Use ‘volatile’ keyword when the following condition occurs… Multiple threads access the same variable for reading and writing. Singleton patterns are used. Thread safety is needed. Java program using ‘volatile’ keyword: class SFlag {     private volatile boolean f_run = true;     public void stop() {         f_run = false; // this statement makes this variable visible to all threads     }     public void doIt() {       ...

Synchronized block in java

                 It is a block of code which makes the program control takes over a single thread over multithreads in a critical region . Why synchronized block ? It deals with single thread. So, it improves the performance. Specific block is locked here. It considers a set of code instead of complete method. The race condition is effectively handled. Data inconsistency also improved. How to create a synchronized block? The code is given below. Code: synchronized ( lockObject ) {     // critical section code }   Let us create a java program to implement the counting application using synchronized block. Program: A class sample is created with private variable ‘s_incre’ and incre() function’s part of code as synchronized block. It increments the value ‘s_incre’. A function getIt() displays the counted value. In the main() function, object is created for sample class . Two threads are create...