Given below defined UML class diagram

Question

Given below defined UML class diagram. Suppose that a file of object named “FComputer” stored computers. Write a method void loadComputer(), that reads from FComputer and stores laptops in a linkedlist and desktops in another linkedlist. So that the first element to be read from FComputer is an integer that gives the number of write computers in the file.

Summary

In the given question we have defined the UML class diagram. Where we have already given a class name as a computer, laptop, and Desktop.

Explanation

A linked list is a list that consists of two parts. Where one part of the list store the element and the data whereas the second part is used to point to the next node. A linked list is a list that is made up of nodes. Nodes will be connected to one another and thus a list is formed. So as there is a benefit of the link list are data can be added at any point of the list.
Here we have already given a class name as a Class computer which is an abstract class, Next is a class laptop and the third class is class desktop. Each class has its own functions and default constructor.

 

Algorithm to insert any element in linked list:

  •  Set list=NULL, head=NULL.
  •  Set new_node->data=val, new_node->next=NULL.
  •  If head==NULL
    Set head=new_node
    And list=new_node
    Else
    Set list->next=new_node
    Update list=new_node
  •  End.

Code

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

//class Computer
class Computer
{
public:
    virtual void print()=0;
};

//class Laptop
class Laptop: public Computer
{
public:
    string lname;
    class Laptop *next;
    Laptop()
    {	lname="";
    }
    void print()
    {
        cout<<lname<<"\n";
    }
    Laptop(Laptop &l)
    {	lname=l.lname;
    }
};

//class Desktop
class Desktop: public Computer
{
public:
    string dname;
    class Desktop *next;
    Desktop()
    {	dname="";
    }
    void print()
    {
        cout<<dname<<"\n";
    }
    Desktop(Desktop &d)
    {	dname=d.dname;
    }
};

void loadComputer();
//declaration of linkedlists
Laptop *list1,*head1=NULL;
Desktop *list2,*head2=NULL;

int main()
{	loadComputer();

    cout<<"LaptopList is: \n";
    cout<<"Laptop"\n
    cout<<"Laptop"\n
    cout<<"Laptop"\n
    Laptop *temp=head1;
    while(temp!=NULL)
    {	temp->print();
        temp=temp->next;
    }
    cout<<"DesktopList is: \n";
    cout<<"Desktop"
    Desktop *tempr=head2;
    while(tempr!=NULL)
    {	tempr->print();
        tempr=tempr->next;
    }
return 0;
}
//loadComputers() function definition
void loadComputer()
{	int no;
    string word;
    fstream file;
    //open file
    file.open("FComputers.txt");
    //input number of computers
    file>>no;
    
    for(int i=0; i<no; i++)
    {
        file>>word;
        //if the word is laptop
        //insert into laptop list
        if(word=="Laptop")
        {	Laptop *temp= new Laptop();
            temp->lname=word;
            temp->next=NULL;
            if(head1==NULL)
            {	head1=temp;
                list1=temp;
            }
            else
            {	list1->next=temp;
                list1=temp;
            }
        }
        else	//insert into desktoplist
        {	Desktop *temp= new Desktop();
            temp->dname=word;
            temp->next=NULL;
            if(head2==NULL)
            {	head2=temp;
                list2=temp;
            }
            else
            {	list2->next=temp;
                list2=temp;
            }
        }
    }
    file.close();	
}

Output

defined UML class diagram

 

Also read, can you fix any mistake on this C program.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *