Docsity

JAVA PROGRAMMING I DATA INPUT AND MEMORY MANAGEMENT, Exams of Java Programming

Toggle sidebar
bg1
JAVA PROGRAMMING I DATA INPUT AND
MEMORY MANAGEMENT
Buffered Input - SOLUTION-Provides a way to create interactive
computer programs with input form the user through the keyboard
and or mouse, which can be processed and output by the
program.
InputStreamReader - SOLUTION-converts the integer values of the
System.in to its character representation
BufferedReader - SOLUTION-constructs a string from the character
values provided by InputStreamReader
Wrapper classes - SOLUTION-Wrapper classes convert form String
(a reference data type) to primitive data types and vice-versa
StringTokenizer - SOLUTION-The String Tokenizer class is used to
break a large chunk of infomration into smaller, useful toekns. The
process of breaking a string down into tokens is called parsing
Scanner class - SOLUTION-The Scanner provides a broad range of
options for data entry and parsing. It essentially consolidates the
functionality of buffered input, wrapper classes, and the
StringTokenizer class
2 Steps of Primitive Variable declaration - SOLUTION-1. Allocates
memory for the variable - Declaration
2. Association of variable name with meory location (initialization)
3 steps of Reference variable declaration - SOLUTION-1. A list of
all variable objects and the associated data types is provided to
the compiler
2. Allocation of sufficient memory for hte declared type
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download JAVA PROGRAMMING I DATA INPUT AND MEMORY MANAGEMENT and more Exams Java Programming in PDF only on Docsity!

JAVA PROGRAMMING I DATA INPUT AND

MEMORY MANAGEMENT

Buffered Input - SOLUTION -Provides a way to create interactive computer programs with input form the user through the keyboard and or mouse, which can be processed and output by the program. InputStreamReader - SOLUTION -converts the integer values of the System.in to its character representation BufferedReader - SOLUTION -constructs a string from the character values provided by InputStreamReader Wrapper classes - SOLUTION -Wrapper classes convert form String (a reference data type) to primitive data types and vice-versa StringTokenizer - SOLUTION -The String Tokenizer class is used to break a large chunk of infomration into smaller, useful toekns. The process of breaking a string down into tokens is called parsing Scanner class - SOLUTION -The Scanner provides a broad range of options for data entry and parsing. It essentially consolidates the functionality of buffered input, wrapper classes, and the StringTokenizer class 2 Steps of Primitive Variable declaration - SOLUTION -1. Allocates memory for the variable - Declaration

  1. Association of variable name with meory location (initialization) 3 steps of Reference variable declaration - SOLUTION -1. A list of all variable objects and the associated data types is provided to the compiler
  2. Allocation of sufficient memory for hte declared type
  1. once the object is instantiated, the first byte of the address is associated with the variable name Control structures - SOLUTION -A segment of a program that takes control of the program. Control structures have one point of entry and one point of exit in the program. Logical or boolean operators - SOLUTION -Logical operators evaluate logical conditions. Often used in control structures, they return one of two values - false or true Selection controlled structure - SOLUTION -Selection control structures use conditional tests to change the path of a program. Rather htan follow the program from one statemtn to the next, statements can be skipped and new paths selected One way selection - SOLUTION -The conditional path is selected if the condition is true. If false, the corresponding statements will be skipepd and the liens of code following the block will be executed instead. Two way selection - SOLUTION -Regardless of conditions, one of two paths (blocks of code) are taken. Both paths then converge on the same line of code. Multi way selection - SOLUTION -Mulitple paths exist that can be taken depending on the conditional tests Decision tree - SOLUTION -Decision tree structure are nested multi way selection structures
  1. Loop condition - tests value of LCV
  2. Loop body
  3. Update the LCV Symmetrical bound counter - SOLUTION -<= in the loop condition Means the count = (b-a) + 1 Asymmetrical bound counter - SOLUTION -< in the loop condition means the count = (b-a) Event controlled loop - SOLUTION -(while loop) A loop that terminatesbased ona condition (or conditions) inside a loop. Sentinel controlled loop - SOLUTION -Uses an artificial data (ADV) value within a data set to tirgger the end of a control loop. For example, a program may not need a negative value. The writer of the program can then use a negative number as the sentinel value. As long as the user doesn't enter a negative number ,the loop will continue to be processed. Flag controlled loop - SOLUTION -A boolean statement is used to control the flow of the program. For example: in a situation where all nubmers are valid (postiive, negative, or zero) you must rely on a logical statement to exit the loop. For example, if the next input is not a floating pointn umber, then the condition is false and exits the loop Pre test loop - SOLUTION -(z trip) Test is prior to the code block. This means it's possible the code block will never get executed. Post test loop - SOLUTION -(1 trip) - test happens after the code block. This means the code block gets executed at least once.

3 parts of Methods - SOLUTION -Calling statements Heading Method body Calling a method - SOLUTION -A general purpose is called by giving hte method's name and passing parameters to it. Functions of main method - SOLUTION -1. constructs objects

  1. Invokes other methods
  2. Prints out results
  3. Prints the expected results
  4. Static methods are placed after the main() method
  5. Non static methods are declared and placed before the main()
  6. Method nesting is not permitted in Java. Each method must be constructed as a separate entity with its own parameters and variables Method overloading - SOLUTION -Two methods with the same name but different parameter types (and number of parameters). The compiler can distinguish between these methods because they have different signatures. This can be useful when you want to accoint for different data types the user may use (For example double float, and integer) and still compute a result. Method header - SOLUTION -Consists of:
  7. Primary scope
  8. Secondary scope
  9. Return type (can also be void when no value is returned)
  10. The method name
  11. List of formal parameters (when empty, known as empty argument method)

The caller can use the values directly in the expression, system statement, or it must have allocated storage to hold the returned values Static methods - SOLUTION -Static methods receive all of their information from the arguments. Static methods are usually public and are called general methods. In order to reference them, they do not have to be prefaced by the class name Strict methods - SOLUTION -Strict methods operate on objects and are identified by the object they act on and their class Objects and Classes - SOLUTION -In object oriented languages, all data types are constructed as classes, and all data values are objects Class characteristics - SOLUTION -A class is a programmer defined data type, a combination of data, and their associated operations Class = allowable data + operation capabilities OR Class = data + methods Components of an OOP class - SOLUTION -class name Class member declaration Constructors (kind of method) Methods Class members - SOLUTION -Constructors, methods, and variables Variable members - SOLUTION -Also referred to as data members or instance variables

Data operations - SOLUTION -Data operations can only be executed using class members Public vs private - SOLUTION -Normally the data members are declared as private and the methods as public Constructors - SOLUTION -A method that has the same name as the class and no return statement. Used to initialize the variables. A class can have one or more constructors. Multiple constructors are ok as long as they have different signatures (number and type of parameters) Scope - SOLUTION -The section of a program within which an identifier is valid or known Block scope - SOLUTION -Variables and parameters with block scope can be used from their point of declaration until the end of their block code. Local variables take precedence over the class variable with the same name Class scope - SOLUTION -These variables and parameters are available throughout all modules of a class. Class variables can be accessed by using the class name in the statement. The variables declared in outer blocks can be accessed within all inner blocks. The variables in outer blocks are static. Variables declared within inner blocks cannot be accessed in any enclosing outer blocks. The variables within a block go out of scope after the block end } Computer Science - SOLUTION -The study of how computers can be applied effectively and efficiently to solve problems.

Problem of Synthesis - SOLUTION -A formal problem where the initial condition (input) and the plan of action are known. The goal is to identify the result (output). Problems of Synthesis are generally solved by a computer. Real Formal Problems - SOLUTION -Real formal problems - The making of a product Artificial Formal Problems - SOLUTION -Simulated problem (playing games/AI). Problems of Analysis and Synthesis - SOLUTION -Generally speaking software engineers convert problems of analysis to problems of synthesis which can then be solved by computers. Plan of Action - SOLUTION -A general outline of the major steps we need to accomplish our goal, starting at predefined initial conditions. Algorithm - SOLUTION -Algorithm is a set of defined instructions which result into a solution Problem Theory - SOLUTION -The study of characteristics of problems. Most problems have certain characteristics that are independent of the details of any particular problem or may have a relation to other problems in general. Algorithm as it Relates to Problem Theory - SOLUTION -Algorithms can be defined as a refinement of a plan of action. Computer Program? - SOLUTION -A computer program is the statement of an algorithm in a computer programming language.

It is a collection of instructions that a computer can understand, process and accomplish. It is an exacting task. Algorithm - SOLUTION -An algorithm is a programming recipe. Recommendations for algorithm selection - SOLUTION -1. Avoid algorithms which cause informal problems.

  1. Avoid algorithms with undetermined solutions
  2. Avoid algorithms which require excessive amounts of time.
  3. Select algorithms that closely fit the specifications
  4. Select algorithms which allow for assumptions
  5. Select algorithms which don't generate multiple problems 6.1 Examine the algorithm's efficiency Four Stages of Program Development - SOLUTION -1. Program Planning
  6. Program Coding
  7. Testing and Debugging
  8. Maintenance/Refactoring Three Principles of Programming Style - SOLUTION -1. Program structure and design
  9. Program Documentation
  10. Program Overall Appearance and Style Program design structures - SOLUTION -Top Down and Bottom Up Bottom up design - SOLUTION -Modules are designed first, then put together to form the main program. Advantage of Bottom up design - SOLUTION -Less expensive and faster to build

Purpose of OOP - SOLUTION -To provide software developers with: Reliability Cost effectiveness Adaptability Understandability Reusable Object - SOLUTION -All objects have:

  1. Name
  2. Behavior
  3. Receives input (receives message) Example: Remote Control - SOLUTION -name - remote control behavior - changes channels on a television receives input - from user using the number/keypad Stages of Program Design - SOLUTION -stages
  4. Input Phase
  5. Output Phase
  6. Formulation of Assumptions
  7. Constraints Input phase of Program Design - SOLUTION -Identify where input comes from and what they are. User? Storage? Output phase of Program Design - SOLUTION -Identify the outputs of the computer program. Does something get displayed on the screen? On a printer? Abstract data type - SOLUTION -Refers to a data type together with a set of operations that can be performed on that data structure.

Object Oriented Programs - SOLUTION -Complex software programs requiring proper abstraction and separating the internal implementation from external behavior of the object. Characteristics of OOP - SOLUTION -Polymorphism Inheritance Encapsulation Polymorphism - SOLUTION -The capability of an object to assume different forms. Messages can lead to different operations depending on the object receiving it. Example: Context based right clicking Polymorphism - SOLUTION -The mechanism by which one general interface can be used to access different specific implementations. Inheritance - SOLUTION -Each object is related to one or more objects. One class of objects inherits data behavior from another object. In other words, sub classes share characteristics of the class they are derived from. Encapsulation - SOLUTION -The mechanism that binds together codes and data and prevents unauthorized access to the heart of the class which is private. A class is designed such that the implementation is protected from the action of external code except through form interface. Module - SOLUTION -A structure consisting of interrelated segments arranged in logical order to form an integrated unit. It is constructed basically from classes.

Wrapper Classes - SOLUTION -Wrapper classes allow us to convert between different variable types: strings, integers, doubles, booleans, etc String Tokenization - SOLUTION -Using ReadLine(), it is possible to convert a string of multiple items into individual values (tokens). _______ _______ generally requires wrapper classes to convert between different integer types Parsing - SOLUTION -The process of tokenization Primitive vs Reference Data Types - SOLUTION -Primitive data types can perform simple arithmetic. Reference data types require methods to be manipulated. String? - SOLUTION -A string is a REFERENCE data type where the variable references the location in memory where the actual value is stored. The actual stored value is the object. Instantiation - SOLUTION -The process of allocation that creates a SPECIFIC instance of an object Two data types that don't have to be instantiated - SOLUTION - Strings and arrays two ways to declare the string msg = "foo" - SOLUTION -String msg = new String ("foo"); //with instantiation String msg = "foo"; //without instantiation Automatic Dereferencing - SOLUTION -The process of obtaining the address from a reference variable and going to the address to retrieve the correct number of bytes for the object.

handle dangling pointers caused by string literals - SOLUTION -How does Java handle dangling pointers caused by string literals declared without instantiation? Java periodically runs a GarbageCollection method to eliminate objects with zero reference. when a Primitive Data Type is declared? - SOLUTION -1. A space is reserved for an int data type

  1. The variable name gets associated with the first byte of allocated storage Reference Data Type is declared - SOLUTION -reference data type
  2. A list of all variable objects and their associated data types are generated for compiling.
  3. Sufficient memory is allocated for the declared type as referenced by the address.
  4. The first byte of the address is assigned to the reference name, only after the object is instantiated. The allocation is not done until the object is instantiated. Therefore string msg; does nothing. Logical Operators - SOLUTION -OR NOR XOR XNOR AND NAND NOT Use of Logical Operators - SOLUTION -1. For Supporting multiple or complex conditions
  5. For setting or clearing bit sequences OR - SOLUTION -True when any input is true. The java syntax is || Used for bit setting (sets any or all bits to 1) in a sequence.

two types of control structures - SOLUTION -Selection Control Structure and Loop Control Structure Selection Control Structure - SOLUTION -Allows the program to execute one of the alternative paths. The selection is governed by the outcome of a conditional test that evaluates if a statement is true or false. Three types of selection control structures? - SOLUTION -One-way selection control structure, two-way selection control structure, and multi-way selection control structure. one way selection control - SOLUTION -which way? The conditional path is selected if the condition is true. If false, the corresponding statement(s) will be passed and the lines of code following the block are executed. Two-way selection control - SOLUTION -Regardless of conditions, one of two paths will always be taken Multi-way Selection Control - SOLUTION -Selection control that involves more than two structures. Two types of multi-way selection control - SOLUTION -Nested selection control and switch control structure Nested Control structure - SOLUTION -Multiple number of "IF" statements are nested during a process. There are two forms of nested control structure, left-decision tree and right-decision tree left-decision tree nested control - SOLUTION -the "IF"s are nested in the "IF" structures not in the else block structure. This decision

tree control structure is not commonly used. The syntax is as follows: IF (cond1) IF (cond2) Statement 1 ELSE Statement 2 ELSE Statement 3 Right decision tree - SOLUTION -in this nested control structure, the statements are tested until a true statement is found. This is the most common nested control structure. The syntax follows: IF (cond1) Statement 1 ELSE IF (cond2) Statement 2 ELSE IF (cond3) Statement 3 ELSE Statement 4 Switch Control - SOLUTION -The switch statement allows you to choose from a fixed set of alternatives. It is used in a narrow collection of situations. Switches should not be used with real numbers. only Integers and integer compatibles (characters) Switches must use "breaks" to avoid code "fall through" When to use switch control - SOLUTION -1. When a comparison (equality) is performed