Empty array object creation in Java - java

I am trying to learn some foundational ways to manipulate certain aspects of coding that stray away from single use and make something more "dynamic" that can kind of build itself on the fly.
For example of what I would like to accomplish, which I believe would consist of using some type of empty array of an object and a loop. I just don't know how to write it.
Lets keep it basic and say I have a Person Class.
public class Person
{
String name;
String age;
}
So in my TestDrive I would just get a simple user input to gather information on that person.
Such as...
import javax.swing.JOptionPane;
public class PersonTestDrive
{
public static void main(String[] args)
{
String name;
String age;
name = JOptionPane.showInputDialog(null, "Enter your name");
age = JOptionPane.showInputDialog(null, "Enter your age");
Person human = new Person();
human.name = name;
human.age = age;
JOptionPane.showInputDialog(null, "Would you like add another entry?");
/* At this point it would loop if the user wanted to add another entry.
I would just wrap it all in while loop that just checked if the user said yes or no.
However, if they do choose to add another entry
"IF" a human object already existed it would create a
new human2 object, and then human3, etc. */
}
}

It sounds that all you need is a collection of objects, such as ArrayList<Person>.
"human" is a name of your variable, and at compile time you don't know how many other variables there can be so you cannot refer to them in the code using "human2", "human3" and so on. You can create these variables, but they may be nulls and your input will also be limited to how many variables you have. Another problem would be keeping track of what variable to assign to next.
With List<Person> list you can do list.get(2) to get third object (it will thrown an exception if there are few than 3) or list.size() to check how many objects were created so far.
Here is some more information about Java collections: http://docs.oracle.com/javase/tutorial/collections/

Related

How to construct a user(/input)-specified number of objects

I'm currently doing an intro level undergrad CS course (learning basics of 'program'&'class' building using Java).
The relevant part of my personal (&job related) project: I have a list of zip codes associated with One county.
I'm gonna define a class called 'County'. Then I'm gonna use this class to construct an object of type County, called 'middlesex'.
ie: County middlesex = new County();
Now, in this object, I want to construct a number of objects of class-type ZipCode.
ie: ZipCode objName = new ZipCode();
(Each such ZipCode object is gonna contain certain instance data).
My problem is this. Assume that I don't know how many zipcodes are contained in the Middlesex county. However, I have a .txt file that contains just a list of all the zipcodes of Middlesex county.
Let's say there are n number of zipcodes in this list.
In the object 'middlesex', of class-type 'County', I want to set up a loop. This loop will scan each zipcode in the list, then construct an object of class-type ZipCode for each zipcode.
Thus the loop may go thru n iterations, and construct n objects of type ZipCode.
Thus, for every iteration of the loop, a unique object-reference-name must be created (corresponding to the particular zipcode in the list).
Part of this problem (but distinct and optional), is that I want to know how (if possible), I can set up a structure that allows an inputted (scanned) string to be used as the name of an object-reference.
I apologize if I've made incorrect terminology use. I know that many are gonna suggest arrays. I haven't learned about them yet, but I gotta read about them over this weekend for school. I'm just gonna try to figure this out for a day or two, and then just move on to using arrays to perform this task.
So, if I've made any sense to anyone, is what I'm trying to do possible without arrays?
Thank u.
You're describing a very basic scenario, one where one object contains (possibly) many references to objects of a 2nd type, what we call a constructor called "composition" where the relationship here is a "has-a" relationship, County has-a (or has-many) zip codes
As opposed to using inheritance to wrongly try to solve this, the "inheritance" relationship or the "is-a" relationship -- County is not a zip code and zip code is not a county.
The code to create this can be very simple, something like:
import java.util.ArrayList;
import java.util.List;
public class County {
private String name;
private List<String> zipCodes = new ArrayList<>();
// constructor that takes county name
public County(String name) {
this.name = name;
}
public void addZipCode(String code) {
zipCodes.add(code);
}
// ..... more code
If a zip code is a single String, then no need to create a new class for this. If however it is more complex and holds more data than a single String, then create a class for ZipCode, and change the code above to something like
import java.util.ArrayList;
import java.util.List;
public class County {
private String name;
private List<ZipCode> zipCodes = new ArrayList<>();
// constructor that takes county name
public County(String name) {
this.name = name;
}
public void addZipCode(ZipCode code) {
zipCodes.add(code);
}
// getters, setters, a decent toString method override...
Where ZipCode could contain....
public class ZipCode {
String code;
// other fields if needed....
public ZipCode(String code) {
this.code = code;
}
// ....
then when reading in data, create your County objects and as each Zip code is read in, add it to the appropriate County object using the addZipCode(...) method.
zipCode is an object of Type ZipCode then what are its fields? Think of the reasons for making it an object and not a variable
"Thus the loop may go thru n iterations, and construct n objects of type ZipCode"
Unforutnality this is not possible without making the use of Arrays
"structure that allows an inputted (scanned) string to be used as the name of an object"
Nope can do that.

call existing class by name in String variable

Let's say I have a
public class things {
String name = new string;
int nr = new nr;
//constructor etc
}
Now let's assume there are already a bunch of instances initialized. let's call them thingA, thingB, thingC (I will need them to be more later...)
I'm having a JFrame where the User can Input a text and the input shall be one of the classes so he could type "thingB", "thingD", ... "thingN"
within an actionPerformed I now want to:
String s = jLabel_1.getText();
and then do sth like.
System.out.println("" + s.nr)
Obviously, that doesn't work since s is a string only containing the value entered in the text-label, so what I wanna do is tell Java that the object's name I'm trying to use is the one saved in 's'. I really tried to look it up but I couldn't find anything except a
Class.forName(String...) option which didn't really work either, respectively I didn't know how to use it.

create multiple instances of a class without array in a loop

I know this question been asked many times, and I've looked through many similar topics but they all include array or list and I don't want to use array or lists, second I don't understand :).
Anyway here's my question: I have a class with name,id,job methods to store these in a variable, and I want to to ask how many Staff the company has, and that's why I cant hard code each variable because the number is unknown thus I need a loop, And am not allowed to use array or list. Any idea or help is appreciated Thanks.
Here's the code for the class:
public class Staff{
public void name(String name){
String staff_name = name;
}
public void id(String id){
String staf_id = id;
}
public void job(String job){
String staff_job = job;
}
}
code for main:
import javax.swing.JOptionPane;
public class P2Q2{
public static void main(String[]args){
System.out.println("How many staff in Department? ");
String staff_num = JOptionPane.showInputDialog(null,"How many Staff are there",
"Department staff numbers",JOptionPane.QUESTION_MESSAGE);
int staff_num_int = Integer.parseInt(staff_num);
for(int i=1;i<staff_num_int;i++){
//somthing in here to create objects for each staff
}
}
}
Here is the whole question:
EDIT:
I just didn't want to provide the whole question, but if this is better there you go.
You can create Staff objects without array or list - but it will not have sense because you will not have access to the objects. If you will not have access to the objects - that means the Garbage Collector will destroy them after you go out the loop scope.
Well if you can't use an array or list, and you want to access to each staff object. You can look into using something such as a hash map. You can store objects in the hash map and just use the staff names as a key to access each object after created and added to the map.

Multiple instance of class versus array Java

My question which of the following examples represents the right practice ?
What are the advantages and downsides of these approaches.
Is there another(right) way to achieve this?
Let's say I have class
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String print() {
return this.name;
}
}
And use of class like:
public static void main(String[] args) {
List<Person> people = new ArrayList<Person>();
people.add(new Person("Jane Doe"));
System.out.println(people.get(0).print());
}
And then another way to do this:
public class Persons {
private ArrayList<String> persons;
public Persons() {
persons = new ArrayList<String>();
}
public void putPerson(String name) {
persons.add(name);
}
public String print(int id) {
return this.persons.get(id);
}
}
And use of that:
public static void main(String[] args) {
Persons persons = new Persons();
persons.putPerson("John Doe");
System.out.println(persons.print(0));
}
EDIT:
Assume I have 10 000 of these persons.
Is there any downside to creating 10,000 instances of the class?
I would prefer the first way because there is a rare need to create a wrapper class for keeping a collection (like Persons -> Person). It makes sense if the class Persons gets renamed to a Company/Community, which contains a List of workers/persons and provides specific operations over this list.
public String showAllMembers() { ... }
public void notifyAllMembers() { ... }
Also, the second way breaks the Single responsibility principle. It shouldn't take care about printing a person, the class is responsible for adding/removing them. The Persons can provide to you a specific Person, then you have to call a method print() on a given instance:
Persons persons = ...;
persons.getMember(10).print();
Lets say i have 10 000 of these persons. Is there any downside to create 10 000 instance of class?
In any case, you will have to create 10000+ instances. Consider,
10000 Persons + a List
10000 Persons + a Persons + a List
The first one is more object-oriented than the second, which becomes apparent as soon as you start adding more properties to a person.
For example, consider adding a date of birth to a person. When you have class Person, you modify the class, and everyone who has access to it will be able to get it. You will also be passing the date of birth with the Person object, so any method that takes Person as a parameter will have access to that person's date of birth:
static void displayPerson(Person p) {
// Here, we can print both the name and the date of birth / age
}
public static void main(String[] args) {
...
displayPerson(people.get(0));
}
The second approach would require adding a parallel collection to the Persons class. All users of Persons would get access to date of birth, but unless a method takes the full collection as a parameter, it would have access to only the properties that the caller takes from the collection:
void displayPerson(String name) {
// Here, we have no access to person's date of birth
}
public static void main(String[] args) {
...
displayPerson(persons.print(0));
}
In Java, the first approach is the one to go with. It is more in accordance with the OOP way of doing things.
While there is nothing structurally wrong with the second, it doesn't follow the OOP architecture. You may do this kind of operation in C.
In the first approach, you create a Person class which defines what a person is - its properties, methods, etc. This is the object in Object oriented programming. When you need a Person, you then instantiate one and add it to a list of people.
In the second, you create a array, essentially. You can then create an instance of it and fill in the properties you want. However, I see the following drawbacks for this approach:
The object you're adding to the class doesn't exist anymore. Nowhere have you defined what a person is and what properties it has. These properties only exist in the values you add to the array. This can get very confusing and risky after a while.
No getters and setters. Every operation to retrieve a specific property and update it will result in very complex and redundant iterations on your array. In addition, your class can potentially have a very large number of properties, and every time you want to update a property, you'll have to be very careful to get the right index of that property in the array. It's a recipe for disaster.
Also, a putPerson method which just adds a value to the array? Cringy.
Short answer: don't do the second way, in Java, ever.
In my opinion example A is much easier to understand than B hence I would say code readability and maintainability wise it is better.
Reason is that for example B the persons structure actually involves composition of another structure ArrayList to accomplish generics which in example A, you have achieve it by declaring List<Person>.
Example B is also less flexible than A, because it is always a collection whereas example A you can simply use it as a Person and just plug it into different data structures available in Java. Example if you want to keep track of unique person, you can easily come out with a map that is keyed by <name, Person>, whereas example B would not be as clean because you will be doing something like <name, Persons> where each persons would only contain 1 person. Hence extensiblity wise may not be as great as the other one.
Also to me, A is more of the traditional way of doing object oriented because you are representing a real world object as a class whereas B is really just a wrapper of an object.
Obviously there can be a lot of arguments to this, but I'm happy with others input and critic about this answer.

Receiving simple parameters with webservice

I am currently just getting into Web services as it is new to me. I have put together a sample one returning animal type.
public class AnimalTypeService {
public String animalType(String animal) {
String animalType = "";
if ("Lion".equals(animal)) {
animalType = "Wild";
} else if ("Dog".equals(animal)) {
animalType = "Domestic";
} else {
animalType = "I don't know!";
}
return animalType;
}
}
However, I would now like to take several string/parameters. For instance someone who enters in a name, lastname, age, birthday. Can someone give me an example on how I could take in a set of parameters such as these and write them into a xml doc format?
For taking in multiple parameters you have three options:
1) You could create a class (human for example), which contains the instance variables name last name, age, and birthday.
2) You could pass in the parameters as an ArrayList or as an Array. However, you then have to be careful that the ordering of the parameters fits what you need (so that you don't read in a last name as a birthday for example).
3) You can pass in the parameters as a map, with a key being "name" and a value being "steve" for example. Then the method could search for certain keys and find the values associated with those keys.
In regards to writing them into an xml doc format, i would suggest looking up DOM objects, as they make writing xml docs very simple. http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ provides a great tutorial/example

Categories