Week 1 TT Notes
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
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
// 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);
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);
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);