Weakly vs Strongly Type

Python is weakly, java is strongly

Variable Types

  • Python - type of variable is automatically defined
  • Java - type of variable needs to be manually defined

Java vs JavaC

  • Python run - automatically runs, compiles it on the fly
  • Java run - javac compiles the code and changes into byte code and then runs
  • 1's and 0's so types have prefixes to enable interpreter to understand

Primitive vs Wrapper

  • Primitives lowercase vs wrappers uppercase
  • Wrapper class has methods, built-in code that can do certain actions
  • Everything in Java is OOP except for primitives

Running the code

DefinePrimitives class

  • Uses concatenation to combine name of different variables with their value
  • If left variable is a string, everything else becomes a string
  • Different types have different types of values.
  • All are primitive

PrimitivesScanner class

  • Identifies the type of integer and asks for the right one
  • If wrong type is entered, it returns a statement that says the value entered is not that type

PrimitiveDivision class

  • Shows how there are many ways to write outputs to create the same thing

GradeCalculator class

  • Takes inputs for doubles to get certain number of grades
  • Averages those inputs

Hacks

Data Types

// Using primitive data types
import java.util.Scanner;


public class WorkWithPrimitives {
    public static void main(String[] args) {    
        Scanner input;

        // Takes in two integers and adds them together
        input = new Scanner(System.in);
        try {
            // Taking two integers
            System.out.println("Enter an integer: ");
            int sampleInputInt = input.nextInt();
            System.out.println("Enter another integer to add: ");
            int secondInputInt = input.nextInt();
            // Adding two integers 
            int addedOutput = sampleInputInt + secondInputInt;
            System.out.println("The sum of " + sampleInputInt + " and " + secondInputInt + " is: " + addedOutput);
        } catch (Exception e) {  // if one of the variables is not an int
            System.out.println("One of your variables was not an integer (form like 159), " + e);
        }
        input.close();

        System.out.println("=======");

        // Takes in two primitive doubles and multiplies them together
        input = new Scanner(System.in);
        try {
            // Taking the two doubles as input
            System.out.println("Enter a double: ");
            double sampleInputDouble = input.nextDouble();
            System.out.println("Enter another double: ");
            double secondInputDouble = input.nextDouble();
            // Multiplying the two doubles with a double as an output using casting
            System.out.println("The product of " + sampleInputDouble + " and " + secondInputDouble + " is: " + (double) sampleInputDouble * secondInputDouble);
            //System.out.println(sampleInputDouble);
        } catch (Exception e) {  // if a variable is not a double
            System.out.println("One of your variables was not a double (form like 9.99), " + e);
        }
        input.close();

        System.out.println("=======");

        // Using an AND gate
        input =  new Scanner(System.in);
        try {
            // Takes two boolean inputs
            System.out.println("Enter a boolean: ");
            boolean sampleInputBoolean = input.nextBoolean(); 
            System.out.println("Enter another boolean: ");
            boolean secondInputBoolean = input.nextBoolean(); 

            // An AND gate comparing both booleans
            if (sampleInputBoolean) {
                if (secondInputBoolean) {
                    // TRUE
                    System.out.println("Since both booleans are true, the AND gate returns TRUE");
                }
                else {
                    // FALSE
                    System.out.println("Since the second boolean is false, the AND gate returns FALSE");
                }
            }
            else {
                // FALSE
                System.out.println("Since the first boolean is false, the AND gate returns FALSE");
            }
            
        } catch (Exception e) {  // if not true or false
            System.out.println("One of these is not a boolean (true or false), " + e);
        }
        input.close();

        System.out.println("=======");

        // wrapper class String
        System.out.println("Enter a String: ");
        input =  new Scanner(System.in);
        try {
            // Takes two String inputs
            String sampleInputString = input.nextLine();
            System.out.println("Enter another String: ");
            String secondInputString = input.nextLine();
            //Using compound assignment operator
            sampleInputString += secondInputString;
            // Prints combined String
            System.out.println("Combined String: " + sampleInputString);
        } catch (Exception e) { // if not a String
            System.out.println("Not an String, " + e);
        }
        input.close();
    }
}
WorkWithPrimitives.main(null);
Enter an integer: 
Enter another integer to add: 
The sum of 15 and 4 is: 19
=======
Enter a double: 
Enter another double: 
The product of 15.0 and 4.0 is: 60.0
=======
Enter a boolean: 
Enter another boolean: 
Since the second boolean is false, the AND gate returns FALSE
=======
Enter a String: 
Enter another String: 
Combined String: Hi Mr. M! I am testing combining two Strings into one.

Code.org problem tester

Trying to figure out how the code does this math problem

int a = 3;
int b = 10;
double c = 2.5;
c = 2 * a - 15 / b + c;

System.out.println("15/b results in an integer value therefore result for that segment rounds to 1");
System.out.println("Therefore, the rest is 2 * 3 - 1 + 2.5 which gives 7.5.");
System.out.println("-------------");
// 


System.out.println("Result: " + c);
15/b results in an integer value therefore result for that segment rounds to 1
Therefore, the rest is 2 * 3 - 1 + 2.5 which gives 7.5.
-------------
Result: 7.5

Centimeeters to Miles Converter

import java.util.*;

public class main{
    public static double converter(double input) {
        double meters = input / 100.0;
        double miles = meters / 1609.34;
        return miles;
    }

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a double value: ");
        double value = input.nextDouble();
        System.out.println("The conversion of " + value + " cm in miles is: " + converter(value));
        
    }
}

main.main(null);
System.out.println("-------");
main.main(null);
Enter a double value: 
The conversion of 600975.0 cm in miles is: 3.7342948040811765
-------
Enter a double value: 
The conversion of 23.0 cm in miles is: 1.4291572942945555E-4