Basics Flashcards
How many characters can a long store?
long largest =
19 characters
Can a comparison in Java have more than 2 sides? e.g.
while(!dice1 == dice2 ==dice3)
// all 3 dice match
?
No. Only 2 sides. So:
!(dice1 ==2 && dice2 == dice3)
while not
dice 1 is the same as dice 2
and
dice 2 is the same as dice 3
Define a variable
a called message (with some rules) that has been stored under a variable name
Variable Name rules?
1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int)
Variable Name rules?
1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int, Char)
Variable Name declaration rules?
1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int, Char)
3) camel case - mutiple words strung together, first word must be lower case followed by upper case on first letter of each subsequent word e.g. glasgowIsgreat = true;
4) cannot start with numbers
5) cannot start with ‘ or “
6) stick to standard text characters
7) make it mean something and simple (as long as you like)
String message = “Hello!”;
double?
stores decimals, -ve numbers
1,1, 2.1345, -1.4e10
char
stores individual characters
a, b, 1
char
stores individual characters
(a, b, 1)
char d = ‘f’;
Not single ‘’
boolean
stores T or F
boolean GlasgowIsgreat = true;
What is camel case?
mutiple words strung together, first word must be lower case followed by upper case on first letter of each subsequent word e.g. glasgowIsgreat = true;
What is the difference between doing maths using double and int?
Integer division - the remainders are left out!
int a = 3;
int b = 2;
a/b;
= 1 [not 1.5!]
For integer division, how does Java get the remainder (how much is left)?
3 % 2 = 1 // can remove one 2 from 3, and we are left with 1
5 % 6 = 5 // all 5 are remaining
10 % 4 = 2
What is the Order of Operations in Java?
1) not necessarily Left to Right
2) Order is:
*
/
+
-
* and / are done BEFORE + and -
3) If multiple / and *, they are done Left to Right
When using brackets to specify the order of operations, which nested values are evaluated first?
the innermost
e.g.
(a(b + c))/(ac + b);
If you want to divide two int variables and then do a standard double division i.e. change from int division to answer being a double?
e.g. double c = a/b
double c = 1.0*a/b;
1.0* converts a into a double and then the division is done as a double division
What is casting?
Casting is converting one type to another and storing it in another variable e.g. convert d (a double variable) into an int and storing the result in c
Can you cast from a double to a string?
NO
What is a literal?
when we put actual values into the programme e.g.
String message = “Hola Chica!” –> is the literal
It is a hard coded value
What is a method?
a function
Can you put two variables inside the same scope, even if it's a different type? e.g. int a = 5; a = 6; or int a = 5; int a = 6;
NEVER
int a = 5;
a = 6;
is correct
Can you put two variables inside the same scope, even if it's a different type? e.g. int a = 5; a = 6; or int a = 5; int a = 6;
NEVER
int a = 5;
a = 6;
is correct
Can you put two variables inside the same scope, even if it's a different type? e.g. int a = 5; a = 6; or int a = 5; int a = 6;
NEVER
int a = 5;
a = 6;
is correct
How do you stop a programme from running from inside a programme?
What is the convention?
System.exit(int n);
Calling this method terminates your programme. The value n is passed back to whatever started your programme and the normal convention is:
N = 0 //sucessfully ran
N = 1 //unsuccessful
What is overloading?
same name for two different methods but with different arguments
e.g. to calculate the area of a rectangle
public static double rectangleArea (double width){ return rectangleArea(width, width); }
What is the signature of a method?
its name and argument list
computeArea(double myRadius)
What are the 5 conditions in Java?
If
Else
Else if
==,
not
How do you write comments?
//
/* */
In conditions, what does == mean?
equals
In conditions, what does != mean?
not equals
> =
greater than or equal to
<=
less than or equal to
How do you string operations together?
&&
(both conditions have to be true - and)
||
(either condition has to be true - or)
When conducting string equality, how should we compare two strings?
if (a.equals(b)) {…
}
DO NOT DO:
if(a==b){…}
What questions can you ask yourself to develop a good testing mindset?
How can I convince myself that this programme works?
What are good inputs to check (test cases)?
The import command
imports a class e.g. Scanner
import.util.Scanner;
System.in
gets text entry from the user’s keyboard
System.in.read()
a method that calls the System.in
nextLine()
collects all key presses and returns them into a String
nextLine()
collects all key presses and returns them into a String
What does \ denote?
A special character
\t
tab
\n
newline
Java shortcuts:
int i = 0;
System.out.println(i);
i++;
What does i++ do?
increases i by 1 each time
What are the 3 types of Loops java uses?
do Loops
while Loops
for Loops
What is a loop?
a compound statement that keeps repeating until some condition is met.
Scope rules apply!
how do we start writing a do Loop?
do{ //some conditions } while(condition);
if you declare a variable within it a loop, will it be visible outside the loop?
No
But loops can see variables declared before the loop!
how do we make a while loop?
while(condition){ //some condition }
what is the difference between a do loop and a while loop?
do loop - checks the condition at the end
while loop - checks the condition first
What is a counter loop and can you give an example of a type of look that does this?
loops that change some variable at each iteration (e.g. i++)
for loops
What is a counter loop and can you give an example of a type of look that does this?
loops that change some variable at each iteration (e.g. i++)
for loops
What is the syntax for FOR loops?
for(, , )
variable : to use for the loop and its initialisation (can be declared here or earlier)
e.g. i
condition : has to be true for loop to keep looping
change : how the variable should be changed at each iteration
i++
What does this mean?
for(int i=0; i<=9; i++)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
count up
for(int i=9; i>=0; i–)
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
count down
for(int i=0; i>=9; i+=4)
0, 4, 8
int i=1;i<=20;i*=5
1, 5
for(int i=0; i>=20; i*=5)
0000000 until programme ends
for (;;)
?
infinite loop
What are the two loop commands?
break
continue
what is a break?
jumps out of the loop
break;
what is continue in a loop?
jumps straight to the next iteration (if there is one)
e.g. prints only even numbers
String.format?
a method that allows us to combine Strings and numbers with control over:
- length
- padding (with spaces)
- number of digits after decimal point
String line = String.format(“%15s, %5d”, name, age)
What does %15s mean?
What does %5d mean?
%15s
string of 15 characters long. No truncation.
%5d
pads the integer to be total 5 characters
%d
gets the integer as is
%5d
pads the integer with spaces to give a length of at least 5
%-5d
adds padding after the integer
23—
%05d
pads with (0) instead of spaces
00023
How do you string.format doubles?
%f
e.g.%5.2f
pads to at least 5 with 2 digits after the decimal point
e.g. 3.1415 becomes -3.14
. counts as a character
%7.2f
—3.14
%7.3f
–3.142
integers:
%05.2f
pads with 0 rather than space
integers:
%-5.2f
does the padding after the number
What does String[] args denote?
a placeholder for information we want to pass into our program.
What does System.out.println() denote?
System is a built-in Java class that contains useful tools for our programs.
out is short for “output”.
println is short for “print line”.
What does System.out.print(). denote?
this type of print statement outputs everything on the same line
What does the {} denote
the scope of our classes and methods
What does it mean when we say Java is a compiled programming language?
the code we write in a .java file is transformed into byte code by a compiler before it is executed by the Java Virtual Machine on your computer
A compiler is a program that translates human-friendly programming languages into other programming languages that computers can execute.
The compiling process catches mistakes before the computer runs our code.
ava programs have at least one class and one main() method.
What does the class mean?
What does the main() mean?
Each class represents one real-world idea.
The main() method runs the tasks of the program.
What are the three primitive data types?
int
double
boolean
What can char hold?
any character, like a letter, space, or punctuation mark
must be surrounded by single quotes ‘’
Is String an object?
YES
How do we use a String literal?
How do we call a String class to create a String object?
literal:
A String literal is any sequence of characters enclosed in double-quotes (String greeting = “Hello World”;)
class: create a new String object by calling the String class when declaring a String (String salutations = new String ("Hello World!");
What are the 3 escape sequences?
\" allows us to add " to a String value e.g. ("\"Hello World\"") // prints "Hello World"
\\ allows you to print backlash \ ("This is the back slash symbol: \\") prints This is the back slash symbol:\
\n new line ("Hellow\nWorld!") prints: Hello World!
What does static typicing mean?
Static typing helps because bugs are caught during programming rather than during execution of the code.
Java programs will not compile if a variable is assigned a value of an incorrect type.