Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem finding a solution for this peculiar problem.
I have this class of a generic Stack.
class Pilita<T> {
private int tam;
private T[] arregloPila;
private int tope;
#SuppressWarnings("unchecked")
public Pilita(int tam) {
this.tam=tam;
arregloPila=(T[])new Object[tam];
tope=-1;
}
public void push(T valor){
if(pilaLlena()){
throw new StackFullException("No se puede meter el: "+"["+valor+"]"+" La pila esta llena");
}
arregloPila[++tope]=valor;
}
public T pop() {
if(pilaVacia()){
throw new StackEmptyException("La pila esta vacia");
}
return arregloPila[tope--];
}
public boolean pilaVacia(){
return (tope == -1);
}
public boolean pilaLlena(){
return (tope==tam-1);
}
public int contar(){
return(tope+1);
}
And I want to know how to implement a method to print the stack, because i have a method for normal stacks, but it doesn't seems to work with Generic Stack.
Any help would be Helpful.
To print the all the stack, you only need to check if the stack is not empty
public static void main(String[] args) {
Pilita pila = new Pilita(3);
pila.push("1");
pila.push("2");
pila.push("3");
while (!pila.pilaVacia()) {
System.out.println("Pil pop " + pila.pop());
}
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have an Interface and a class which implements it.
I #Override the method and I want to test it using JUnit4.
I don't know if I have an error on my method or if I'm doing something wrong in my test class.
Is it correct to have an instance of the interface itself, or should I make it an instance of the myCustomString Class which implements the Interface.
if anyone could explain how the objects are calling each other that would be awesome.
My JUnit test returns a null pointer exception.
public interface MyCustomStringInterface {
int countNumbers();
}
public class MyCustomString implements MyCustomStringInterface {
private String string;
#Override
public int countNumbers() {
while (string.length() != 0 ) {
int count = 0;
for (int i = 0; i < string.length(); i++) {
if(Character.isDigit(string.charAt(i))) count++;
}
return count;
}
return 0;
}
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
#Test
public void testCountNumbers1() {
mycustomstring.setString("th1s strin8 shou1d hav3 numb3rs,
righ7?");
assertEquals(6, mycustomstring.countNumbers());
}
}
Well, according to the code you posted, you didn't create a MyCustomString object. That's probably the source of your NullPointerException.
Try adding mycustomstring = new MyCustomString() right above your setString method, like this:
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
#Test
public void testCountNumbers1() {
mycustomstring = new MyCustomString();
mycustomstring.setString("th1s strin8 shou1d hav3 numb3rs, righ7?");
assertEquals(6, mycustomstring.countNumbers());
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Keep getting the error, I can't seem to figure out why, the abstract method is implemented. I tried adding the keyword public, but that didn't help at all. I've read examples on the internet about abstract method, but mostl of them are void.
The abstract class:
public abstract class Osoba {
private String ime_prezime;
private char pol;
public Osoba(String ime_prezime, char pol) {
this.ime_prezime=ime_prezime;
this.pol=pol;
}
public String getImePrezime() { return ime_prezime; }
public void setImePrezime(String ime_prezime) { this.ime_prezime=ime_prezime; }
public char getPol() { return pol; }
public void setPol(char pol) { this.pol=pol; }
abstract int brojGodinaOsobe(Datum danasnji_datum);
}
And the subclass:
public class OsobaDatum extends Osoba{
private Datum datum_rodjenja;
public OsobaDatum(String ime_prezime, char pol, final Datum datum_rodjenja) {
super(ime_prezime, pol);
this.datum_rodjenja=datum_rodjenja;
}
public String toString() {
return "OsobaDatum: \n" + "Ime i prezime: " + getImePrezime() +
"\n" + "Pol: " + getPol() + "\nDatum rodjenja: "
+ datum_rodjenja.toString();
}
int brojGodina(Datum danasnji_datum) {
if ( datum_rodjenja.getMesec() < danasnji_datum.getMesec() ) {
return danasnji_datum.getGodina() - datum_rodjenja.getGodina();
}
else if (datum_rodjenja.getDan() <= danasnji_datum.getDan() &&
datum_rodjenja.getMesec() == danasnji_datum.getMesec()) {
return danasnji_datum.getGodina() - datum_rodjenja.getGodina();
}
else
return danasnji_datum.getGodina() - datum_rodjenja.getGodina() -1 ;
}
}
The abstract method's name is brojGodinaOsobe() and it looks like you only implemented a method called brojGodina(). Add the Osobe and you should be OK.
It looks like you are not overriding the method brojGodinaOsobe(Datum danasnji_datum); from Super class.
Change brojGodina(Datum danasnji_datum) to brojGodinaOsobe(Datum danasnji_datum) in the OsobaDatum class.
Regards.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm unable to test the hasNext() method in the IterableIntegerArrays class. If I call this method in IterableIntegerArraysTestCases, it says "cannot find symbol". How should I resolve this error?
public class IterableIntegerArrays extends IntegerRelation implements Iterable<IntPair> {
public IterableIntegerArrays(int n) {
super(n);
}
#Override
public Iterator<IntPair> iterator() {
return new Iterator<IntPair>() {
#Override
public boolean hasNext() {
//implementation
return false;
}
#Override
public IntPair next() {
//implementation
}
};
}
}
-
public abstract class IterableIntegerArraysTestCases extends IntegerRelationTestCases {
protected Iterable<IntPair> iterable;
protected void setIterable(final int n) {
setInstance(n);
iterable = ((Iterable<IntPair>) instance);
}
private void checkHasNext(int a, int b, boolean expResult) {
boolean result = iterable.hasNext(); //It says "cannot find symbol" here
assertEquals("hasNext(" + a + ", " + b + ")", expResult, result);
}
}
Iterable does not have hasNext method . Its Iterator which has hasNext method. You should do
iterable.iterator().hasNext()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can NullReferenceException happen here:
public class DataContextBinder extends Binder {
private static final String DATA_CONTEXT = "DataContext";
// Fields
private final Listener<PropertyChangedEventArgs<Object>> dataContextListener = new Listener<PropertyChangedEventArgs<Object>>() {
#Override
public void onEvent(PropertyChangedEventArgs<Object> args) {
setSource(args.getNewValue());
}
};
// Constructors
protected DataContextBinder(Object target, String targetPropertyName,
Binding binding) {
super(Property.getPropertyValue(target, DATA_CONTEXT), target,
targetPropertyName, binding);
}
// Methods
protected void createListeners(Object source) {
Log.e("DataContextBinder", "DCL = " + dataContextListener.toString());
// It throws NullPointerException on dataContextListener.toString()
Property.addPropertyChangedListener(getTarget(), DATA_CONTEXT,
dataContextListener);
super.createListeners(source);
}
protected void removeListeners(Object source) {
Property.removePropertyChangedListener(getTarget(), DATA_CONTEXT,
dataContextListener);
super.removeListeners(source);
}
}
dataContextListener is null..
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I hava a class StageDAO like this:
public class Stage{
public int stageID;
public String label;
public Stage(ResultSet rs) {
try{
this.stageID=rs.getInt("StageID");
this.label=rs.getString("Label");
}
catch(Exception e){}
}
}
I have a method in a class StageDAO, where i get Data from Database, like this:
public class StageDAO{
Connect connectdb;
public StageDAO(Connect connectdb){
this.connectdb=connectdb;
}
public Vector retrieveAll() {
ResultSet lobjRS=null;
Vector lobjList=new Vector();
Connection lobjConnection = null;
Statement lobjStatement=null;
Stage lobjStage = null;
try{
lobjConnection = this.connectdb.getConnection();
lobjStatement = lobjConnection.createStatement();
lobjRS = lobjStatement.executeQuery(
"SELECT * FROM Stage order by sortkey");
while(lobjRS.next()){
lobjStage = new Stage (lobjRS);
lobjList.add(lobjStage);
}
}catch(){}
}
}
in my GUi class i have this:
StageDAO lobjStage= new StageDAO (connectdb);
Vector<Stage> stageList = lobjStage.retrieveAll();//Here i have the information
of stageID and stagelabel
private JComboBox lcbstage;
public void initialize(){
lcbstage= new JComboBox();
for(int i=0; i<stageList .size();i++){
lcbstage.addItem(stageList.get(i).label);
}
}
But know if i select in my Gui the stage, i want to know the stageid.
I don't know how to get the stageid of the selected stagelabel ?
Thank you for your help.
Add the whole item to your JComboBox, not just the label, and create a toString() method which will return just the label:
public String toString(){
return this.label;
}
JComboBox's addItem takes an object and then converts it to a string for display, so when you do getSelectedItem() the whole object, id and label and everything else, will be returned.
public void initialize(){
lcbstage= new JComboBox();
for(int i=0; i<stageList .size();i++){
lcbstage.addItem(stageList.get(i));//this line changed
}
}