Data Types

  • Define in a Class the following data types
  • Describe in comments how each data type choice is appropriate to application
public class DefinePrimitives {
    public static void main(String[] args) {
      int anInt = 100;  //It can be used in app that stored user information like age
      double aDouble = 89.9; //It like integer, but It can used to store information(number) after the decimal point
      boolean aBoolean = true; // It can be used to define a value is true or not which we can use it in if statement

      String aString = "Hello, World!";   //It can be used to print information in the screen for users to see
      String aStringFormal = new String("Greetings, World!");
    }
  }
  DefinePrimitives.main(null)

Perform arithmetic expressions and assignment in a program code

Perform compound assignment operator (ie +=), add comments to describe the result of operator

int a = 100;
int b = 200;
int c = 0;

c = (a + b) * (a - b); // c is equal to (a + b) * (a - b)

c += (100 * a)/b * a; // c is equal to c + (a + b) * (a - b)

System.out.println("c: " + c);
c: -25000

Determine what is result is in a variable as a result of an data type and expression (ie integer vs double)

Answer: if the result in a variable is an integer number or the expression is int, then the data type is integer. If the result include a decimal point or the expression is double, then it is double.

Input Primitive data

Perform an arithmetic expressions that uses casting, add comments that show how it produces desired result.

double a = 12.2/2;
int b = (int)(a + 0.5);
System.out.println("12.2/2 =" + a);
System.out.println("12/2 = " + (int)a);
System.out.println("12.2/2 rounded = " + b);
12.2/2 =6.1
12/2 = 6
12.2/2 rounded = 6