Explain between given two programming languages


Question:

Explain between two programming languages

Two programs(Java and C++). They looks similar. But I need to explain why C++ is longer than java and what else to explain such as memory, time, build-in-function and so on...

// p1stack.cpp

#include
#include

using namespace std;


// stack exemptions
class EmptyStackException
{
};

// stack node
struct StackNode
{
string item;
StackNode* next;

StackNode(string s)
{
item = s;
next=0;
}

};

// stack class
class Stack
{

private:
StackNode* items; // stack items
int numItems; // number of items in stack

public:

//. initialize
Stack()
{
items=0;
numItems = 0;
}

// return number of items in stack
int size()
{
return numItems;
}

// push item onto stack
void push(string item)
{

StackNode* node = new StackNode(item);

if(items==0)

items = node;

else
{
numItems++;
node->next = items;
items = node;
}

}


// remove item from top of stack
string pop()
{

if(items == 0)
throw EmptyStackException();

else
{

string item = items->item;
items = items->next;
numItems--;
return item;

}
}

// return item from top of stack
string peek()
{

if(items == 0)
throw EmptyStackException();

else
{

string item = items->item;
return item;

}
}

// search for item in stack
bool search(string item)
{

StackNode* node = items;

while(node != 0)
{
if(node->item == item)
return true;
node = node->next;

}

return false;

}

// print out stack
friend ostream& operator <<(ostream& out, Stack& stk)
{

StackNode* node = stk.items;

while(node != 0)
{
out << node->item << " " << endl;

node = node->next;

}

return out;
}

};


// declare functions used by main
void print(Stack& stack);

int main()
{
Stack stack; // make stack

// push items onto stack
stack.push("Brazil");
stack.push("Canada");
stack.push("France");
stack.push("Mexico");
stack.push("Russia");
stack.push("Sweden");
stack.push("Brazil");
stack.push("Turkey");

print(stack);

cout << "stack.search(\"Brazil\") = "
<< stack.search("Brazil") << endl;

cout << "stack.pop() = " << stack.pop() << endl;

out << "stack.pop() = " << stack.pop() << endl;

print(stack);

cout << "stack.search(\"Brazil\") = " << stack.search("Brazil") << endl;

return 0;
}

// print size and items in stack
void print(Stack& stack)
{
cout << stack << endl;

cout << "stack.size() = " << stack.size() << endl;

try
{
cout << "stack.peek() = " << stack.peek() << endl;

}

catch(EmptyStackException)
{
cout << ": The stack is empty." << endl;
}

import java.util.Stack;
public class P1_Stack
{ public static void main(String[] args)
{ Stack stack = new Stack();
stack.push("Brazil");
stack.push("Canada");
stack.push("France");
stack.push("Mexico");
stack.push("Russia");
stack.push("Sweden");
stack.push("Brazil");
stack.push("Turkey");
print(stack);
System.out.println("stack.search(\"Brazil\") = "
+ stack.search("Brazil"));
System.out.println("stack.pop() = " + stack.pop());
System.out.println("stack.pop() = " + stack.pop());
print(stack);
System.out.println("stack.search(\"Brazil\") = "
+ stack.search("Brazil"));
}
private static void print(Stack stack)
{ System.out.println(stack);
System.out.println("stack.size() = " + stack.size());
try
{ System.out.println("stack.peek() = " + stack.peek());
}
catch(java.util.EmptyStackException e)
{ System.out.println(e + ": The stack is empty.");
}
}
}
}

Solution Preview :

Prepared by a verified Expert
Programming Languages: Explain between given two programming languages
Reference No:- TGS01937957

Now Priced at $20 (50% Discount)

Recommended (95%)

Rated (4.7/5)