Unit 1 Blog Primitives
Why Java
- Java syntax easier compared to other languages
- focuses on classes, objects, and methods
- easy connection with APIs
- once compiled, it will run on any machine
Anatomy
- main.java is run by the java compiler
- classname.java are names for the java files, public allows being called from other classes
- {} is the stuff inside the class, static means that instance of main class isn't needed, void doesn't return to another function
Primitive Data Types
- int: integer number
- double: decimal number up to 15 decimal places
- boolean: true or false
Variables
- to initialize variable: (scope) dataType variableName = dataValue
- private only allowed in a class
- public any class can access
- protected allowed to all classes in package and any subclass
- variables known as constants don't change
Basic Math
- equal sign is assignment operator, can be used to change the value of the variable
- modulo operator is % (a % b, which is integer remainder of a divided by b)
Compound Assignment Operators
- operators to change numbers (ie: integer += 1;)
- *=,+=,-=,/=,%=,i++,i--
- Change a variable’s type, so for example changing an int to a double or vice versa
- Truncate by changing a double to an int, this removes everything after the decimal
- A trick to round with casting is to change a double to an int, but before doing this, add .5 if a positive integer and subtract .5 if a negative integer
int num1 = 50;
int num2 = 56;
double round;
double intaverage = num1 / num2;
double doubaverage = (double)num1 / num2;
System.out.println("dividing with integer(also truncating): " + intaverage);
System.out.println("dividing with double: " + doubaverage);
Constructor - A constructor initializes an object, a constructor has no return value because it is not actually used by the code, instead it simply initializes the object.
public class Customer { //Intializing class Customer in this case with a constructor - no return value
Accessor methods - AKA “getters”, return the values in a defined class/reads the value, it is usually named get + Class Name
public String getName(){
return name;
};
- Wrapper classes allow converting primitives into objects and vice versa, which allows java to be object oriented, as primitives aren’t an object by default
- Someone may use a wrapper class on int to convert it to an object as they contain many methods to deal with an int value like converting it to a string representation, this is the same for doubles
- Concatenation is combining 2+ strings to make a string using either the + or += operator or the concat() method
- concat() is immutable, so you need to set it to a string so a.concat() wouldn't work, but a = a.concat() would
- to concatenate multiple data types, you need to use + and not concat method, os this would allow concating a string and a integer for example
public class Main{
public static void main(String args[]){
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer
String string = i.toString(); //converting the Integer to a string, this doesn't work with the primitive
String string1 = " 2";
String string2 = string.concat(string1); //using concat
String otherdata = string+a;
System.out.println(a+" "+i);
System.out.println("concat 20 and 2 together: " +" " +string2);
System.out.println("concat a string and integer: " +" " +otherdata);
}}
Main.main(null);
public class Example {
public static void main(String[] args) {
System.out.print("Hello World");
System.out.print(" From Team Oops!");
System.out.println(); //done to separate two different lines
System.out.println("Welcome to our presentation!");
System.out.print("We hope you learn something from it!");
}
}
Example.main(null);
- can compare numbers using the equals method
- can compare numbers using compare method
- operators such as <, >, == can be used
Integer x = 0;
Integer y = 0;
if(x.equals(y)) {
System.out.println("Both x and y are equal");
}
else {
System.out.println("x and y are not equal");
}
int comp = Integer.compare(x, y);
if(comp>0) {
System.out.println("x is greater than y");
}else if (comp<0){
System.out.println("x is less than y");
}else{
System.out.println("x and y are equal");
}
- can use equals method to compare two strings based on content in string
- equalsIgnoreCase method compares two strings no matter the case
String string = "Sdsad";
String string1 = "sdsad";
System.out.println(string.equals(string1));
System.out.println(string.equalsIgnoreCase(string1));
Big O notation for Hash map, Binary Search, Single loop, Nested Loop Good for determining time efficiency in a Java code, good for reducing and improving time.
- java math class provides many math methods that allow finding different things, such as a max or min function
- math.random will return a number between 0.0 and 1.0, or you can multiply it to get 1 to 100 (seen in code)
for (int i = 0; i <5; i++) {
int randomNum = (int)(Math.random() * 101);
System.out.println(randomNum);
}
public class Printing {
public static void main(String[] args) {
System.out.println("Samuel");
System.out.println("Team 10"); //simple printing
}
}
Printing.main(null);
public class Example {
public static void main(String[] args) {
int Herbo = 10;
double gasPrices = 7.99;
final boolean Hot = true;
String name = "Team Oops is hot:";
System.out.println(Herbo);
System.out.println(gasPrices);
System.out.println(name + Hot);
// Hot = false; cannot assign a value to final variable Hot
}
}
Example.main(null);
public class Biodata {
public static void main(String[] args) {
String name = "Samuel";
int age = 17;
boolean underclassman = false; //making variables and printing them
int height = 6;
System.out.println(name);
System.out.println(age);
System.out.println(underclassman);
System.out.println(height);
}
}
Biodata.main(null);
public class Math {
public static void main(String[] args) {
int number = 2;
int number2 = 5;
double number3 = 2.0;
double number4 = 5.0;
System.out.println(number+number2);
System.out.println(number3+number4);
System.out.println(number-number2);
System.out.println(number3-number4);
System.out.println(number * number2);
System.out.println(number3 * number4);
System.out.println(number/number2);
System.out.println(number3/number4);
System.out.println(number4 % number3);
System.out.println(number2 % number);
}
}
Math.main(null);
public class Num {
public static void main(String[] args) {
Double num = (((6.0*1234124.0)/(11345.0-890809.0+90800.0))%980098.0); //the math function, using parentheses to make sure pemdas works well
num = num/100;
System.out.println(num);
}
}
Num.main(null);
public class Operators {
public static void main(String[] args) {
double n = 1;
double b = 2;
System.out.println(n+=b); // a bunch of diferent math functions
System.out.println(n-=b);
System.out.println(n*=b);
System.out.println(n/=b);
System.out.println(n%=b);
System.out.println(n++);
System.out.println(n--);
}
}
Operators.main(null);
public class Cast {
public static void main(String[] args) {
double num = 10.5;
int num2 = 100;
int numInt = (int)num;
double num2Double = (double)num2;
System.out.println(num);
System.out.println(num2);
System.out.println(numInt);
System.out.println(num2Double);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
Cast.main(null);
public class CastActivity {
public static void main(String[] args) {
double data = 123456.123456;
int value = (int)data; //using casting to truncate
System.out.println(value);
// int num = 678901234567890;
// System.out.println(num); this turns out to be an error
}
}
CastActivity.main(null);
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What is your name?");
String name = sc.next(); //string
System.out.println(name);
System.out.println("How many pizzas do you want to buy?");
int pizzas = sc.nextInt(); //integer
System.out.println(pizzas);
System.out.println("Do you have the discount (true/false)?");
boolean hasDiscount = sc.nextBoolean(); //boolean
System.out.println(hasDiscount);
double price; //double, defaults to 0
if (hasDiscount) {
price = 1.20;
} else {
price = 2.10;
}
char firstChar = name.charAt(0); //character
double finalPrice = price * pizzas * 1.08; // adding taxes
System.out.println("Hi " + firstChar + "! You have to pay " + (finalPrice) + " dollars.");
}
}
Main.main(null);