Data types in java script
logical operators: x&&y
both x and y must be true
Example of lowercase method:
char c = 'e'; out.println(c); c = Character.toUpperCase(c); Output: e
Escape Sequences: \b
deletes previous character (backspace)
How would you make a Do while loop?
do{ dosomethingOne(); } while(boolean condition placed here); the difference here is that a DO WHILE loop does the things first before checking the condition, It will continue to loop until
logical operators: x||y
either x or y must be true
how would you create a while loop?
while(boolean condition placed here) { do somethingOne(); } a while loop will continue to run intil the condition becomes false.
How do you compare two strings?
you would use .equals() EX: String stringOne = "big"; if( stringOne.equals("it") ) { System.out.println("== it"); } if( stringOne.equals("big") ) { System.out.println("== big"); }
substring(x)
returns a section of the string from x to length-1
substring(x,y)
returns a section of the string from x to y not including y
length()
returns the # of chars
charAt(x)
returns the char at spot x
what are some ways to create a increment variable?
score++; or score = score + 1;
Operators used in math and examples:
+ addition - subtraction * multiplication / division % modulus num = 45; out.println(num%10); 5 out.println(num/10); 4
In Java, every program statement ends with a what ?
;
Boolean conditions used:
== equal != not equal <= less than or equal to >= greater than or equal to || or && and (if you are comparing strings use .equals)
How many methods does class CS contain? public class CS { public void one() { System.out.print("one"); } }
1
What is output by the following code given the input 11 15 25 30 Scanner kb = new Scanner( System.in ); kb.nextInt(); System.out.println( kb.nextInt() );
15 (cause another command is inside the System.out.println, duuuuuhhh)
Short
16 bits, -327 to 327
char
16 bits, 0 - 65535
Int
32 bits, -2 billion to 2 billion
float
32 bits, big to big
reference
32 bitsl N/A
double
64 bits, big to big
long
64 bits, big to big
Byte
8 bits, -128 to 127
Arrays in Strings
A string is a group of characters. The first character in the group is at spot 0 and the last character is -1.
What is encapsulation?
All data members should have private access. A set of public methods should be provided to manipulate the private data.
What is casting and what is an example of casting?
Casting is often used to create compatibility among data types. EX: int one =0; double = 7.756; one=(int)dec;
Constructors/Zero arg constructor
Constructors set the properties of an object to an initial state. (often have parameters) public Triangle() { sideA=0; sideB=0; sideC=0; }
what will this command do?: String[] words = new String[10];
I will create a String Array named "words" and make it have 11 indexes (because 0 counts)
what does this command do?: aplus[0]= 9;
It sets spot 0 to the value of 9.
What will this command do? String s = "one two four five"; String[] words = s.split(" ");
It will separate String s at all of the spaces and create it as a Array String named "words"
what would this command do?: intnums[] = {45,78,90,66,11}; Arrays.sort(nums);
It will sort number and chars and strings
Given the following statement, var must be defined as which of the following types? var = keyboard.nextLine();
String (for both nextLine & next you get a string)
Which of the following lines correctly defines a String variable?
String s = "aplus";
String instantiation
String s = "apluscompsci"; or String champ = new String("aplus"); s or champ is the reference variable. EX: String s = "apluscs"; int len = s.length(); System.out.println( len );
What is the output by the code below? String s = "Hello"; System.out.println("\String s");
There is no output due to a syntax error. ( can't have those backslashes in the middle of code)
logical operators: !x
True if x is false
what are instantiation?
When you create a way to reference a method and/or code. EX: AplusBug dude = new AplusBug();
Does PEMDAS affect the math equations?
Yes, you need to follow the rules of PEMDAS
Which of the following is the newline character?
\n (remember, it is a backslash)
How do you assign a variable without using a data type?
abdi = 19;
Methods frequently used for strings:
equals(s) checks if this string has same chars as s compareTo(s) compares this string and s for >,<, and == trim() removes leading and trailing whitespace replaceAll(x,y) returns a new String with all x changed to y toUpperCase() returns a new String with uppercase chars toLowerCase() returns a new String with lowercase chars
Math return methods:
floor(x) rounds x down ceil(x) rounds x up pow(x,y) returns x to the power of y abs(x) returns the absolute value of x sqrt(x) returns the square root of x round(x) rounds x to the nearest whole number min(x,y) returns smallest of x and y max(x,y) returns biggest of x and y random() returns a double >=0.0 and < 1.0 (random will give you a number from 0.0 to 0.99, not 1.0) (everything needs to start with Math.)
How would you create a FOR LOOP?
for(init value; boolean condition; increment) { doSomething(); } -the (init value) would be the starting number of times it will run -(boolean condition) decides when the loop stops running. the (increment) is the amount a value increases to end make the boolean condition true.
How do you write a If Else statement?
if( boolean condition placed here ) { do something 1; } else { do something 2; }
How would you import all plugins?
import java.util.*;
More String methods
indexOf( str ) returns the loc of String str in the string, searching from spot 0 to spot length-1 indexOf( ch ) returns the loc of char ch in the string, searching from spot 0 to spot length-1 lastIndexOf( str ) returns the loc of String str in the string, searching from spot length-1 to spot 0 lastIndexOf( ch ) returns the loc of char ch in the string, searching from spot length-1 to spot 0
Given the following statement, var must be defined as which of the following types? var=keyboard.nextInt();
int (Also, in the code "nextInt", "Int" has to be capitalized.
Which of the following lines correctly defines an integer variable?
int abdi = 16;
How do you create a IF switch statement? and how does it work?
int num = 30; switch(num) { case 11: System.out.println("num == 11"); break; case 30: System.out.println("num == 30"); break; } (This switch statement will check if num is equal to 11 and 30(case 11 & 30) if it is, it will print out the corresponding text, AND if it does not have a break; all conditons after the true statement will be executed untill the end or break; )
How do i create a array variable?
int[] abdi = {0,1,2,3,4,5}; (datatype)[] (name) = {(values)};
What of the following is not a legal reserved word for a java data type?
integer (cause it it's "int" not integer)
what will this command do?: abdi--;
it will decrease abdi's value by one.
what will this command do?: abdi++;
it will increase abdi's value by one.
Escape Sequences: \r
moves in front of current line
Formatting Output out.printf( " %9.2f " , 8.25612 );
out.printf( " %9.2f " , 8.25612 ); 9 = column size .2 = # of decimals f = data type output == 8.26
Escape Sequences: \"
outputs: "
Escape Sequences: \'
outputs: '
Escape Sequences: \\
outputs: \