Primitives Notes

Python

  • weakly type language
  • don't have to specifiy type of data, ie double quote for string

Java

  • strongy type language
  • requires you to think about it more and which type of data it is

Java C and Java

  • Java C executes then Java executes then the program runs

Data Tyes

  • Data types with a captial letter are primitives are lowercase
  • Data types with a lowercase letter are wrappers

  • Primitives are just the numbers assigned to memory

  • primitives are built in

  • Wrappers are classes and have methods, getters, and setters

  • has built in code with it that can do more stuff
  • to compare two strings you can use ".equals"
  • methods are code associated with the type
  • string is basically an object
  • not built in has to be declared

Primitive Hacks

  • Define in a class the following data types
    • Demonstrate use of Primitives: int, double, boolean, string
public class Primitives {
  public static void main(String[] args) {
    int x = 5;
    double z = 9.88;
    boolean w = true;
    System.out.println(x);
    System.out.println(z);
    System.out.println(w);
    }
}
Primitives.main(null);

5
9.88
true

  • Demonstrate use of Wrapper Class object: String
  • Perform arithmetic expressions and assignment in a program code Code.org Lesson
public class Math{
    public static void main(String[] args){
        int a = 3;
        int b = 10;
        double c = 2.5;
        c = 2 * a - 15 / b + c;
        System.out.println(c);
    }
}
Math.main(null);
7.5
  • Describe in comments how each data type choice is appropriate to application
  • Determine what is result in a variable as a result of an data type and expression
import java.util.Scanner; // Import the Scanner class

public class TemperatureConverter { // Defining the main
    public static void main(String[] arg){
        Scanner inputTemp = new Scanner(System.in);
        
        System.out.println("Enter a temperature");

        int tempInt = inputTemp.nextInt(); // Stores as an integer. 

        Scanner tempTypeinput = new Scanner(System.in);
        System.out.println("Is it in Fahrenheit or Celsius. Type C for Celsius or F for Fahrenheit.");
        String tempType = tempTypeinput.nextLine(); // Stores as a string

        if (tempType.equals("C")){
           double convertedTemp = tempInt * 1.8; // Double is appropriate here because decimals are needed
           double convertedTempF = convertedTemp + 32;
           System.out.println("Celsius: " + tempInt + "°C and Fahrenheit: " + convertedTempF + "°F");
        } 
        if (tempType.equals("F")) {
           double convertedTemp = tempInt - 32;
           double convertedTempC = convertedTemp/1.8; // Double is appropriate here because decimals are needed
           System.out.println("Fahrenheit: " + tempInt + "°F and Celsius: " + convertedTempC + "°C");
        }
    }
}

TemperatureConverter.main(null);
Enter a temperature
Is it in Fahrenheit or Celsius. Type C for Celsius or F for Fahrenheit.
Celsius: 0°C and Fahrenheit: 32.0°F
  • Perform an arithmetic expressions that uses casting, add comments that show how it produces desired results
// Casting temporary treats on variable of a data type as another data type

public class Math{
    public static void main(String[] args) {
        double a = 6.5 + 5;
        System.out.println(a);
        System.out.println((int)(a));

        double number = 11.0/4;
        int roundNumber = (int)(number+0.5);
        System.out.println(number);
        System.out.println(roundNumber);

        Scanner inputNumber = new Scanner(System.in);
        
        System.out.println("Enter a decimal to be rounded");

        double tobeRounded = inputNumber.nextDouble(); // Stores as an double.
        
        System.out.println((int)(tobeRounded + 0.5));
        
    }
}
Math.main(null)
11.5
11
2.75
3
Enter a decimal to be rounded
7