1/34
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
int
int (name) = 3
Whole numbers (e.g., 5, -10)
double
double (name) = 3.4;
Decimal numbers (e.g., 3.14, -0.5)
boolean
i.e. boolean (name) = true/false;
True or false values (true, false)
String
String (name) = “Your mom.com”;
A sequence of characters (e.g., "Hello", "CodeHS") - use quotes
Variable Rules
Must start with a letter, , or $ (e.g., myVar, value).
Cannot start with numbers or use reserved keywords like class.
Type Casting:
Example: Convert int to double
int x = 5;
doubley=(double)x; //y is now 5.0
Creating a string
String greeting = "Hello";
Write a small program to print out the length of the word “asdfafd”
String word = “asdfafd”;
System.out.println(word.length());
Write a small program to turn the word “grah” to all uppercase.
System.out.println(“grah”.toUpperCase());
Write a program to find the char at the index of 3 in “dogs”.
String s = “dogs”;
System.out.println(s.charAt(3));
How do you write equal to as a comparison operator?
==
How do you write not equal to as a comparison operator?
!=
How do you write greater than as a comparison operator?
>
How do you write greater than or equal to as a comparison operator?
>=
How do you write less than as a comparison operator?
<
How do you write less than or equal to as a comparison operator?
<=
How do you write AND as a logical operator?
&&
How do you write OR as a logical operator?
||
How do you write NOT as a logical operator?
!
How do you write an if statement?
if (condition) {
}
How do you write an if-else statement?
if (condition) {
}
else {
}
How do you write an else-if statement?
if (condition) {
}
else if{
}
Write a program that prompts the user to input an int, which is y. if int y >= 10, the console prints “Greater than or equal 10”, and if it’s lower than ten, it prints “Less than 5”.
Scanner input = new Scanner(System.in);
int y = input.nextInt();
if (y >= 10){
System.out.println(“Greater than or equal 10”);
}
else{
System.out.println(“Less than 5”);
}
What is the main class name?
public static void main(String[] args)
What does the first value in a for loop do?
Initializes the variable
What does the second value in a for loop do?
Sets the if condition.
What does the third value in a for loop do?
Makes the change in the variable
Write a for statement that repeats until i equals 5 while adding 1 everytime starting from zero, printing i every repition.
for(int i = 0; int <= 5, i++){
System.out.println(i);
}
What’s a for loop?
Use when you know how many times to repeat the loop.
What is while loop?
Use when the number of repetitions depends on a condition.
Write a while statement that repeats until count equals 2 while adding 1 everytime starting from zero, printing count every repition.
int count = 0;
while (count < 3) {
System.out.println(count);
count++;
}
Infinite Loops example?
while (true) {
System.out.println("This will run forever!");
}
What is the output of the following code?
int a = 8;
double b = 3.0;
System.out.println(a / b);
a. 2
b. 2.0
c. 2.6666666666666665
d. Error
c. 2.666666666666665
What is the result of true && false?
a. true
b. false
c. Error
d. 0
b. false
Write a for loop to print numbers 1 through 5.
for(int i = 1; i < 6; i++){
System.out.println(i);
}