Procedural Programming Flashcards Preview

3. Procedural Programming (CSC 1025) > Procedural Programming > Flashcards

Flashcards in Procedural Programming Deck (87)
Loading flashcards...
1

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.

2

What is the case if there are errors at the compilation stage?

Compilation can't take place.

3

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.

4

What is a virtual machine?

Reads + runs .class file.

5

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.

6

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.

7

Why do we type String [] args?

This relates to parameters the method requires. A string array will be referred to variable name "args".

8

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();

9

What should we do when taking user input?

Check user has entered correct data as users easily make mistakes.

10

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: ");

11

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.

12

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.

13

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.

14

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.

15

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.

16

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.

17

What are the 4 integer types?

Byte, short, int + long.

18

What are the 2 floating point types?

Double + float.

19

What are the 2 non number related primitive types?

Boolean (1 bit) + char (2 bytes, 16 bits)

20

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

21

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

22

How do you declare char values?

Enclose in single quotes, any printable character e.g. char initial = 'n' or '+'. Anything in unicode.

23

How do you declare a boolean value?

No null value, no numeric equivalent. Boolean isHappy = true; Must be lowercase.

24

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

25

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.

26

What is another way of typing -1 or +1 in java?

Type variable -- or variable ++

27

What happens if you mix integer + floating point variables in an arithmetic expression?

You get a floating point number.

28

To find the remainder when dividing, what must we do?

Instead of using mod, we use % e.g. 7%4 = 3.

29

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);

30

How do we find the length of a string?

int n = stringName.length(); A string of length 0 is empty + denoted by "".