CSCD210 Amack pt 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

formatting printf

%#.## first # is field width (3 in this example) second # is precision (2 in this example) third # is conversion code/format specifier (b,c,d,f,e,s) in order to actually print "%" use %% - : left justified meaning it denotes blank space

loops

(0 based) can be used to tell a program to repeatedly execute statements. loop-continuation

generate random char

(char)('a' + Math.random() *('z' - 'a' + 1)) between a and z: (char)*+(ch1 + Math.random() * (ch2 - ch1 +1))

Comparing Strings

.equals(s1) : return true if this string is equal to string s1. .equalsIgnoreCase(s1) : returns true if this string is equal to string s1 (case insensitive). .compareTo(s1) : returns an int greater than 0, equal to 0 or less than 0 to indicate whether the string is greater than, equal to, or less than s1. .compareToIgnoreCase(s1) : returns an int greater than 0, equal to 0 or less than 0 to indicate whether the string is greater than, equal to, or less than s1 (case insensitive). .startsWith(prefix) : returns true if this string starts with specified prefix. .endsWith(suffix) : returns true if this string starts with specified suffix.

Obtaining substrings

.substring(beginIndex) : retunr this string's substring that begins with the char at the specified beginIndex and extends to the end of the string .substring(beginIndex, endIndex) : returns this string's substring that begins with the char at the specified beginIndex and extends to the char at endIndex - 1.

terminate program

0: normal termination 1: error message Sytem.exit(#);

Reuse methods

ClassName.methodName ex: TestMax.max();

unchecked exception

RuntimeException, Error, and their subclasses

printf

System.out.printf(format,items); accepts: decimal integers, fp numbers, boolean values, chars, strings, numbers in scientific notation.

infinite loops

a loop that never becomes false

iteration

aka "repetition", an execution of a loop body

call stack

aka "the stack" Memory that stores parameters and variables for the method and activates the record. activation records are removed from the stack once the method has been returned

calling a method

aka invoke. if method returns a value, it is called as a value. ex: System.out.println(max(3,5)); if method does not return a value, it is called in a statement. ex: System.out.println("Hello World!");

exception

an object that represents an error or a condition that prevents execution from proceeding normally

create array

arrayRefVar = new elementType[arraySize]; if a variable does not contain a reference to an array, the value of the variable is null.

array size and default values

arrayRefVar.length primitive data types: 0 char: '\u0000' boolean: false

specified identifiers % b % c % d % f % e % s

boolean value character decimal int fp scientific notation string

char arrays

can use S.O.P()

linear search

compares key element sequentially with each element. Returns the index (position) of the matching element OR -1 if no match is found

initialize an array

dataType[] arrayName = { #,#,#}

while Loops

declare variable OUTSIDE of loop because otherwise it wont be declared and initialized variable should start at 0 unless it will NOT fail the first iteration datatype variable = value; while(boolean) { //loop body; statement(s);}

local variable

defined inside a method

minimizing numeric errors

do not use float, double or long add small number before large ones

do-while Loops

do { //loop body; statement(s); } while (boolean); Use this over a regular while loop if you have statements that you need to have executed at least once

void method

does not return value must be called as a statement

copying arrays

elementType[] array = {value1, value2, ... valuek}; elementType[] arrayCopy = new elementType[array.length]; for (elementType e = 0; e < array.length; e++){ arrayCopy[e] = array[e];}

declare and create an array

elementType[] arrayRefVar = new elementType[arraySize];

initialize an array

elementType[] arrayRefVar = {value0, value1, valuek}; OR elementType[] arrayRefVar = new elementType[i]; arrayRefVar[i] = x; *remember that arrays are 0 based*

declare array

elementType[] arrayRefVar; elementType arrayRefVar[]; also works but above is preferred

binary search

elements must be ordered. compares the key to middle element. if key is larger/smaller it looks at the corresponding half of the element until a match is found.

sentinel value

ends a loop while (boolean x) { x is sentinel value

foreach

for (elementType element; arrayRefVar) { //process the element}

printing arrays

for (int i = 0; i < myList.length; i++){ System.out.println(myList[i] + " ");} unless char then use one print statement

for Loops

for(initial action; boolean; after action){ //loop body; statement(s);} initial action only runs first time and can be a list of zero or more comma separated expressions. after action can be a list of zero or more comma separated statements. control variable is usually declared in initial action and the after action typically increments/decrements the control value and eventually it will cause the boolean statement to be false, terminating the loop.

throws

goes after main throws errorType{

passing arrays to methods

in method header in parameters (elementType [] arrayRefVar OR {value,...valuek})

method abstraction

is achieved by separating the use of a method from its implementation

output redirection

java ClassName > output.txt

input redirection

java SentinelValue < input.txt

input and output redirection

java SentinelValue output.txt

file instance

java.io.File file = new java.io.File("name"); or Scanner kb = new Scanner(newFile("name"));

modularizing code

makes the code easy to maintain and debug and enables the code to be reused aka reducing redundant code

defining a method

method name, parameters, return value type and body: modifier returnValueType methodName(list of parameters) { body;}

terminating value-returning method

must use "return"

printing an array that is NOT a char

must walk it out

scope of variables

part of program that a variable is referenced

scanner

pass in a file or string from user. .close(); //ALWAYS CLOSE FILES .hasNext(); useDelimeter(pattern: String);

-->

passed by reference in Debugger

default values of arrays

primitive numbers = 0 char = '\u0000' boolean = false

selection sort

public class SelectionSort { public static void selectionSort(double[] list) { for (int i = 0; i < list.length - 1; i++){ double cM = list[i]; int cMI = i; for (int j = i + 1; j < list.length; j++) { if (cM > list[j]){ cM = list[j]; cMI = j;}} if (cMI != i) { list[CMI]=list[i]; list[i] = cM;}}}}

clear buffer

scannerName.nextLine();

checked exceptions

the compiler forces the programmer to check and deal with them in a try-catch block or declare it in the method header

throws: try catch ...finally

throws ...(){ try { Code to run; A statement or a method that may throw an exception; More code to run;} catch (type ex) { Code to process the exception;}//can have multiple catches finally{ //ALWAYS run ex stands for exception and is used as the variable name in most cases

overloading methods

use same method name as long as signatures are different(they have different parameters)

methods

used to define reusable code and organize and simplify coding ALWAYS comment before a method, defining it

Arrays

varType[] arrayName = new varType[# of entries] index starts at 0

returnValueType when method operations does not return a value

void

ambiguous invocation

when there are two or more possible matches for the invocation of a method, but the compiler can't determine the best match

passing parameters

when you call the method the parameters must match what was in the defined method


Set pelajaran terkait

C952 Computer Architecture PGKO Pre-Assessment Practice

View Set

Personal Finance - Chapter 18 Study

View Set

Chapter 4 Auding your Understanding Accounting

View Set

Financial Accounting - Module 9 - PP&E and Intangible Assets

View Set

psychology exam 4, Psychology exam 3, Unit 2 psychology, Psychology Exam 1

View Set

Salesforce Platform Developer I CRT-450 New Questions V9.02 | Killtest

View Set

The Greek City-States 4:Ancient Greece

View Set

The Heroes adventure and My hero 886- 896

View Set