top of page
Writer's picturePraveen Mogan

Math, Assignment, and Operators in Java

Now that we have the essentials out of the way, let's focus on arguably the most important part of any program: variable management. Whether you are coding in C++, Python, or Java, all programmers need to be able to understand how to create variables that they can track throughout their program. Daunting? Don't worry about it; this tutorial will cover all your fears! Math really isn't that bad, once you get to know it. Even better? The only math you really need to know is addition and PEMDAS.


More Vocab (Make sure you know these):

Assigning - giving variables a value. Assignment operations - operators that change the value of a variable. Binary operations - Operations that involve two operands. Casting - Treating a value as another type. Concatenation - When a String value is joined to another piece of data. Declare - To create a variable. Initializing - The first time a variable is assigned a value. Literals values - that can only be interpreted in only one way. Operand - A value used in a mathematical operation. Operator precedence - The order in which operations are performed. Primitives - Built-in data types that hold a small amount of data. Shortcut operators - Assignment statements that are shortcuts to perform variable changes. Truncation - When the data past the decimal point is dropped to get an integer value. Unary operations - Operations that involve only one operand. Variables - Containers that hold data while your program is running.


Still don't understand? Don't worry; it's more important to understand the actual action than memorizing the term that correlates with the action. With that said, here are some more abstract constructs:


4 primitive data types - double(decimal numbers), int(whole numbers), char(letter), and boolean(true or false)


Variable naming conventions - lower and upper case letters and numbers, underscores are acceptable, but no other special characters (separate words with lower/uppercase or underscores)


Variable Assignment (primitive data types are lowercase objects such as Strings are uppercase):

type name = value;

int x = 3;

String x = "fog";


Know your ASCII values! They can be derived from counting up from the following base numbers:

32 = space(' ')

48 = '0'

65 = 'A'

97 = 'a'


Note: Characters can be converted to ASCII values vice versa

int c = 'b';

c-=30;

System.out.println((char)c); //output = D - the conversion of the integer c to a character is known as casting, treating values as different types


Note: Operator precedence follows PEMDAS


concatenations vs. addition Examples (if a string precedes the value, then the value will be concatenated, otherwise the values will be added)

ex.

int num = 12;

System.out.println(5+3+"-7"+num+8); //output =8-7128


Operators:

int x = 0;

x+=3; //output = 3

x/=3 //output = 1

x-=1 //output = 0

x*=4 //output = 0


Tricky Problem:

int a,b;

a = 8;

b = 4;

a*=b/a; //8*(4/8) = 0

System.out.println(a); //output = 0 because 4/8 in integer division leads to truncation, so 0*8 = 0


Printf:

There are several tutorials online going into detail. I don't believe you need to know how to use these on the AP Exam, so this description will be vague.

System.out.printf(%,left align,minimum space,decimal place, type of variable);

Ex. System.out.printf("%-2.6f",320);


Scanners (used to get input):

Basically, to create a Scanner, you need to import the Scanner class (import java.util.Scanner;)

Then, create a Scanner object (Scanner keyboard = new Scanner(System.in);)

Finally, you can use this Scanner:

int input = keyboard.nextInt();

char input = keyboard.next().charAt(0);

String input = keyboard.nextLine() OR keyboard.next()

double input = keyboard.nextDouble();


Math Class(methods you need to know):

Math.PI - gives the value of pi

Math.pow(base, power) - power function

Math.abs(num) - absolute value

Math.sqrt(num) - square root function

(int)(Math.random()*numOfValues+startingValue) - random value from start, based on number of values


See, math isn't that bad. I told you! :)


Wow, that was a lot! I hope it helps and Happy coding/cramming!

34 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page