Answer to Question #268722 in Java | JSP | JSF for Mohammad

Question #268722

Java Data Types Assignment?



Object [ ] = { “Java”, 1.23F, true, false, 23, ‘a’, ‘b’, 23.4F, 45.6F}; Write the Java Program to sum only the float variables or any data type.

1
Expert's answer
2021-11-19T14:38:12-0500
public class Main {
    public static void main(String[] args) {
        Object[] data = new Object[]{ "Java", 1.23F, true, false, 23, 'a', 'b', 23.4F, 45.6F};

        float sum = 0;
        for (int i = 0; i < data.length; i++) {
            Object obj = data[i];
            if (obj.getClass() == Float.class)
                sum += ((Float) obj);
        }
        System.out.println("Sum of floats = " + sum);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS