8 Replies - 507 Views - Last Post: 31 March 2016 - 08:42 AM Rate Topic: -----

#1 AlexADB88  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-February 16

Interfaces. Please Help.

Posted 30 March 2016 - 04:31 PM

public interface StackInterface {
 /**
 * pushes an char onto the stack
 * @param input the String to be pushed
 */
 public void push(String input);
 /**
 * pops a char from the stack
 * @return returns the top char on the stack
 */
 public String pop();
 /**
 * peeks at the top of the stack
 * @return returns the top char on the stack but it stays on top
 */
 public String peek();
 /**
 *
 * @return true if the stack is empty
 */
 public boolean isEmpty();
    
}



import java.util.*;

public class myStack implements StackInterface {

    private String[] stackArray;
    private int stackSize;
    private int topOfStack = -1;
    
    myStack(int size){
        
        stackSize = size;
        stackArray = new String[size];
        Arrays.fill(stackArray, "-1");    
    }
    
    @Override
    public void push(String input){
        
        if(topOfStack + 1 < stackSize){
            topOfStack++;
            stackArray[topOfStack] = input;
        }
        
        else{
            System.out.println("Stack is full");
            System.out.println(stackArray[topOfStack]);
        }
    }

    @Override
    public String pop(){

        if(topOfStack >= 0){
            
            System.out.println("POP " + stackArray[topOfStack] + " was removed from the stack.");
            stackArray[topOfStack] = "-1";
            return stackArray[topOfStack--];
        }
        else
            System.out.println(stackArray[topOfStack]);
            return "-1";
    }
    
    @Override
    public String peek(){
       
        String peek = "PEEK " + stackArray[topOfStack] + " is at the top.";
        return peek; 
    }
    
    @Override
    public boolean isEmpty(){
        
        if(topOfStack < 0)
            return true;
        else
            return false;
    }
}



import java.util.Arrays;

public class Stack extends myStack { 
    
        public Stack(int size) {
        super(size);
    }
    
    public static void main(String[]args){
        
        Stack s = new Stack(9);
        s.push("Alexander");
        s.peek();
        s.push("Rocks");
        s.peek();
    }
}



I don't know whats going wrong here. I did an interface. I implemented it and declared methods. Then, I should be able to create a public static main void to create an array object and start pushing and popping. I have been trying to figure this out all day. I would appreciate if someone would point me in the right direction. I can't keep looking at this problem.

Is This A Good Question/Topic? 0
  • +

Replies To: Interfaces. Please Help.

#2 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Interfaces. Please Help.

Posted 30 March 2016 - 04:38 PM

peek() returns a String, but you are not actually printing the String.

System.out.println(s.peek());



Try that.

Beyond that problem, what exactly are you having issues with? You were not specific at all. Say what you want to do, what it is doing, and what you wrote to make that happen.
Was This Post Helpful? 0
  • +
  • -

#3 AlexADB88  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-February 16

Re: Interfaces. Please Help.

Posted 30 March 2016 - 09:52 PM

import java.util.*;

public class myStack implements StackInterface {

private char[] elements;
private final int size;
private int top;

public myStack(int s) {
    size = s > 0 ? s : 10;
    top = -1;
    elements = new char[size];
}

    @Override
    public void push(char ele){
        
        if(top + 1 < size){
            top++;
            elements[top] = ele;
        }
        
        else{
            System.out.println("Stack is full");
            System.out.println(elements[top]);
        }
    }

    @Override
    public char pop(){

        if(top >= 0){
            
            System.out.println("POP " + elements[top] + " was removed from the stack.");
            elements[top] = elements[top-1];
            return elements[top--];
        }
        else
            System.out.println("The Stack is Empty");
            return elements[top];
    }
    
    @Override
    public String peek(){
       
        String peek = "PEEK " + elements[top] + " is at the top.";
        System.out.println(peek);
        return peek; 
    }
    
    @Override
    public boolean isEmpty(){
        
        if(top < 0)
            return true;
        else
            return false;
    }
    
   [b] @Override
    public boolean palindromeChecker(String s){
        
        int len = s.length();
        double dHalf = len*.5;
        int iHalf = len*(1/2);
        elements = s.toCharArray();
            if(dHalf == iHalf){
                for(int i=0; i==iHalf; i++){
                    s.pop();
                }
    }[/b]
}



i've run into another problem. i started writing this palindrome checker method and i basically need access to the "pop" method i made earlier in the class. what is the best way to do this? do i have to make a palindromeChecker class extends myStack (myStack being the code i just sent)?
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Interfaces. Please Help.

Posted 31 March 2016 - 07:04 AM

You have to use a stack object and push the letters onto the stack. Then you would pop the letters off the stack and compare it to the letters at that index. If they continue to match throughout the length of the string, it's a palindrome. Otherwise, it's not.

You need a tutor, or to talk to your instructor about where you're at right now. You're not going to accomplish this program without significant hand-holding at your current understanding level, which is only going to hurt you for the next assignment because you will need even more hand-holding for the next one.

I do feel for you though. I'm in an intro java class right now too, and my fellow students also put the assignments off until the last minute, thinking that they can just crank out the work because "it seems easy enough." Sorry, programming is not like other classes you take. You cannot get away with it. A single mistake can turn into a whole day of debugging for a novice programmer. Where will the time come from if it's due tomorrow?

Take this as a learning experience.
Was This Post Helpful? 0
  • +
  • -

#5 AlexADB88  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-February 16

Re: Interfaces. Please Help.

Posted 31 March 2016 - 07:40 AM

you should go ahead and delete your last post. the first 1/3 didn't tell me anything i didn't already know. the last 2/3 was totally irrelevant to the post. if you're not going to answer the question or provide guidance to the answer-- don't post. i don't care about your personal assessment of my programming skills; or, what you think i should do about it.

i need a stack object. that's obvious.
i need to push and pop things from the stack. also obvious.

what is not obvious to me is if palindromeChecker method goes in the same class as the push, pop, isempty, and peak methods. It would seems strange if it did because I need to use those methods within the palindromeChecker. Therefore, I am thinking I need an entirely new class for palendrome and it needs access to the entire myStack class (methods and constructor).

relevant replies welcome.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,479
  • Joined: 12-June 08

Re: Interfaces. Please Help.

Posted 31 March 2016 - 07:53 AM

Easy easy, lemon squeezy. Let's pump the brakes on lashing out at folks.

People are free to offer conversation. This isn't some code writing service. It's a community!
Was This Post Helpful? 0
  • +
  • -

#7 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Interfaces. Please Help.

Posted 31 March 2016 - 08:00 AM

Very well.

Quote

what is not obvious to me is if palindromeChecker method goes in the same class as the push, pop, isempty, and peak methods.


Is a stack a palindrome checker? Or does putting palindrome checking specific code into the stack cause the stack object to do more than it should be responsible for?

If you create a new unrelated program, and you want to use your stack object (pretend you cannot change it at this point), would it make sense that it also has a palindrome checker in it?

If the answer is no, it doesn't belong in the stack.

Quote

Therefore, I am thinking I need an entirely new class for palendrome and it needs access to the entire myStack class (methods and constructor).


Nope. You just need to do this logic in the main method. Feel free to use other static helper methods, but the palindrome checking part doesn't need to be an object.

This post has been edited by MentalFloss: 31 March 2016 - 08:02 AM

Was This Post Helpful? 1
  • +
  • -

#8 AlexADB88  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-February 16

Re: Interfaces. Please Help.

Posted 31 March 2016 - 08:14 AM

View Postmodi123_1, on 31 March 2016 - 07:53 AM, said:

Easy easy, lemon squeezy. Let's pump the brakes on lashing out at folks.

People are free to offer conversation. This isn't some code writing service. It's a community!



i never asked anyone to write my code. i use this community for tutoring... which, is ironic considering Mental's last post. i didn't lash out at anyone, either. the guys post was arbitrary and condescending. if hes going to reply to anymore of my posts-- needed to be said.
Was This Post Helpful? 0
  • +
  • -

#9 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Interfaces. Please Help.

Posted 31 March 2016 - 08:42 AM

Quote

the guys post was arbitrary and condescending.


I'm not trying to be condescending. I'm trying to give you some solid advice for how you might be more successful in your course. I've been doing this a long time, and I have a pretty good grasp on gauging people's skill level, and what they need assistance with. You lack understanding of some fundamental concepts to the point that it would be very beneficial for you to take some time with a tutor to assess your weaknesses.

61
    public boolean palindromeChecker(String s){
62
         
63
        int len = s.length();
64
        double dHalf = len*.5;
65
        int iHalf = len*(1/2);
66
        elements = s.toCharArray();
67
            if(dHalf == iHalf){
68
                for(int i=0; i==iHalf; i++){
69
                    s.pop();
70
                }
71
    }[/b]



Show them this. They can help you.

EDIT:

Quote

i use this community for tutoring.


This is a bad idea. You need a real tutor at this point. You are working with interfaces, objects, and some intermediate concepts, but you lack fundamentals.

This post has been edited by MentalFloss: 31 March 2016 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1