Download Programming Fundamentals: Requirements, Flowcharts, IDEs, Java Basics and more Exams Nursing in PDF only on Docsity!
COMPUTER SCIENCE 109: INTRODUCTION TO
PROGRAMMING
Requirements Gathering Requirements Gathering: to find out what the customer wants the program to do. Steps:
- Document review: a copy of the business's file to see what needs to be made and how it is served.
- Observation: observing the business to see what is being done and what is not being done.
- Meetings: to discuss about what the customer wants in a program.
- Surveys: a survey to find out customers' thoughts on the new program. Requirement document: These four tools are used to create a draft on program and reviewing it with customers. During the meetings and surveys step, the programmer should: A. Examine the customer's business competitors B. Examine the customer's business as a whole and how it does things C. Set up a schedule for observations
D. Find out if the customer's business is making a profit E. Ask for copies of the business's tax returns When writing a program that will do bookkeeping for a small business, the document review step should include going through ___. A. A list of documents to be input into the system B. Programs that competing businesses use C. A list of user requirements D. What the business owner wants the program to do E. Customer surveys When is the Requirements Document finished? A. Not until the customer agrees that it accurately describes everything he wants the program to do. B. Not until the customer gives it back to me after review. C. Not until the surveys are analyzed and results incorporated. D. Not until after the requirements review. E. Not until suggestions and requests have been incorporated into the document. Flowchart Symbols
Start/end symbol: to represent either the beginning or ending of a program.
Process symbol: how the programming is functioning.
- I.E. A symbol that handles calculations, analysis, and other functions.
- Input/output symbol: represents actions such as entering, displaying, or printing data.
- Display symbol: signifies that information is displayed to the user.
- Decision symbol: signifies that the user must choose an option based on specific criteria.
- I.E. A symbol to represent 'If the Box is empty'
Compiled code: translates a program written in a high-level language to the machine language of the computer ( high-level program is referred to as the source code ).
- Entire source code needs to be compiled before the program can be executed.
- Faster since the translation is completed in one step.
- Examples: C++, C#, COBOL, FORTRAN Interpreted code: translates source code line-by-line during execution.
- Flexible and can run interactively.
- Try out a few lines of code to see if they work without having to go through the steps of compiling and executing the program.
- Examples: JavaScript, Perl, Python, Ruby Public static void main: example question A. javac WelcomeWorld.java B. javac Welcome World.java C. javac HellWorld!.java D. javac HelloWorld.java
In Java, you compile a program by calling the class by its name. In this program the name of the class is WelcomeWorld (with no spaces) because the 1st line of the program is: class WelcomeWorld The static keyword indicates that the main method is a ___ method. A. Empty
Identifiers, Literals, Abstract Data types, Interface Identifiers: names used for identification (i.e. Can be the name of a class, method, or variable ). Literals: representation of a data type that provides value to a variable. What does the void keyword mean in the public static void main statement? A. The method cannot return a value B. The method is empty C. The method returns values D. The method returns only NULL values A. The method cannot return a value Other methods in other classes can receive and return values/variables, but main can't return anything.
- To end this, void is not a negative. Referring to image: The printMessage method is declared with the void keyword, meaning it performs its action (printing "Hello, World!") and then terminates without returning any value. Task-Oriented Methods : Sometimes, a method is designed purely to perform an action or task, such as printing to the console, updating the state of an object, or interacting with other parts of the program. In such cases, there's no need to compute a result or send data back to the caller, so using void makes the method simpler and more direct. We have an expert-written solution to this problem!
Java Naming Conventions: Classes, Interfaces, Methods, and Variables Classes and Interfaces
- Naming convention: capitalized nouns, each word capitalized.
Example: Employee, UnionEmployee, Remote Methods
- Naming convention: lowercase verbs.
Example: public void enterData( ) { } Variables
- Naming convention: short and meaningful.
Examples: int[ ] counter; double fees Constants
- Naming convention: all uppercase.
Examples: univers = ALL; Packages
- Naming convention: all-lowercase letters.
Examples: java.util.*; Java Keywords Examples
- case
- break
- public
- static
- if
- else
- char Java Naming Conventions: Rules, Conventions, Constants
short sn1 = 31000; short sn2 = - 31000; Primitive data type: int int (integer): medium data type that consumes 32 bits.
- Useful for keeping track of age.
- Most common for counters.
- Example: int customer_age = 18; Primitive data type: long long: holds the largest integer values that consumes 64 bits.
- When declaring the variable literally ( assigning a value to it ), the number must end with an L or l.
- Useful for keeping a track of world's population.
- Examples: long long_number1 = 4356334286 L; long long_number2 = 834 l; What is the value of myNumber in the following? int myNumber; A. - 1 B. Undefined C. 0 D. Null C. 0 The default value for short, int, or long is 0. If you need to save a long (longum) to an int (intnum), which of the following would be used?
A. long intnum = longnum; B. longnum1 = intnum; C. intnum = (int)longnum; D. intnum = longnum; C. intnum = (int)longnum; It uses a type cast to explicitly convert the long value to an int value. Note that this may result in loss of data if the long value is outside the range of the int.
- Can save a short value into an int variable, and an int value into a long variable; BUT, can't directly store a long variable into an int or short variable.
- Other example: if you want to save a long into an int long value_1; long value_2; long value_1 = (int) value_2; Java: Floating Point Numbers: Single precision vs Double precision Single precision: occupies 4 bytes, or 32 bits in computer memory.
- Represented by float data type in Java. --> Used when less precision is acceptable and memory savings are needed. Double precision: occupies 64 bits in computer memory.
- Represented by double data type in Java. --> Used when high precision for floating-point numbers is required. Float and Double Float and Double: to support decimal values or floating point literals.
- Floating point numbers are known as real numbers and are used to when we need precision in calculations. CAUTION:
- NOT used to represent values that require a very high precision.
double x = - 125. Java: Floating Point Numbers: Converting a Float value to a Double value + Dividing Can convert a float value to a double without loss of precision.
Example: float f = 0.25f; double h = double(f); Not possible to convert a double value to a float.
- Will cause an error because Java programming will not know what to do with the extra digits. Possible to divide a double by 0.0 without causing an error.
- Answer will be infinity. Will the following code work? char myChar = 'ABC'; A. Yes, myChar will display ABC B. Yes, but with AB C. Yes, but only A D. No, but you can only store one literal character in a char D. No, but you can only store one literal character in a char Char: short for character, 16 bits in size.
- Actual data stored doesn't take up more than 8 bits. --> Java allows 16 bits so that all characters in all human languages can be represented ( is in Unicode format ).
---> Unicode: all characters are represented by numeric value that is universally used. Consider the following code. What will the value of newChar be if displayed on the screen? char newChar = 65; A. 65 B. A C. Undefined D. - 90 B. A Declaring char variables can either be done via: a. Single quotes (i.e. char value = 'A') b. Omit the quotes and set the Unicode representation of the value. The char data type is most closely related to the ___ data type. A. Float B. Short C. Double D. Integer (int) D. Integer (int) Because the char stores an integer value, it is most like the integer data type. The integer value (2 bytes) is the Unicode representation of the character.
- Char can be used as if they were integers ( add one value to another ). Java: String Data Type: String Length, Upper and Lower case, Trim, Replace
C. No value D. True A. False The default for the boolean primitive type is FALSE. Java Boolean data is a primitive data type ( primitive means it comes packaged with the programming language ). Java Data Type Conversion: Implicit vs Explicit Implicit: Java will implicitly and automatically move the data without raising an error condition when numbers are moved from a data type of a given size into a data type of wider size without any loss of information.
- Examples: byte - > short - > int - > long - > float - > double
Example: int inValue = 100; long longValue = intValue ( implicit conversion from int to long ).
- Implicit conversion to/from other data types ( even if their storage space is wider ) is NOT ALLOWED ( Java will issue an error ). Explicit: Programmer can explicitly use a type cast to force Java to move a wider data type to a narrower data type by enclosing the narrower data type name in parentheses and placing it in front of the variable declared with the wider data type.
- Conversion from a double or float data type to any integer type will discard any fractional portion. Java ___ allows boolean true/false values to be converted to/from other data type. A. Never B. Always C. Implicitly D. Explicitly
A. Never Java prohibits boolean true/false values from being converted to/from ANY other data type. Assumes the binary representation of 42.75 is stored in a variable named myNumber that's declared as data type float. What numeric value will be stored in the variable theAnswer when the following Java statement is executed? float myNumber = 42.75f; byte theAnswer = (byte)myNumber; A. Java will raise an error B. 75 C. 42 D. 42. C. 42 When floating data point data is converted to an integer, any fractional portion is discarded. Thus 42 will be stored in the 8-bit integer variable theAnswer. Java: Arithmetic Operators: Modulo operator Modulo operator (%): return the remainder of the division between two numbers.
- Based on x/y: -- if x is greater than y, the result will be a number between 1 and y - 1. -- if x is less than y, the result will be x. Examples:
- 10 % 5 = 0 cause 10/2 = 5
- 5 % 10 = 5 cause 5/10 = 0.
Remainder is 0.5, therefore 5 is the answer
C. An instance of the Random class D. An integer for the lower value A. An integer for the upper value The lower bound (min) isn't required, since Java would just use 0.
- The code does need an integer for the upper value to determine the range for the random number generation. Remember, when using nextInt(int n) in Java, n specifies the bound, making the range from 0 (inclusive) to n (exclusive). The nextInt(int n) method in Java's Random class requires an argument, which represents the exclusive upper bound for the random number. In simpler terms, you need to specify the range of numbers from which you want to generate a random number. The Java Random class has a function that lets you generate a ___ number. A. Random B. Partial random C. Pseudorandom D. Fully random C. Pseudorandom It is a pseudorandom number because the bits being moved underneath the hood are not equally randomized.
- Appropriate for most applications that need random numbers.
- Not appropriate for advanced usage such as cryptocurrency. Which of the following is NOT a category for operators? A. Relational B. Logical C. Arithmetic D. Operational D. Operational
Operational is NOT a category for operators. The term has no meaning with respect to operators. Logical, arithmetic, and relational are all operator categories. If var1 is 17 and var2 is 4, what is the result of the statement? result = var1 % var2? A. 4 B. 2 C. 0 D. 1 D. 1 17/4 = 17 - 16 = 1 Remainder is 1, answer is 1 HOWEVER, IF result = var1 / var ANSWER is 4 because 17 and 4 are integers. Arithmetic Operators : fix notation
- x + y --> infix notation
- +xy --> prefix notation
- xy+ --> postfix notation All add the variables x & y together. Which of these is the correct way to see if the String object s1 equals 'green'? A. if (s1.equals("green")) { } B. if (s1 = "green") { } C. if (s1 := "green"){ } D. if (s1 equals "green"){ } A. if (s1.equals("green")) { } The correct way to see if the String object s1 equals 'green' is: if (s1.equals("green")){ }. Recall that the Java String object has a method