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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone please explain the use of the following code in Java
private TableColumn<Books, Integer> colId;
I want to know the reason of using <?,?> to declare a variable in Java.
Those are 'Generics'. Specifically, the things inside the brackets are called 'Type Parameters'. It allows different types to be chosen when you create the object or 'variable' as you call it.
So you could have a list of numbers with: new ArrayList<Integer>()
or a list of strings with: new ArrayList<String>()
https://www.tutorialspoint.com/java/java_generics.htm
In your example, it's creating a column to keep track of ids for books. The ids will be ints and the object they are identifying is books. But, you may want another TableColumn (which behaves the same as the Book Id column) but for tracking the titles of the books: TableColumn<Books, String> colTitle
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 4 years ago.
Improve this question
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();
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;
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?
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.