Posts

Showing posts from April, 2018
//C++ Code for adjacency matrix representation of graph and traversal on it .(DFS,Non-recursive //DFS and BFS)  #include<iostream> //#include<queue> //#include<stack> using namespace std; class graph {int G[10][10],n; int visit[10]; public: void create(); void bfs(); void dfs(int); void dfs1(); }; class stack { int st[10]; public: int top,v1; stack() { top=-1; } void push(int v1) { st[++top]=v1; } int pop() { v1=st[top]; top--; return v1; } }; class queue { public: int Q[10],v; int f,r; queue() { f=-1; r=-1; } void insert(int v) { if(f==-1&&r==-1) { f=0;r=0; Q[r]=v; } else{ r++; Q[r]=v; } } int del() { v=Q[f]; f++; return v; } }; void graph::create() {int e,i,j,v1,v2; cout<<"enter the no.of vertices"; cin>>n; cout<<"enter the no.of edges"; cin&g
Image
Java code to illustrate package and Interface. Here screenshot shows two packages -First and second. Package First contains Interface myinterface.(myinterface.java) Package second contains Class Banking(Banking.java) myinterface.class  package First; public interface myinterface { public void createAccount(int v); public void displayBalance(); public void displayRecords(); public void updateBalance(int val); } Banking.java package second; import java.util.Scanner; import First.myinterface; public class Banking implements myinterface{ static int no; int account_no; int balance; String Cust_name; void getdata() { System.out.println("Enter Balance"); Scanner sc=new Scanner(System.in); balance=sc.nextInt(); System.out.println("Enter Customer Name"); Cust_name=sc.next(); } public void createAccount(int val) { account_no=val; getdata(); } int getAccno() { return account_no; } public void displayB
Image
Graph Representation using Adjacency matrix and Adjacency List and Traversal on it using DFS and BFS
#include<iostream> #include<fstream> using namespace std; // define a class to store student data class student {    int roll;    char name[30];    float marks; public:    student() { }    void getData(); // get student data from user    void displayData(); // display data    int getNo(){return roll;} }; void student :: getData() {    cout << "\nEnter Roll No. : ";    cin >> roll;    cin.ignore(); // ignore the newline char inserted when you press enter    cout << "Enter Name : ";    cin.getline(name, 30);    cout << "Enter Marks : ";    cin >> marks; } void student :: displayData() {    cout << "\nRoll No. : " << roll << endl;    cout << "Name : " << name << endl;    cout << "Marks : " << marks << endl; } int main() {    student s[3]; // array of 3 student objects    fstream file,file1;    int i;    file.open("objects.txt&quo
package scope; import java.util.Scanner; class person {private String name; int adharno; person() {     System.out.println("Base class constructor invoked");         } private    void getAdharno(int no,String s)     {         adharno=no;         name=s;     }    protected void getdata(int num,String s)     {       getAdharno(num,s);             }       void display()    {        System.out.println(adharno+"   "+name);    } } public class student extends person{     int rollno;     String year;     String branch;     student()     {super();         System.out.println("Enter Roll No");     Scanner s=new Scanner(System.in);     rollno=s.nextInt();         }     void display()     {super.display();         System.out.println("Derived class method invoked"+rollno);             }     public static void main(String args[])     {         student s=new student();                     System.out.println(&
Image
In this assignment we are illustrating how to import class in other package. In java , classes and interfaces can be grouped in packages. Right click newly created java project and you will see option new-> package as shown below. create java project :packageDemo Right click newly created project ( packageDemo ) select new-> select package Click on package. (enter package name eg: Package name :First) click on finish. Right click the package created (First). Add new class. Following image shows two packages created First (with class in it class1.java)  and Second (with class in it class2.java). Add following code in Class1.java package first; public class class1 {    int num;     public void getdata(int id) {    num=id; System.out.println("getdata() of Class1 from package first invoked"); } public void display() {   System.out.println(num); } } create other package (