Procedural Programming Flashcards
Youcheng Sun
What is a computer program?
Collection of instructions that performs specific task when executed by comp, tells comp what to do, step by step. Rep of algorithm. To run it, we need a few things: editor, source file (.java), compiler, class files (.class), virtual machine + library files.
What is the case if there are errors at the compilation stage?
Compilation can’t take place.
What is a .class file?
Can be read by any device capable of interpreting/translating file into something it can run. Platform independent so portable. Class file name must match that of Java program you are coding. First letter should be uppercase.
What is a virtual machine?
Reads + runs .class file.
How do you instruct the comp to do something?
Use statements, like declaration, assignments or method calls. To instruct to do something multiple times, use loops such as for, while or do. If you want it do something based on a condition, use if else, switch or nested loops.
What are the reasons to have the main method as public and typing static + void after it?
Public allows everyone to access it so it can be seen + run. Static identifies that it isn’t related to an object + void tells comp that nothing needs to be returned from method.
Why do we type String [] args?
This relates to parameters the method requires. A string array will be referred to variable name “args”.
What is the Scanner used for + how do we access it?
Required import from standard Java libraries, java.util, which allows user to input data from console. Import statements must be at top of file. Must set up Scanner variable called userInput so we can store input. Must then set up int/double variable to read this info. We then code nextInt() method which saves input to variable. Must close scanner using userInput.close();
What should we do when taking user input?
Check user has entered correct data as users easily make mistakes.
What is JOptionPane used for + how do we access it?
Displays data + allows user input. Must import from Java libraries (import javax.swing.JOptionPane. To show data, use JOptionPane.showMessageDialog(null, “Message”);. To get input, String name = JOptionPane.showInputDialog(“Question: “);
What are the 3 steps of programming?
Design, commenting in java + coding + compiling + running code. Could encounter errors while compiling + running code so we must return to fix bugs as java is case sensitive.
What is a syntax error?
AKA compile time error. Not including semicolon, incorrect spelling, leaving out “ or not matching {} or (). The compiler will flag up these errors.
What is a runtime error?
AKA logic error. Incorrectly spelling statements in quotations, trying to do impossible calculation or referring to undefined value. Harder to trace as compiler won’t flag them up. Some runtime errors are so extreme that they generate an exception e.g. dividing by 0.
What is an algorithm?
Series of steps which help us to find solution to problem. Must first design on paper. Must be unambiguous, executable + terminating. We use pseudocode before writing actual program and after writing algorithm. Doesn’t have strict requirements as read by humans, not comp.
What is a variable?
Container to hold info that will change throughout program. Must include name + type when declaring them. Each type has associated size. Beware of overflow. 2 categories of variable types: primitive + non primitive. First letter should be lowercase + not start with nums. Use camel naming method + don’t use reserved words.
What is a primitive type?
Predefined by language + named by reserved keyword. Don’t share space in memory with other primitive values. 8 types: byte, short, int, long, double, float, boolean + char.
What are the 4 integer types?
Byte, short, int + long.
What are the 2 floating point types?
Double + float.
What are the 2 non number related primitive types?
Boolean (1 bit) + char (2 bytes, 16 bits)
Describe double type.
All transcendental math functions return double values {-sin;, cos();, sqrt();} Double is best choice when need accuracy over many iterative calcs or when manipulating large nums. To express, e.g. double d1 = 123.4; For scientific notation, double d2 = 1.234e2
Describe float type.
Faster on some processors (takes up half space as double + imprecise when values very large/small). Useul when need fractional component but don’t require it to be very precise. e.g. fuel price. To express, e.g. float f1 = 123.4f
How do you declare char values?
Enclose in single quotes, any printable character e.g. char initial = ‘n’ or ‘+’. Anything in unicode.
How do you declare a boolean value?
No null value, no numeric equivalent. Boolean isHappy = true; Must be lowercase.
How do we convert a floating point num to an integer?
Using casting. Must be done this way as fractional part is lost + magnitude may be too large so java won’t let you do it normally. e.g. int myInt1 = (int)myDouble1
What are some examples of reserved words you can’t use when declaring variables?
Any of the data types, catch, continue, extends, final, if, package, super, private, try + while.
What is another way of typing -1 or +1 in java?
Type variable – or variable ++
What happens if you mix integer + floating point variables in an arithmetic expression?
You get a floating point number.
To find the remainder when dividing, what must we do?
Instead of using mod, we use % e.g. 7%4 = 3.
How do we find the root of a number?
How do we find the note the power of a number?
Math.sqrt(x);
Math.pow (x,n);
How do we find the length of a string?
int n = stringName.length(); A string of length 0 is empty + denoted by “”.
What is a string?
A set of unicode characters.
How do you return a char value from a string?
Use charAt method e.g. char start = name.charAt (0); OR name.charAt(length-1);
What is a substring?
Allow us to pick out parts of strings e.g. str.substring(start, pastEnd); \n String name = “Hello W”; \n String sub = name.substring (0,5); to give us Hello
How can we include a quotation mark in a string?
We can use \ e.g. “He said "Hello"”. We can also use backslash for file references + take a new line using \n.