Is there any way to auto generate objects from a class? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a class with 5 variables, and 3 out of the 5 will be the same for every new object. Is there a way to create, say, 100 objects, from a string file containing 100 items?

Yeah of course can you do that. I wont write code for you, but the following steps should help to come to a solution yourself.
Make a constructor delegation, one which accepts 5 values. And another which accepts just the 2 which are not changing. This constructor then overloads the other with the default values. this(3,2,"a", input1, input2)
Initialize the 3 variables which are not changing directly in the field declaration: private int initialSetVariable1 = 3;

Related

Use the value that a function returns as the name of a new Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Good I am trying to create a function that generates new Array, and trying to use a getter method that returns a name for example ArrayD5, ArrayD10 and use these as the name of the new arrays to generate.
I have tried to do this:
int length = 5;
String (SeatIbiza.nombres(length))[] = new String[length];
String nombres (int length) {
return "ArrayD"+length;
}
If you are looking for a way to declare something like e.g.
String "ArrayD" + 2
so that you have a variable
String ArrayD2
then this is not possible in Java. Dynamic naming of variables is not supported.
I don't think if that's possible. The variable names are required to be declared during compile time in JAVA and dynamic naming isn't supported... yet.
Correct me if I'm wrong as I'm still learning about JAVA

Cast a special java object to an integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've created a program, which helds the grades of the pupils. I've created a class Grade and now I am using it as an object. Now I want to calculate the average of the grades. Do you know any possible way to cast it to an Integer?
Thanks for your help
You should have at least one attribute in your Grades class.
You cant cast Grade to Integer.
Calculate grades in your main/test or other class via get method.
you don't have to convert custom object(Grades in this case) to Integer and you can't. If Grades are just integers , you don't have to create the separate class for it. If it has more than one attribute and then the right attribute from grade class and do whatever processing you want.

Create variable execution time Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is possible to do in Java something like this? :
System.out.... -> ask for a string
Store this string
Create a variable with the name of this string
Thank you very much!
No. You cannot create a variable, at execution time, with a particular name.
However, you can use a Map, which probably does what you want. The get method will look up an entry, and the put method will set an entry. Example:
Map<String, Integer> myMapOfThings = new HashMap<>();
String nameOfThing = /* ... get a string from the user somehow here ... */;
myMapOfThings.put(nameOfThing, 2);
System.out.println(myMapOfThings.get(nameOfThing)); // prints 2

How to access a specific array element in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to define specific numbers out of an array of 52 numbers so I can assign them an image (yes I am creating an applet). I don't think that code is necessary for any of you to answer this question. Any help would be fantastic.
I'm not sure I understand what you're asking. You can set an item in an array this way:
myArray[4] = 52;
That would set array index #4 (remember it's zero based so it's actually the 5th item) to the value of 52. Is that what you're looking for?

I need a three-valued logic in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.

Categories

Resources