Posts

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...

Static synchronization

       Static synchronization makes a single thread to execute the static method. Generally, this method deals with class level itself. 👉Why static synchronization?   It provides the consistency for all objects in the class. It ensures the other threads does not interfere the thread using static data . Program: This program creates a class with static member variable and static method . In the main function, a single thread can access it at a time in class level, not object level. class increment {     static int inc = 0;     // Static synchronized method     public static synchronized void s_incre() {         inc++;         System.out.println("The value is incremented as " + inc);     } } public class SampleCode {     public static void main(String[] args) {    ...

Lock interface in thread synchronization

               It is the built-in interface in java.util.concurrent.locks package.it is very easy to handle the synchronization in a flexible way. It has three types of locking such as interruptible locking , try-locking and timed locking . Let us create a java program to illustrate the lock interface. ·        This program starts from including the built-in package. ·        A class is written as SResource. ·        It creates a lock object as reentrantlock . ·        A method “SafeMethod()” is act as critical region. It locks and unlocks the thread based on the situation. ·        At last, the thread is unlocked using finally block. ·        To get and display the output, the LockEg class is used. ·        It creates the object for ...