PDA

View Full Version : learning to code [Java problem]


cablegunmaster
03-25-2009, 05:33 PM
Ive got trouble making 2 assignments.

im making assignments from the book "Objects first with Java"




SOLVED
im on chapter 4. 3rd edition 4.25
It asks me to make a join statement.

public void join(Membership members)
{
String naam = "test";
int month = 11;
int year = 12;
Members.add(new Membership(naam,month,year));
}


Ive got a Membership object and add it. (it has the same values as the hardcoded one) How can I complete the join method?
I fill in the object but it only adds the hardcoded data.


Suggestion

Members.add(new Membership(Membership.getName(),Membership.getMonth(),Membership.getYear()));


this is what I thought just calling the functions in the other Object but I dont know how.


First solution Worked but differently:

Members.add(new Membership(members.getName(),members.getMonth(),members.getYear()));

Question 2 Unsolved
members is probably where they store all the data's just for the record is members a object? or what is it?

assignment 4.26
I have to make a Close() function where you have to close all the
numbers in the arraylist where the string is "lalalala"
But I dont know how to do a Contain function. on a Iterator. I just dont get it.

public void close()
{
Iterator<Lot> it = lots.iterator();
{
while(it.hasNext())
{
if(it.hasNext())
{
System.out.println(it.next());
}
}
}
}

Ive got this far but I dont know how to make a contains on a String.
it should be something like this :


public void close()
{
Iterator<Lot> it = lots.iterator();
{
while(it.hasNext())
{
if(it.hasNext().contains("Notbiddedfor"))
{
System.out.println(it.next()+" is not bidded for" );

}else{
System.out.println(it.next()+" is closed" );
}
}
}

Fou-Lu
03-25-2009, 06:53 PM
Umm.... I'm not sure exactly what you're question(s) are.
About the join.
What you have there is correct. That assumed that the Membership is not an instanceof Cloneable but instead uses an overloaded constructor taking the name, month and year. The only difference is that you need to extract the values from the passed in members variable instead of hardcoding them. The easiest way since the data is primitive would be to implement Cloneable on you're Membership class, and then you can simply use: Members.add(membership.clone());

'Members' is a collection, that is likely generic to <Member>. This one contains the add method, which generally indicates a type of collection or list.

The iterator is a typeof <Lot> iterator. This means that it.next() will return a Lot object. Comparisons can be done by overriding the equals method or by checking the exact fields (among other routes like comparable and comparitor). Sounds to me like you want to check the exact fields (string does have a .contains method on it), but you do not access the property to do the comparison on. Without it, it will use the toString method indicated on the Lot object which is not likely what you want.

I think that covers what you asked.

cablegunmaster
03-25-2009, 07:20 PM
I want to check if in that arrayList the words "notbid" are present and because I want it to check for every one , I used a Iterator.

but how do I check with .contains?
if(it.hasNext().contains("notbid")) ?

how do I formulate this?

Fou-Lu
03-25-2009, 07:33 PM
You can't; the iterator indicates that the objects within it are typeof Lot. I don't know what the Lot class is, so I can't tell you where to look on it. This would work if the list was a typeof <String> since the ArrayList has an implemented contains method on it as well, so you could do a this.lots.contains("string"); on the arrayList if it was storing just strings.

Depending on the type of list it is (as in, ArrayList, Vector, etc), you may be dealing with possible null values as well.
So you'll be looking at something like this:

public void Close()
{
Iterator<Lot> it;
Lot item;
string bid; // something like this, a field within Lot?
it = this.lots.iterator();
while (it.hasNext())
{
item = it.next();
if (item != null)
{
bid = item.getBid(); // I have no idea if this method exists.
if (bid.equals("notbid"))
{
System.out.println(item.getADisplayName() + " is not bid on...");
}
else
{
System.out.println(item.getADisplayName() + " is closed.");
}
}
}
}


I can give you better information if you post the Lot class.

cablegunmaster
03-25-2009, 07:45 PM
this is what the class says this is what gets added.
getvalue = a number for the bid like 20000
number = a number you put in
im curious about whether the string contains "(No bid)"
This is what get put in the array

example : 1: Car Bid: 20000
Example2: 2: House (No Bid)

public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Bid: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}

Fou-Lu
03-25-2009, 07:52 PM
This is in the toString method, so contains is what you want to use. Since the toString exists, you can work directly off of the Lot object.

public void Close()
{
Iterator<Lot> it;
Lot item;
string lotString;
it = this.lots.iterator();
while (it.hasNext())
{
item = it.next();
if (item != null)
{
lotString = item;
if (lotString.contains("(No Bid)") )
{
System.out.println(item + " is not bid on...");
}
else
{
System.out.println(item + " is closed.");
}
}
}
}



Something like that should work.

If you have access to more information on the object through getters, I'd check to see the value that the bid is at instead of relying on the contains method.

cablegunmaster
03-25-2009, 08:02 PM
Appearantly The question was:

" How do I make from a Object in a arraylist an String "
String Lotstring

object = object
item = it.next();

String = object
lotString = item;

How do you solve this puzzle?

here you can see all the code;
http://www.cablegunmaster.nl/biddingsystem.rar

Fou-Lu
03-25-2009, 08:21 PM
I don't understand what you're question is?
I can't open that either since I'm at work.
An object can be assigned to a string since it has a toString method to use. toString is enforced by java.lang.Object, so every object in Java has a toString on it - the difference is that the default returns some type of memory location for it, which is why you need to override it, or you'll get a result like @185f33 for example.
What you *may* be able to do, and I can't confirm this since I don't recall ever trying before, is to use an Iterator of string generic, that may work with object. That would give you you're iterator as Iterator<String> instead of lot, which would return a string from the iterator.next() call instead of a lot. That *may* work.

cablegunmaster
03-25-2009, 08:25 PM
I don't understand what you're question is?
I can't open that either since I'm at work.
An object can be assigned to a string since it has a toString method to use. toString is enforced by java.lang.Object, so every object in Java has a toString on it - the difference is that the default returns some type of memory location for it, which is why you need to override it, or you'll get a result like @185f33 for example.
What you *may* be able to do, and I can't confirm this since I don't recall ever trying before, is to use an Iterator of string generic, that may work with object. That would give you you're iterator as Iterator<String> instead of lot, which would return a string from the iterator.next() call instead of a lot. That *may* work.

The whole purpose of that Iteration is to say that <Lot> is an object you fill in from another class called Lot. you cant fill in a Lot anymore if you do it this way!. The array would not be filled. meaning there should be another way. Ill ask tomorrow :) You have helped me allot ^^

* continues with some reading * :) Thanks

Fou-Lu
03-25-2009, 08:51 PM
Don't be mistaken on an Iterator versus a list. The list itself keeps you're collection of datatypes, while the Iterator can be moved between different types. This helps you handle access on more abstract types of Lists providing runtime casting. What would prevent you from adding 'Lot' objects is if you changed you're List to a <String>, not the actual iterator.
Sadly, like I said in the previous post, I don't think that you can just move you're Iterator into a string. The difference is that the List is a collection of lots which are not a subclass for String, so I don't think the iterator would actually succeed. On the other hand, Lot has a toString on it, so it may work. Only one way to find out though ;)

cablegunmaster
03-25-2009, 09:22 PM
public void close()
{
Iterator<Lot> it;
Lot item;
String lotString;
it = lots.iterator();
while(it.hasNext())
{
item = it.next();
if (item.getHighestBid() == null)
{
System.out.println(item + " is not bid on...");
}
else
{
System.out.println(item + " is closed.");
}
}
}


this works :)

Fou-Lu
03-25-2009, 09:33 PM
Great, thats what I was talking about a few posts up when I had this:


If you have access to more information on the object through getters, I'd check to see the value that the bid is at instead of relying on the contains method.


There is always a way ;)

cablegunmaster
03-25-2009, 09:51 PM
Great, thats what I was talking about a few posts up when I had this:



There is always a way ;)


well I will have more problems in the future.

tranceftw
04-07-2009, 05:01 PM
i've send you a message... I hope you got it, but if you haven't get it... then say something here.