a last in, first out (LIFO) abstract data type and data structure. Perhaps the most common use of stacks is to store - stack

MyStack()
{
Vector<Integer> v=new Vector<Integer>(10,2);
}
void push(int n)
{
v.addElement(n);
}
void pop()
{
if(v.isEmpty())
System.out.println("Stack underflow!");
else
System.out.println(v.elementAt(0));
}
void display()
{
for(int i=0;i<v.size();i++)
System.out.print(v.elementAt(i) +" ");
}
}
class StackDemo
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
MyStack s=new MyStack();
int option=0;
do
{
System.out.println("1: Push\n2:Pop\n3:Display\n4:Quit");
System.out.println("Enter your option: ");
option=in.nextInt();
switch(option)
{
case 1:
{
System.out.println("Enter an integer:");
int n=in.nextInt();
s.push(n);break;
}
case 2:s.pop();break;
case 3:s.display();break;
}
}
while(option!=4);
}
}
// throws an error: variable v not found. Any help would be much appreciated.Thanks.

It looks like v is being created locally in your constructor instead of as a member of your class.
Try defining v as a class member and then simply assign it in your constructor.
class MyStack {
Vector<Integer> v;
public MyStack() {
v = new Vector<Integer>(10,2);
}
}
Or just assign it when you define it:
class MyStack {
Vector<Integer> v = new Vector<Integer>(10,2);
}
Check out the Java tutorial on class members.

Related

How to implement generic class spezialization in Dart

In Dart we can use generic classes [class]. We can also specialize those classes [class]. However at runtime the specialization is not used. (In C++ this is called template programming)
Example: The following code will result in the output
Hallo world
How are you
class MyClass<T> {
foo( print('Hallo world'); );
}
class MyClassInt implements MyClass<int> {
#override
foo( print('How are you'); );
}
main() {
MyClass<int> a = Myclass<int>();
MyClassInt b = MyClassInt();
a.foo();
b.foo();
}
How can the specialization (here type [int]) be done, that it is called at runtime, i.e.
main() {
MyClass<int> a = Myclass<int>();
a.foo();
}
should result in the outcome "How are you".
As mentioned by jamesdlin, Dart does not support specialization. But you can do something like this to make the illusion:
class MyClass<T> {
factory MyClass() {
if (T == int) {
return MyClassInt() as MyClass<T>;
} else {
return MyClass._();
}
}
// Hidden real constructor for MyClass
MyClass._();
void foo() {
print('Hallo world');
}
}
class MyClassInt implements MyClass<int> {
#override
void foo() {
print('How are you');
}
}
void main() {
final a = MyClass<int>();
final b = MyClassInt();
final c = MyClass<String>();
a.foo(); // How are you
b.foo(); // How are you
c.foo(); // Hallo world
}

dependency injection in Arduino

I've started working with Arduino and I want to do a time share system so I do not use the delay command.
I have a problem when I try to register objects that inherit from another.
Here I have a test code that should show in the terminal: "Wow wow Miuau miuau ..."
I have doubts when I try to create an Interface and how do I declare the register () function so that Cat and Dog objects can be entered in the Animal type Array.
The following code is only to show the problem:
class Animal {
public:
void message() {
}
};
class Dog : public Animal {
public:
void message() {
Serial.println("Guau guau");
}
};
class Cat : public Animal {
public:
void message() {
Serial.println("Miau miau ");
}
};
class Multiplex {
private:
int index = 0;
Animal objects[5];
public:
void register(Animal object) {
objects[index] = object;
index++;
}
void go() {
for(int i = 0;i<index;i++) {
objects[i].message();
}
}
};
Dog dog;
Cat cat;
Multiplex multiplex;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
multiplex.register(dog);
multiplex.register(cat);
}
void loop() {
// put your main code here, to run repeatedly:
multiplex.go();
delay(1000);
}
Any help is welcome ...
Thanks and sorry for my english.
In this case you have to use polymorphism (virtual methods). But it still won't work with so many copies of "registered" object into the Animal base class (it shows nothing because Animal::message() is called). You have to use pointers (or references - but it's not so easy in this case)
class Animal { // pure virtual class (abstract class)
public:
virtual void message() = 0; // The '= 0;' makes whole class "pure virtual"
};
class Dog : public Animal {
public:
virtual void message() {
Serial.println("Guau guau");
}
};
class Cat : public Animal {
public:
virtual void message() {
Serial.println("Miau miau ");
}
};
class Multiplex {
private:
int index = 0;
Animal * objects[5];
public:
void reg(Animal * object) { // pass pointer to the object
objects[index] = object; // object must be valid for whole time
index++;
}
void go() {
for(int i = 0;i<index;i++) {
objects[i]->message();
}
}
};
Dog dog;
Cat cat;
Multiplex multiplex;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
multiplex.reg(&dog);
multiplex.reg(&cat);
}
void loop() {
// put your main code here, to run repeatedly:
multiplex.go();
delay(1000);
}
If you don't like dynamic polymorphism, you have to use something like object type, switch and typecasting to its correct type.

Writing a size() method for a User-defined Stack Class

I'm writing a program that requires the length/size of a stack. Because I am not importing the Stack class (and I've made my own - see below) I don't know how to make a method that calculates the size of the stack and returns that integer value. Here's the Stack class so far:
public class Stack<String> implements StackInter<String>
{
public void push(String x)
{ // This method is written
}
public String pop()
{ // This method is written
}
public boolean isEmptyStack()
{ // This method is written
}
public String peek()
{ // This method is written
}
public int size()
{
// What goes in here!
}
}
This is where I want to use the size method
public class InfixCalculator
{
Stack<String> stack = new Stack<String>();
int size = stack.size();
}
Any suggestions would be greatly appreciated!
the pseudo code is somthing like this:
public class Stack<String> implements StackInter<String>
{
private size;
//constructor
public Stack<String>(){
size=0;
}
public void push(String x)
{ // This method is written
size++;
}
public String pop()
{ // This method is written
if(size > 0 )
size--;
}
public boolean isEmptyStack()
{ // This method is written
}
public String peek()
{ // This method is written
}
public int size()
{
int theSize=size;
return theSize;
}
}
after you apply your operations the size can be computed.
If your Stack is an array write
public int size() {
return array.length;
}

Override method in dart on fly (like JAVA)

Is there way to overriding method in Dart like JAVA, for example:
public class A {
public void handleLoad() {
}
}
And when overriding:
A a = new A() {
#Override
public void handleLoad() {
// do some code
}
};
No, Dart does not have anonymous classes. You have to create a class that extends A and instantiate it.
No but it much less useful in Dart because you can just reassign function:
typedef void PrintMsg(msg);
class Printer {
PrintMsg foo = (m) => print(m);
}
main() {
Printer p = new Printer()
..foo('Hello') // Hello
..foo = ((String msg) => print(msg.toUpperCase()))
..foo('Hello'); //HELLO
}
However you will need some extra boilerplate to access instance.
Use type Function:
class A {
final Function h
A(this.h);
void handleLoad(String loadResult) { h(loadResult); }
}
Or
class A {
final Function handleLoad;
A(this.handleLoad);
}
A a = new A((String loadResult){
//do smth.
});

How to implement Iterable<E>

I am trying to port the Java code below to Dart and am puzzled about to do this.
In Java the Iterable interface is where clean with one method and to implement this is a snap.
How is this code best transformed to Dart?
/**
* Chess squares represented as a bitmap.
*/
public class ChessSquares implements Iterable<ChessSquare> {
private static class ChessSquaresIterator implements Iterator<ChessSquare> {
long bits;
int nextBit;
public ChessSquaresIterator(long bits) {
this.bits = bits;
nextBit = Long.numberOfTrailingZeros(bits);
}
#Override
public boolean hasNext() {
return (nextBit < 64);
}
#Override
public ChessSquare next() {
ChessSquare sq = ChessSquare.values()[nextBit];
bits = bits & ~sq.bit;
nextBit = Long.numberOfTrailingZeros(bits);
return sq;
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
}
#Override
public Iterator<ChessSquare> iterator() {
return new ChessSquaresIterator(bits);
}
...
By using IterableMixin you only need to implement the iterator-function.
class ChessSquares with IterableMixin<ChessSquare> {
#override
Iterator<ChessSquare> get iterator => new ChessSquaresIterator(bits);
...
}
Visit http://blog.sethladd.com/2013/03/first-look-at-dart-mixins.html for a short introduction on mixins.
The Iterator-interface is straight forward. You only have to implement the function moveNext and the getter current.
Soo I tried this which is kind of not what I want since I do not want to extend a base class.
/**
* Chess squares represented as a bitmap.
*/
class ChessSquares extends IterableBase<ChessSquare> {
Iterator<ChessSquare> get iterator {
return new ChessSquaresIterator(this);
}
...
}
class ChessSquaresIterator extends Iterator<ChessSquare> {
int _nextBit;
int64 _bits;
ChessSquare _current;
ChessSquaresIterator(ChessSquares squares) {
_bits = new int64.fromInt(squares._bits);
}
bool moveNext() {
_nextBit = _bits.numberOfTrailingZeros();
if (_nextBit < 64) {
_current = ChessSquare.values()[_nextBit];
_bits = _bits & ~_current.bit();
} else {
_current = null;
}
return _nextBit < 64;
}
E get current => _current;
}

Resources