Cannot Return Object Of Iterator As Its An Interface
Mar 3, 2014What does the iterator() method return???it can't return an object of Iterator as it's an interface...
View RepliesWhat does the iterator() method return???it can't return an object of Iterator as it's an interface...
View RepliesI have been researching the Iterator and making a class implement iterable. I have seen this example shown below and was wondering how I could change this so that iterable() is not called upon in the main. I would like to be able to make a method that returns an instance of a class that implements the Iterator interface hopefully an inner class. This is because my program will not have a main and will be supplied with a main that includes a new Object with will use the iterator method.
import java.util.*;
public class IteratorDemo {
public static void main(String args[]) {
// Create an array list
ArrayList al = new ArrayList();
// add elements to the array list
al.add("C");
[Code] ....
This is all I have been able to understand from what I want to do. This does not work and this is what I am trying to achieve
public class MyArrayList implements Iterable {
public static final int DEFAULT_SIZE = 5;
public static final int EXPANSION = 5;
private int capacity;
private int size;
private Object[] items;
[Code] ...
I'm not new to java but i'm not able to solve the following issue: I have a class
public class Localizzazioni implements java.io.Serializable {
private <complexType> id;
public getId().......
public setId().....
The complexType is a class defined in the code somewhere. Now I want to access it in another class I have
Set localizzazioni = new HashSet(0);
localizzazioni=opere.getOiLocalizzazioneOperas(); -- this object give an object of tyoe HashSet
for(Object object : localizzazioni) {
object.get......... // i cannot use any method defined in the class Localizzazioni
}
Why I cannot write inside the for object.getId() and using it?? In other word how i can access the element contained in the object?? the object is an iterator of type Localizzazioni . The class Localizzazioni has some method but i cannot use them? why ....
How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.
When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.
// Here is what i tried to do:
Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.
Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).
Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.
class TwoDPoint {
int x = 2;
int y = 4;
}
class TestTwoDPoint {
public static void main(String args[]) {
TwoDPoint obj1 = new TwoDPoint();
System.out.println(obj1.x);
System.out.println(obj1.y);
[Code] ....
I'm working on an assignment where the program has to process a file and read every line then print it out in all caps. I'm pretty sure I have most of it written out, however, I am having trouble with my main method. I am supposed to call my go method in my FileProcessor class and have it use the StringProcessor interface to call my Upper class. I'm using an interface because I will be adding other classes later, but for now I am having trouble with implementing it all in my Driver class.
How do I declare a StringProcessor object in my Driver class and how can I use it so that it would create the file in all caps?
Here's my code so far:
Driver.java
import java.util.Scanner;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import java.io.File;
[Code].....
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}
else return false;
}
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
I'm having trouble understanding the concept of the interface Connection, and PreparedStatement.
1) The simplest way to put it is how is it possible that this code is creating Connection and PreparedStatement objects? I was always under the impression that interfaces cannot be instantiated, but rather implemented. For example I don't see "public class Prepared implements Connection", or "public class Prepared implements PreparedStatement", But I see "Connection con = null;" and "PreparedStatement pst = null;". So it seems as if the interfaces are being used to create objects called con and pst.
2) If in fact these interfaces are being implemented, where are the method blocks in this code that should have been added in order to fulfill the contract?
package zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
[code]....
jboss 7.1.1
EJB 2.1
Though it may seem strange but in one of the application i work on still uses EJB 2.1 entity beans.While looking at the deployment log, seems like each Entity bean is registered using both remote-home and remote interfaces.
java:app/EJBApp/Entity!com.abc.remote.Remote
java:app/EJBApp/Entity!com.abc.remote.RemoteHome
Using the remote-home's JNDI lookup i was able to get the EJBObject proxy and subsequently create and use the entity.But what about the remote interface JNDI lookup ? Reason i am asking is that one needs to create an entity before use it. That said, how to use the object that i get from remote interface JNDI lookup ? Note that the class of the returned object says its "com.sun.proxy.$Proxy13" type.The JNDI location i am using "java:app/EJBApp/Entity!com.abc.remote.Remote"
I am currently trying to use Junit to test a whole bunch of stuff. I almost have full line coverage but I am getting hung up on testing an if statement that consists of whether or not an object is an instance of another class. This class happens to be an interface, and even the object is an interface. Weird I know but I just want to know how to get into that if statement. I realize testing interfaces might be a waste of time but I still really want to know how. Here is an example of what I am trying to test:
Java Code:
if(x instance of y){ //where x and y are both interface objects
doSomething();
} mh_sh_highlight_all('java');
why interfaces inherit prototype of all the non final methods of the object class in itself? Object class is parent class of all the class and Interface is not the class.
View Replies View RelatedI need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?
View Replies View RelatedI need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?
View Replies View RelatedI'd like the code that would return an object's human readable name.ie. myObject.getObjectsName();Which would return the name of the object That is, the name of the object that I use as I code.In the code below, I'm trying to find the correct code to return the string,btnTTT_01..The results of the testing follow.
public static void main(String []args) {
tictactoe game ;
game = new tictactoe();
game.setVisible(true);
System.out.println ("main") ;
System.out.println ( "game.getName() is: " + game.getName() ) ;
System.out.println ( " " );
[code].....
I need to find the Winner using Object Orientation logic I have my old logic from my Tic Tac Toe game but it is not Object Orientated. So I want to convert it and add that code to my GUI Tic Tac Toe. I need to return a winner or tie.
View Replies View RelatedI don't know how to define "abs." Here are the instructions:
The purpose of this exercise is to add to the IntClass class below a method, abs, that takes as its only argument an IntClass object and returns a new IntClass object representing the absolute value of the argument.
Define the desired method as a class method.
Define the desired method as an instance method.
In each case, use the main method to test your code.
There's two boxes of editable code. I'm having trouble with the first one, the one with "abs."
Here is more code:
My code goes here
}
Here's another box of code that they want me to fill out:
public class MainClass
{
public static void main( String[] args )
{
My code goes here...
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1:
public class date {
public int day;
public int month;
public int year;
}
// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}
Suppose you have a generic Dog class of the pet type that implements a DogInterface of the pet type. What is the difference between;
DogInterface<pet> Rex = new Dog<pet>();
and
Dog<pet> Tye = new Dog<pet>();
In what situations might you want to use Rex instead of Tye?
a) I have a Ball Object which implements the Runnable interface and traces the various positions of a ball.
b) I then have a Ball_Bounce JPanel inside a JFrame which creates two instances of the Ball object and then paints them to the JPanel.
As per my understanding, when the main() program in Ball_Bounce.java is started, there a total of three threads running in this program, one for each ball and one for the main(). What I cannot understand is whenever the balls collide, I end up getting the "Collision" message twice even though the collision is checked only in the main() thread.
[#]public class Ball implements Runnable
{
private boolean xUp, yUp, xUp1, yUp1;
private int x, y, xDx, yDy;
private final int MAX_X = 500, MAX_Y = 500;
private boolean flag = true;
private static Thread ball;
[code]...
I am a beginner here at JAVA and I am trying to program a Gratuity Calculator using both interface class and object class but it keeps on compiling with errors saying "cannot find symbol".I tried everything to fix it but it just keeps on stating symbol.
[CODE]
public class GratuityCalculator extends JFrame
{
/* declarations */
// color objects
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
[code]....
I need to find out if one array list is a sub-sequence of another. The elements in the first list all need to be in the second list and must be in the same order. So s1<n, t, a> is a sub-sequence of s2<q, n, f, r, t, d, a>, but s1<a, a, t> is not a sub-sequence of s2<a, t, a>. I need to use iterators to go through each list, and I should only go through each list once (since it has to be in the same order) so nested loops don't seem like they would work because it would start at the beginning of one list every time it moved to another element in the outer loop's list.I seem to have an issue where the itr1. next()
is ignored when in an if statement.
My current code just stalls and will never stop running. I've also switched things around and put the not equal check after the if it is equal and it throws a NoSuchElementException.
import dataStructures.*;
public class Subsequence3
{
public static void main(String[] args)
{
ArrayList<Character> s1 = new ArrayList<Character>();
s1.add('n');
s1.add('p');
s1.add('a');
[code]....
I am trying to add the contents of the iterator to an arraylist so I can do other stuff to it, however I am getting an error when I actually try adding it to the list, stating that
"The method add(Map.Entry<String,myObject>) in the type ArrayList<Map.Entry<String,myObject>> is not applicable for the arguments (myObject)"
Here is what I have tried doing:
Iterator<Map.Entry<String, myObject>> iterator = hash.entrySet().iterator();//hash is my HashMap object
ArrayList<Map.Entry<String, myObject>> list = new ArrayList<Map.Entry<String, myObject>>();
while(iterator.hasNext()){
Map.Entry<String, myObject> entry = iterator.next();
list.add(entry.getValue());//error here
}
I am trying to use an iterator instead of a foreach loop to go through a list of inventories to see if a car is rearDrive and count how many are rearDrive but somehow I seem to be missing something that the counter isn't working as expected.
public int howManyAreRearWheelDrive(){
int i = 0;
int counter = 0;
int inventSize = inventory.size()-1;
while(i < inventSize){
boolean wheelDrive = inventory.get(i).getIsRearWheelDrive();
if( wheelDrive == true){
counter++;
}
}
return counter; // returning here doesn't give me anything
}
The Iteratior provides the functionality of traversing and removal which we can achieve through normal for loop and remove() of the data structure.Then, why do we need Iterator explicitly and as an inner class?
View Replies View RelatedAs part of a homework assignment in my 1st Java Class, I am creating my own Circular Generic LinkedList and Array class. My class uses the Queue Interface Extends Iterable but I am creating my own methods to work with. For the most part, I believe I have been successful in creating the class aside from one method. That method is the Iterator<E> iterator().
/**
* Return a fail-fast iterator, which throw a java.util.ConcurrentModificationException
* if the client modifies the collection (via enqueue(...) or dequeue()) during iteration.
*/
@Override
public Iterator<E> iterator() {
I don't understand how an iterator, let alone a "fail-fast" iterator ties into my project. I've spent hours reading up on a way to imploy my own generic fail-fast iterator but to no avail.
I feel like I could come up with some workable code if I knew what the point is to useing a user-defined, non Java Library iterator is to do.
As well, does throwing a ConcurrentModificationException require a try and catch block?
I have a problem with a linked list iterator, my problem is implementing the iterator.remove method given the following code:
package mapsql.util;
import java.util.Iterator;
public class LinkedList<T> implements List<T> {
private class Node implements Position<T> {
T element;
Node next, prev;
public Node(T element) {
this.element = element;
[Code] ....
I am tring to run LDA to generate some topics from txt files as the following one:
Document1 label1 forest=3.4 tree=5 wood=2.85 hammer=1 colour=1 leaf=1.5
Document2 label2 forest=10 tree=5 wood=2.75 hammer=1 colour=4 leaf=1
Document3 label3 forest=19 tree=0.90 wood=2 hammer=2 colour=9 leaf=4.3
Document4 label4 forest=4 tree=5 wood=10 hammer=1 colour=6 leaf=3
Each numeric value in the file is an indication of the number of occurrences of each feature (e.g., forest, tree) multiplied by a given penalty. To generate instances from such a file, I use the following Java code:
String lineRegex = "^(S*)[s,]*(S*)[s,]*(.*)$";
String dataRegex = "[p{L}([0-9]*.[0-9]+|[0-9]+)_=]+";
InstanceList generateInstances(String dataPath) throws UnsupportedEncodingException, FileNotFoundException {
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
pipeList.add(new Target2Label());
[Code] .....
I then add the so-generated instances to my model using the instruction model.addInstances(generatedInstances). The resulting output is described below.
It contains errors caused by the instruction model.addInstances(generatedInstances). Debugging my code showed me that the alphabet associated to the model is null. Am I using the wrong iterator?
name: document1
target: label1
input: TokenSequence [forest=3.4 feature(forest)=3.4 span[0..10], tree=5 feature(tree)=5.0 span[11..17], wood=2.85 feature(wood)=2.85 span[18..27], hammer=1 feature(hammer)=1.0 span[28..36], colour=1 feature(colour)=1.0 span[37..45], leaf=1.5 feature(leaf)=1.5 span[46..54]]
Token#0:forest=3.4 feature(forest)=3.4 span[0..10]
[Code] ....