|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Thanks guys 
followup question, for some of you that have used arraylists a bunch
my arraylist holds objects, and the objects have like, 2 ints and 3 strings
Is there an easier way to print a string of all the fields (ints and strings in every element) than to make a loop go through each element, and manually add every int and string of each element into one long string and then print the string
or is that what I need to do? I was hoping there would be like, a method that would do that for me I saw toString and tried that but it just returns the reference of the element of the arraylist instead of the actual fields of the element of the arraylist
so where I want something like "Tim 5 The Matrix Interstellar Joe 10 Independence Day", instead I am getting "mediaRentalManager.Customer@4e50df2emediaRentalManager.Customer@1d81eb93mediaRentalManager.Customer@7291c18f"
So, do I need to do this the long way I described?
|
Wait what? Please don't try to use Object as a way of getting around Java's strong typing. As a beginning programmer, if you are using Object, you are doing it wrong (and most of the time, as an experienced Java programmer as well).
Tell us what you're trying to store in that arraylist, and we'll tell you a better way of doing it...
As for final classes: imho the use of final as a class modifier is retarded and doesn't make sense for Java (invent a different keyword). Final means immutable everywhere else, but for classes it means unextendable. It's dumb. But it's the way things work. Basically: don't make classes final unless you have a very good reason to (and as a beginning programmer, you probably will not have a good reason to make a class final, the situations that makes sense are quite rare and obscure)
|
Okay I will describe what I have to do a bit better.
I am making a program that manages/tracks the renting of movies. One specification of this program is that I have to keep track of customers (with an arraylist).
So I made a class for customers, and in it I have the necessary info. This includes things like their name, what they've rented, etc.
So, I have a method that is like addCustomer(String name, String rented, etc)
Then inside that method I've got
Customer currentCustomer = new Customer(name, rented); Database.CustomerArrayList.add(currentCustomer);
Now the program also needs to be able to, when requested, print out all the info for every customer. What's the best way to do that? Or is my approach all wrong?
|
Honestly, if all you're trying to do is print all the variables of objects into string format, then having an Array of Objects isn't really a terrible way to do it...not entirely sure why you'd want to, but it's not necessarily bad.
Important thing is that the ToString method can be (and for many cases, should be) overridden. So you're basically on the right track, you just need to actually define ToString in all of your classes to give a meaningful output.
But in a lot of cases, if your thought process is "I wish there was a method that did X", then the real solution is to make the method yourself.
EDIT: Okay, now that you've updated that...yeah, just override ToString in the Customer class to do what you want. Or write a new Method that prints all the variables as a string and then call that.
|
Well the project specifications require that we use arraylists to store all media objects and all customer objects.
But now I am about to ask a question I feel really, really stupid about asking.
I have an arraylist of objects (all the same type). Those objects all have a field in common (a string)
HOW DO I REACH THAT STRING. lol I feel so dumb for even having to ask this, but it seems like I can return an element of an arraylist but I can't actually reach the information contained in that element.
If I do arraylist.get(index) - there is no option for like .variable or .field or anything which is different than normal arrays? IIRC I could just do system.out.print(array[index].variable)
I am guessing I have to do something like
String allInfo = "";
for(loop through everything){ Customer temporaryCustomer = arraylist.get(index) allInfo = allInfo + temporaryCustomer.whateverString; }
return allInfo;
just seems weird. like there would be an easier way to do it lol
|
No. That looks good (except for the static use, and the exposure of local variables). But you don't have Integer and Strings in your arraylist, you have customers. So you need to define a pretty printer for your customers. Add a toString for your customers that does something like:
return name + " " + rented;
Then you just loop through your arraylist and println all of your customers (this will call toString on each of your customers and print that on a line). The default toString (inherited from Object) is simply the object name and its pointer, which is what you were complaining about earlier.
Now, addressing your Database, it should not just be exposing its innards to the rest of your program. That is not very OOP. Your model (in a model-view-controller architecture) should provide useful getters and setters to your data. So in this case, you should not access your customerArrayList directly from wherever you are doing that now, but you should have an addCustomer method in your Database object which does that functionality. To get your Customers, you should then just call db.getCustomers() (assuming db is a Database object, or... if you really want to do it statically, Database.getCustomers()). There is some movement to declaring final public fields instead of using getters and setters, but I'm not yet aboard. I find getters and setters are easier to debug even if they add (many) extra lines of code.
Remember that a final ArrayList (in java)doesn't mean you cannot add or delete elements to your array list. It just means you can only declare it as a new ArrayList once (when it is created): the pointer is immutable, not the object.
|
You are accessing the variable the correct way. Maybe it's private?
Use a StringBuilder. Those are nicer than what you are trying to do.
When you loop through everything your might want to use a foreach loop: for (Customer c : database.CustomerArrayList) { stringBuilder.append(c.whateverString); }
return stringBuilder.toString();
|
okay thank you that is very helpful
last question(I think)
I think I am using ArrayList wrong? Am I accidentally creating some sort of generic form of the arraylist that holds unspecified objects, and that's why I can't reach the fields within the elements?
Do I need to use new ArrayList<Customer> or something?
|
Confusion was if you were using Objects, or just talking about objects in general. AKA, the difference between ArrayList<Customer> and ArrayList<Object>.
With an ArrayList, the get() function actually returns the object at that index, so you should keep that in a local variable. I'll be honest, this is really, really basic stuff, so this is something you should really try to understand before you move on further.
After that, I'd recommend looking up "foreach" loops.
EDIT: Okay, I guess you do understand those to some degree. Getting very confusing trying to figure out what you know and what you don't .
Answer is yes, you're apparently using an extremely generic ArrayList type. Object is the class that every other class extends from, which is why it will "work". If you want to call your class functions, you need the ArrayList to specify its of that type.
|
On December 06 2015 04:05 travis wrote: okay thank you that is very helpful
last question(I think)
I think I am using ArrayList wrong? Am I accidentally creating some sort of generic form of the arraylist that holds unspecified objects, and that's why I can't reach the fields within the elements?
Do I need to use new ArrayList<Customer> or something?
You should be using an ArrayList of customers, but that wouldn't be why you can't access the fields. Did you declare the fields of your Customer class as private (you should have to encapsulate them). You'll need to make getters (and setters if the data needs to be changed) to access the fields. + Show Spoiler [example code] +public class Customer {
private String name; private int age;
public Customer(String name, int age) { this.name = name; this.age = age; }
public String getName { return name; }
public int getAge { return age; } } then you can access the field by doing: arraylist.get(1).getName; // returns the name of the Customer at index 1 arraylist.get(2).getAge; //returns the age of the Customer at index 2
|
Okay thank you for being patient hehe That was definitely my main problem, using the arraylist wrong
but this has been very helpful as to how I need to design this
no blitzkrieg I didn't set them private, though I know I should
even though sometimes it does seem like using getters and setters for everything is a pain in the ass but my teachers insist it's good programming style
I'll admit I don't complete understand the necessity to always do it though, I actually don't even know what reasons there are, other than being able to change the getter/setter one time instead of having to potentially change your code in tens hundreds or thousands of other places
so, uh, actually I guess that is a pretty good reason lol
|
On December 06 2015 04:05 travis wrote: okay thank you that is very helpful
last question(I think)
I think I am using ArrayList wrong? Am I accidentally creating some sort of generic form of the arraylist that holds unspecified objects, and that's why I can't reach the fields within the elements?
Do I need to use new ArrayList<Customer> or something? Yes. The default is ArrayList<Object> (and your IDE should be all up in your face with warnings and stuff if you leave the generics out). When you call get() on an arraylist with Objects, it will return an object of (duh) type Object. Now YOU know that that thing is actually a Customer, but Java doesn't (yet). You thus have to cast it to Customer first. This was the normal way of doing things before generics. Now, however, generics do all that work for you. Declare your Arraylist<Customer> and get() will return an object of type Customer for you, which will have the fields you're looking for.
@Blitzkrieg: to be honest, I can't remember what happens if you do
ArrayList<Object> foo = new ArrayList<>(); foo.add("bar"); String barString = foo.get(0);
In my mind this should be uncompilable. However, it might just be some really nasty warnings about implicit casts. I honestly don't remember.
|
What about using setters in a constructor?
For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed).
Is there a point to using setters in a constructor ?
|
On December 06 2015 04:19 travis wrote: What about using setters in a constructor?
For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed).
Is there a point to using setters in a constructor ? No. In fact, declare those variables final if they are only ever supposed to be set in the constructor.
|
It's still not a bad idea to use setters. Really, the entire point of having getters and setters is to control how values go in and out of the class, so you only have to look in one place if things need to change or if things go wrong.
Realistically, for smaller projects, you don't need them because you know roughly what everything is going to do. Eventually, though, you'll come to situations where you're not in control of everything, and the scope of your project extends beyond your initial expectations, so it's good to have the framework in place so you don't have to do major code rewrites every time.
(Of course, C# is so much nicer for getters and setters..."public string Word { get; set; }"...)
|
On December 06 2015 04:19 travis wrote: What about using setters in a constructor?
For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed).
Is there a point to using setters in a constructor ?
The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason.
On December 06 2015 04:14 Acrofales wrote:@Blitzkrieg: to be honest, I can't remember what happens if you do ArrayList<Object> foo = new ArrayList<>(); foo.add("bar"); String barString = foo.get(0);
In my mind this should be uncompilable. However, it might just be some really nasty warnings about implicit casts. I honestly don't remember. Your code would require a cast to work. I was thinking along the lines of
ArrayList<Object>foo = new Arraylist<>() Customer test = new Customer(name, age); foo.add(test); String name = food.get(0).getName(); That should compile and work due to polymorphism.
|
On December 06 2015 04:25 WolfintheSheep wrote: It's still not a bad idea to use setters. Really, the entire point of having getters and setters is to control how values go in and out of the class, so you only have to look in one place if things need to change or if things go wrong.
Realistically, for smaller projects, you don't need them because you know roughly what everything is going to do. Eventually, though, you'll come to situations where you're not in control of everything, and the scope of your project extends beyond your initial expectations, so it's good to have the framework in place so you don't have to do major code rewrites every time.
(Of course, C# is so much nicer for getters and setters..."public string Word { get; set; }"...) Yeah sure. I still prefer final and setting them directly in the constructor It just better captures what those variables are all about imho. Of course, you have to think about your project first. For instance if you store "age" in a variable, then you'd better have a way to change that age every year. However, if you store "birthdate", you can just compute the age, and birthdate is immutable.
You'd also better be bloody sure you know at construction time what the value of those variables is, otherwise birthdate will be 0 (or null depending on long or object storage) forever. But if those are requirements, a final variable is great (and by definition doesn't need a setter).
|
On December 06 2015 04:32 Blitzkrieg0 wrote:Show nested quote +On December 06 2015 04:19 travis wrote: What about using setters in a constructor?
For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed).
Is there a point to using setters in a constructor ? The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason. Partially, and the debate still rages. But it is often argued that using the setter from within the class is also best, because it better controls the flow. By going to "setFoo" and finding everything that references it, you will know EVERYWHERE that could possibly be fucking with the value of foo. If you allow class methods to set foo without calling setFoo, then it's harder to debug (you now have to find everywhere within the class that sets foo, and everywhere outside the class that calls setFoo).
|
Now I am getting a warning, is it a legitimate warning, or just because eclipse doesn't understand what is happening
I have a "media" class, and it is a super class for my "movies" class, and my "music" class. The music and movies have some stuff in common, and the project requires basic use of inheritance, so I structured it this way. Plus you need to have an arraylist for media, not an arraylist for movies and an arraylist for music.
so, in my code I do:
Movies newMovie = new Movies(title, copiesAvailable, rating); Database.Media.add(newMovie);
as well as
Music newMusic = new Music(title, copiesAvailable, artist, songs); Database.Media.add(newMusic);
but java is giving me a warning "the method add(object) belongs to the raw type of Arraylist, References to the generic type of arraylist need to be parameterized".
Can I just ignore this warning or is there something else I should be doing
|
On December 06 2015 04:39 Acrofales wrote:Show nested quote +On December 06 2015 04:32 Blitzkrieg0 wrote:On December 06 2015 04:19 travis wrote: What about using setters in a constructor?
For purposes of this specific project I am doing, I won't need to ever set those values once they have been constructed, so I feel like I don't really need to use setters (though getters are needed).
Is there a point to using setters in a constructor ? The point of a setter is that another class can't access the private field. The constructor can change the private field in the class because it is part of the class. Using the setter in the constructor doesn't make sense for this reason. Partially, and the debate still rages. But it is often argued that using the setter from within the class is also best, because it better controls the flow. By going to "setFoo" and finding everything that references it, you will know EVERYWHERE that could possibly be fucking with the value of foo. If you allow class methods to set foo without calling setFoo, then it's harder to debug (you now have to find everywhere within the class that sets foo, and everywhere outside the class that calls setFoo).
In any other method sure, but in the constructor? Using the setter in the constructor just feels stupid to me.
On December 06 2015 04:39 travis wrote: Now I am getting a warning, is it a legitimate warning, or just because eclipse doesn't understand what is happening
I have a "media" class, and it is a super class for my "movies" class, and my "music" class. The music and movies have some stuff in common, and the project requires basic use of inheritance, so I structured it this way. Plus you need to have an arraylist for media, not an arraylist for movies and an arraylist for music.
so, in my code I do:
Movies newMovie = new Movies(title, copiesAvailable, rating); Database.Media.add(newMovie);
as well as
Music newMusic = new Music(title, copiesAvailable, artist, songs); Database.Media.add(newMusic);
but java is giving me a warning "the method add(object) belongs to the raw type of Arraylist, References to the generic type of arraylist need to be parameterized".
Can I just ignore this warning or is there something else I should be doing
When you create the object it needs to be a Media. The ArrayList holds Media. Movie is a Media. Music is a Media. When you get the objects from the ArrayList later polymorphism will know which methods to use.
Media newMovie = new Movies(...); Database.Media.add(newMovie); Media newMusic = new Music(...); Daatabase.Media.add(newMusic);
|
|
|
|