Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, February 3, 2025

February 03, 2025

Electricity Bill Calculator Prgram in JAVA

Objective: Develop a Java application to generate Electricity bill. Create a class with the following members: Consumer no., consumer name, previous month reading, current month reading, type of EB connection (i.e domestic or commercial). Compute the bill amount using the following tariff. If the type of the EB connection is domestic, calculate the amount to be paid as follows:


First 100 units - Rs. 1 per unit

101-200 units - Rs. 2.50 per unit
201 -500 units - Rs. 4 per unit
> 501 units - Rs. 6 per unit

If the type of the EB connection is commercial, calculate the amount to be paid as follows:
First 100 units - Rs. 2 per unit
101-200 units - Rs. 4.50 per unit
201 -500 units - Rs. 6 per unit
 > 501 units - Rs. 7 per unit

 


import java.util.Scanner;


class ElectricityBill {

    private int consumerNo;

    private String consumerName;

    private int prevReading;

    private int currReading;

    private String ebType;

    

    public ElectricityBill(int consumerNo, String consumerName, int prevReading, int currReading, String ebType) {

        this.consumerNo = consumerNo;

        this.consumerName = consumerName;

        this.prevReading = prevReading;

        this.currReading = currReading;

        this.ebType = ebType;

    }

    

    public double calculateBill() {

        int unitsConsumed = currReading - prevReading;

        double amount = 0;

        

        if (ebType.equalsIgnoreCase("domestic")) {

            if (unitsConsumed <= 100)

                amount = unitsConsumed * 1.0;

            else if (unitsConsumed <= 200)

                amount = 100 * 1.0 + (unitsConsumed - 100) * 2.50;

            else if (unitsConsumed <= 500)

                amount = 100 * 1.0 + 100 * 2.50 + (unitsConsumed - 200) * 4.0;

            else

                amount = 100 * 1.0 + 100 * 2.50 + 300 * 4.0 + (unitsConsumed - 500) * 6.0;

        } 

        else if (ebType.equalsIgnoreCase("commercial")) {

            if (unitsConsumed <= 100)

                amount = unitsConsumed * 2.0;

            else if (unitsConsumed <= 200)

                amount = 100 * 2.0 + (unitsConsumed - 100) * 4.50;

            else if (unitsConsumed <= 500)

                amount = 100 * 2.0 + 100 * 4.50 + (unitsConsumed - 200) * 6.0;

            else

                amount = 100 * 2.0 + 100 * 4.50 + 300 * 6.0 + (unitsConsumed - 500) * 7.0;

        } else {

            System.out.println("Invalid connection type!");

        }

        return amount;

    }

    

    public void displayBill() {

        System.out.println("\nElectricity Bill");

        System.out.println("Consumer Number: " + consumerNo);

        System.out.println("Consumer Name: " + consumerName);

        System.out.println("Units Consumed: " + (currReading - prevReading));

        System.out.println("Type of Connection: " + ebType);

        System.out.println("Bill Amount: Rs. " + calculateBill());

    }

}


public class ElectricityBillCalculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        

        System.out.print("Enter Consumer Number: ");

        int consumerNo = sc.nextInt();

        sc.nextLine(); 

        

        System.out.print("Enter Consumer Name: ");

        String consumerName = sc.nextLine();

        

        System.out.print("Enter Previous Month Reading: ");

        int prevReading = sc.nextInt();

        

        System.out.print("Enter Current Month Reading: ");

        int currReading = sc.nextInt();

        sc.nextLine();

        

        System.out.print("Enter Type of EB Connection (Domestic/Commercial): ");

        String ebType = sc.nextLine();

        

        ElectricityBill bill = new ElectricityBill(consumerNo, consumerName, prevReading, currReading, ebType);

        bill.displayBill();

      

        sc.close();

    }

}



Output : 




Tuesday, November 26, 2024

November 26, 2024

Updating a File

Updating a file


Updating a file in C++ involves reading the file, making the necessary modifications, and then writing the updated data back to the file. Below is a structured explanation and example of how to do this:


Steps to Update a File:

Step 1: Write Initial Content to the File

  1. Open the file in write mode (std::ofstream).
  2. Write the initial data into the file line by line.
  3. Close the file to ensure data is saved properly.

Step 2: Read the File into a Data Structure

  1. Open the file in read mode (std::ifstream).
  2. Use a loop to read each line from the file and store it in a suitable data structure, such as a std::vector<std::string>.
  3. Close the file after reading all its content.

Step 3: Modify the Required Content

  1. Identify the specific line or lines that need to be updated using the index in the vector.
  2. Modify the content by replacing or appending as needed.

Step 4: Write the Updated Content Back to the File

  1. Open the file in truncate mode (std::ofstream with std::ios::trunc), which clears the file before writing.
  2. Write each line from the updated data structure back to the file.
  3. Close the file to ensure the changes are saved.

Summary of the Workflow

  1. File Creation: Initially create a file with default content.
  2. Content Reading: Read the current content for processing.
  3. Content Modification: Perform updates on specific parts of the data.
  4. Content Overwriting: Write the updated data back to the file.

These steps provide a structured approach to handling file operations in C++.

Program:

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

int main() 

{

    std::string filename = "data.txt";

    // Step 1: Write initial content to the file

    {

        std::ofstream outputFile(filename);

        if (!outputFile.is_open()) 

{

            std::cerr << "Error: Unable to open file for writing initial content.\n";

            return 1;

}

        // Writing initial content

        outputFile << "Line 1: Hello World\n";

        outputFile << "Line 2: Welcome to C++\n";

        outputFile << "Line 3: File Operations\n";

        outputFile.close();

        std::cout << "Initial content written to the file.\n";

    }

    // Step 2: Read the file into a vector

    std::vector<std::string> lines;

    std::string line;

    std::ifstream inputFile(filename);

    if (!inputFile.is_open()) {

        std::cerr << "Error: Unable to open file for reading.\n";

        return 1;

    }

    while (std::getline(inputFile, line)) 

{

        lines.push_back(line);

    }

    inputFile.close();

    // Step 3: Modify the specific line

    if (lines.size() >= 2) {

        lines[1] = "Line 2: Updated Content"; // Update the second line

    }

else 

{

        std::cerr << "Error: File does not have enough lines to update.\n";

        return 1;

    }

    // Step 4: Write the updated content back to the file

    std::ofstream outputFile(filename, std::ios::trunc);

    if (!outputFile.is_open()) 

{

        std::cerr << "Error: Unable to open file for writing updated content.\n";

        return 1;

 }

    for (const auto& updatedLine : lines) 

{

        outputFile << updatedLine << "\n";

  }

    outputFile.close();

    std::cout << "File updated successfully.\n";

    return 0;

}


Output:



Sunday, November 17, 2024

November 17, 2024

Sequential Input and Output Operations in C++

 Sequential Input and Output Operations in C++

Sequential input and output operations in C++ involve processing data in a file in the order in which it is stored. These operations are straightforward and suited for applications where the data is read or written linearly.

C++ provides classes and functions for sequential I/O through the <fstream> library. The most commonly used classes are:

  • ifstream: For input (reading data from files).
  • ofstream: For output (writing data to files).
  • fstream: For both input and output operations.

Basic Workflow of Sequential I/O

  1. Input Operations:

    • Open a file for reading using ifstream.
    • Read data sequentially until the end of the file.
    • Close the file after reading.
  2. Output Operations:

    • Open a file for writing using ofstream.
    • Write data sequentially to the file.
    • Close the file after writing.

Example: Sequential Output Operation

Writing data sequentially to a file.




Example: Sequential Input Operation

Reading data sequentially from a file.


Program:


#include <iostream>

#include <fstream>

#include <string>

using namespace std;


int main() {

    ifstream infile("example.txt");

    if (!infile) {

        cout << "Error opening file for reading!" << endl;

        return 1;

    }

    string line;

    // Sequentially reading data from the file

    while (getline(infile, line)) {

        cout << line << endl; // Print each line

    }

    infile.close();

    return 0;

}






November 17, 2024

File Pointers and their Manipulations

 

File Pointers and Their Manipulations in C++

In C++, file pointers are used to manage the position of the reading or writing cursor within a file. These pointers allow precise control over where data is read from or written to, making file handling efficient and flexible.


Types of File Pointers

  1. Input File Pointer (get pointer)
    Used for reading from a file. It determines where the next data will be read.

  2. Output File Pointer (put pointer)
    Used for writing to a file. It determines where the next data will be written.

File Pointer Manipulation Functions

C++ provides several member functions for manipulating file pointers. These are:

FunctionPurpose
seekg(offset, dir)Moves the get pointer (read) to a specific location in the file.
seekp(offset, dir)Moves the put pointer (write) to a specific location in the file.
tellg()Returns the current position of the get pointer.
tellp()Returns the current position of the put pointer.
ios::begBeginning of the file (used with seekg or seekp).
ios::curCurrent position in the file (used with seekg or seekp).
ios::endEnd of the file (used with seekg or seekp).



How to Manipulate File Pointers

1. Moving the get Pointer

The seekg() function moves the input file pointer to a specific position.

Syntax:
file.seekg(offset, direction);
  • offset: Number of bytes to move.
  • direction: Position relative to which the movement occurs (ios::beg, ios::cur, or ios::end).


    2. Moving the put Pointer

    The seekp() function moves the output file pointer to a specific position.

    Syntax:
    file.seekp(offset, direction);

    3. Getting the Current Pointer Position

    • tellg() gives the current position of the get pointer.
    • tellp() gives the current position of the put pointer.

      Example: File Pointer Manipulation

    #include <iostream>
    #include <fstream>
    using namespace std;

    int main() {
        fstream file;

        // Open a file in read/write mode
        file.open("example.txt", ios::in | ios::out | ios::trunc);

        if (!file) {
            cout << "Error opening file!" << endl;
            return 1;
        }

        // Write some data to the file
        file << "ABCDEFGH";

        // Move the put pointer to the beginning
        file.seekp(0, ios::beg);

        // Overwrite the first character
        file << 'Z';

        // Move the get pointer to the 4th character
        file.seekg(3, ios::beg);

        // Read the 4th character
        char ch;
        file.get(ch);
        cout << "Character at 4th position: " << ch << endl;

        // Display the current positions of get and put pointers
        cout << "Get pointer position: " << file.tellg() << endl;
        cout << "Put pointer position: " << file.tellp() << endl;

        file.close();
        return 0;
    }

    Example: 




    Key Points

    1. Default Behavior of Pointers:

      • When a file is opened in read mode, the get pointer is set to the beginning of the file.
      • When a file is opened in write mode, the put pointer is set to the beginning (or the end if in append mode).
    2. Using seekg and seekp:

      • seekg is used for positioning the input pointer (reading).
      • seekp is used for positioning the output pointer (writing).
    3. Offsets and Directions:
      The offset parameter specifies the number of bytes to move. The direction specifies the starting point:

      • ios::beg: Beginning of the file.
      • ios::cur: Current position in the file.
      • ios::end: End of the file.


    Example: Random Access with File Pointers

    Random access is achieved by directly moving the file pointers to the required position.

    #include <iostream>
    #include <fstream>
    using namespace std;

    int main() {
        fstream file("data.bin", ios::out | ios::in | ios::binary | ios::trunc);

        if (!file) {
            cout << "Error opening file!" << endl;
            return 1;
        }

        // Write integers to the binary file
        for (int i = 1; i <= 5; i++) {
            file.write((char*)&i, sizeof(i));
        }

        // Move to the 3rd integer (2nd index, as indexing starts from 0)
        file.seekg(2 * sizeof(int), ios::beg);

        // Read and display the 3rd integer
        int num;
        file.read((char*)&num, sizeof(num));
        cout << "The 3rd integer is: " << num << endl;

        // Update the 3rd integer to 10
        num = 10;
        file.seekp(2 * sizeof(int), ios::beg);
        file.write((char*)&num, sizeof(num));

        // Verify the update
        file.seekg(0, ios::beg); // Reset to beginning
        cout << "Updated file contents:" << endl;
        while (file.read((char*)&num, sizeof(num))) {
            cout << num << " ";
        }
        cout << endl;

        file.close();
        return 0;
    }






    Summary

    • File pointers provide flexibility to read/write from specific positions in a file.
    • Use seekg and seekp for moving file pointers and tellg/tellp to get their positions.
    • These functions are vital for random access in large files, enabling efficient data management.


    November 17, 2024

    File mode Detecting End-of-File

     

    File Modes

    Files can be opened in various modes using flags (e.g., ios::in, ios::out, etc.):

    ModeDescription
    ios::inOpen for input (reading).
    ios::outOpen for output (writing).
    ios::appAppend to the end of the file.
    ios::truncTruncate (delete) the contents of the file.
    ios::binaryOpen in binary mode.

    Example:


     Detecting End-of-File (EOF)

    The eof() function is used to check if the end of the file has been reached during a read operation.

    Example:

    #include <iostream>

    #include <fstream>

    using namespace std;

    int main() 

    {

    std::ifstream file("example.txt");

    std::string line;

    while (!file.eof()) {

        std::getline(file, line);

        std::cout << line << std::endl;

    }

    file.close();

        return 0;

    }



    Output: 








    November 17, 2024

    File Stream Operations: Opening and Closing a File

     Working with Files

    1. Include the <fstream> Library
      To use file handling features, include the <fstream> library:

    #include <fstream>

    Opening and Closing a File
    Files are opened using the open() method or during object creation. Always close files after use with close().

    Example:


    #include <iostream>

    #include <fstream>

    using namespace std;

    int main() {

        // Write to a file

        ofstream outfile("example.txt");

        if (outfile.is_open()) {

            outfile << "Hello, File Handling in C++!\n";

            outfile.close();

        } else {

            cout << "Unable to open file for writing.\n";

        }


        // Read from a file

        ifstream infile("example.txt");

        string line;

        if (infile.is_open()) {

            while (getline(infile, line)) {

                cout << line << endl; // Print the content to the console

            }

            infile.close();

        } else {

            cout << "Unable to open file for reading.\n";

        }

        return 0;

    }




    Checking if a File is Open:

    The is_open() method checks whether the file was successfully opened.


    November 17, 2024

    Files and Streams in C++

     

    Files and Streams in C++

    In C++, files and streams are essential concepts used for input and output operations. They allow a program to interact with data stored in external files and handle input and output from the user or other devices.


    Files

    A file is a collection of data stored in secondary storage (e.g., hard drive) that can be read or written by a program. Files can be categorized into:

    1. Text Files: Store data in human-readable characters.
    2. Binary Files: Store data in a format readable only by computers, which is more compact and faster to process.


    Streams

    A stream in C++ is an abstraction that represents a flow of data between a program and an external source or destination. The concept of streams simplifies the process of input and output.

    1. Input Stream: Used to read data into the program (e.g., cin, file input).
    2. Output Stream: Used to write data from the program (e.g., cout, file output).




    File Handling in C++

    C++ provides a library <fstream> for file operations, which includes the following classes:

    1. ifstream: For input (reading data from a file).
    2. ofstream: For output (writing data to a file).
    3. fstream: For both input and output.