Lesson 5 - Activity 2 - Can you add integers and doubles together?

Time: 15 minutes

Purpose: Exploring what happens when you mix decimal and integers

Introduction: You’ve done operations on two identical variable types. What happens when they’re not the same?

Task: Try adding an integer to a double number and printing the result

  1. Describe what happens
  2. Attempt to make the addition work by any means

Was playing around with how many decimal digits we can have :upside_down_face:

    int A = 1;
    double B = 2.9999999999999999;
    double C = 2.999999999999999;
    int D;
    double E;

    D = (int)B + (int)C;
    E = B + C;

    System.out.println(newLine + "INT    ---> " + (int)B + " + " + (int)C + " = " + D);
    System.out.println("DOUBLE ---> " + B + " + " + C + " = " + E);
    System.out.println("DOUBLE ---> " + (double)A + " + " + C + " = " + E);

Good work. You’ve got the basics.