csci 1226 fall 2015 midterm #1 reviews. types of computers: personal computers embedded systems ...

Post on 08-Jan-2018

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 Software:  Data & instructions  Algorithm  Pseudo-code  Data hierarchy:  8 bits  byte  1 or more bytes  data value (field)  1 or more data values/objects  object (record)  data may be stored on secondary memory (file) …COMPUTERS Exam tip! What are the differences? Exam tip! What are the differences? Exam tip! An object can contain other objects! Exam tip! An object can contain other objects!

TRANSCRIPT

CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS

Types of computers:Personal computersEmbedded systems Servers

Hardware: I/O devices: mice, keyboards, monitors, etcCPU (Central Processing Unit)Memory, Main/Primary vs secondary

COMPUTERS

Exam tip!Examples of each type?

Exam tip!What are the differences?

Exam tip!Examples of each type?

Software:Data & instructions

Algorithm Pseudo-code

Data hierarchy: 8 bits byte 1 or more bytes data value (field) 1 or more data values/objects object (record) data may be stored on secondary memory (file)

…COMPUTERS

Exam tip!What are the differences?

Exam tip!An object can contain other objects!

IDE: Integrated Development EnvironmentAlgorithms, pseudo-codeVariables:

Data types: int/double/String/boolean/charDeclaration e.g.) int a; String name = “name”;Naming rules and conventions

Math and special assignment operators+, -, *, /, %+=, -=, *=, /=. %=++, --

JAVA PROGRAMMING BASICS

Exam tip!Declare variables given description:e.g.) the number of words entered by the userwhether the user is 65 or older

Output: System.out.println() and System.out.print()

Print spaces With or without quotes Concatenation (using +, with math expressions, etc)

User input: Scanner class: instantiation, import, etcnext(), nextLine(), nextInt(), nextDouble(), etcUse nextLine() to

flush the input buffer

…JAVA PROGRAMMING BASICS

Exam tip!How to pause a program using nextLine()?

Exam tip!Print all values in one line or each value in one line

Exam tip!Write code to read 2 Strings and a number and ignore the rest: Jimmy Smith 45 ignore mee.g.) review A01

SYNTAX ERROR: NETBEANS

NetBeans notices syntax errors right away look for red squiggle and in margin

mouse over the to see the explanation

LOGIC ERROR

Compiler will not notice!System.out.println("A length by width"

+ "rectangle has an area of area.");Program just does the wrong thing

hopefully you can tell from the output!

→ x / (a + b) → (3 / 5) * (a + b) → k / (2 * a) + 1 / (3 – b) → -a / (3 – b) + 5 * b

MATH EXPRESSIONS

Exam tip!Translate regular math expressions into Java math expressions.

Syntax: if, if-else, if-else if, if-else if … else

Comparison operators==, !=, <, >, <=, >=

Logical operators&& (AND), || (OR), ! (NOT)

CONDITIONALS

Exam tip!Less/larger than or equal to always have “=“ after “<“ or “>”

Sequential and nested if-else:Convert one to the other

What’s the output?:

What’s the code?Possible outcomes:outcome #1: A, B, C, E, Foutcome #2: A, D, E, F

… CONDITIONALS

System.out.print(“A”);if (. . .){ System.out.print(“B”);}if (. . .){ System.out.print(“C”); if (. . .) { System.out.print(“D”); } else { System.out.print(“E”); }}

Can save the answer to a comparisonuse a boolean variableboolean workedOvertime = (hours > 40);

you don’t need parentheses, but it ’s easier to read

E.g.) Write Boolean expressions for whether… x is greater than 0 and y is less than 5 x is greater than zero or y is equal to z it’s not the case that x is greater than the product of y

and z

boolean VARIABLES

Exam tip!“whether” boolean

Break complex expressions downboolean failedMidterm = (midtermGrade < 50);boolean failedFinal = (finalGrade < 50);boolean failedBothTests = failedMidterm && failedFinal;

String comparisons (all return boolean values)oneString.equals(anotherString)oneString.equalsIgnoreCase(anotherString)oneString.startsWith(anotherString)oneString.endsWith(anotherString)

… boolean VARIABLES

Exam tip!boolean a = true, b = false, c = false;!aa && b (a || c) && (!b && !c) (a && !b) || c

STRING.TOUPPERCASE/TOLOWERCASE

Ask a string for upper/lower case versionSystem.out.println(“Do you want fries?”);String answer = kbd.next(); kbd.nextLine();String upperAnswer = answer.toUpperCase();if (upperAnswer.startsWith(“Y”))

…now accepts “Yes”, “yes”, “Yeah, “yup”, …

upperAnswer is “YES”, “YES”, “YEAH”, “YUP” answer is still “Yes”, “yes”, “Yeah”, “yup”

for and while loopsConvert between them

Loop structure Initialize, test/condition, update, process

LOOPS

for (initialize; test; update){

process;}

initialize;while (test) {

process;update;

}

Exam tip!When asked for an algorithm, make sure to include all these 4 steps!

Write loops to:Calculate sum, average, max, min, of numbersWhen to stop?

Negative number Yes/no

Track the variable’s values: What’s the output of the program?

… LOOPS

a = 1;a++;b = a + 6;b--;c = b * 5;c += a;a *= 10;b /= 2;c %= 4;a *= b + c;S.O.Pln(a + “:” + b + “:” + c);

Exam tip!Write the loops to find these values!

Exam tip!Be able to write a loop that stops for these!

public static final data-type CONST_NAME = valueNaming conventions

variableNameConventionCONSTANT_NAME_CONVENTION

NAMED CONSTANTS

DIVIDING INTEGERS & DOUBLES

Java uses / for both int and double division15 / 2 is 7 because 15 and 2 are both int15.0 / 2.0 is 7.5 because 15.0 & 2.0 are doubles

“Mixed” expressions use double division15.0 / 2 is 7.5; 15 / 2.0 is 7.5

(double) says use the double version of this (double)15 / 2 is 7.5; 15 / (double)2 is 7.5

If sum is 17, then (double)sum is 17.0but sum is still an int (17); it doesn’t change!

CHANGING DOUBLES TO INTS

Can also change the other wayuse (int) in front of a double value/variabledouble price = 15.99;int dollars = (int)price;int cents = (int)(100 * (price – dollars));System.out.println(dollars + " dollars and "

+ cents + " cents.");

NOTE: it doesn’t round off; it truncates!15 dollars and 99 cents.

To Round off, use: int approxDollars = (int)Math.round(price);

Loops with Conditionalswhile (num >= 0) { // say whether num is divisible by 5 if (num % 5 == 0) { System.out.println(num + “ is divisible by 5.”); } else { System.out.println(num + “ is not divisible by 5.”); } // get next number num = kbd.nextInt();}

NESTED STRUCTURES

if inside if:System.out.println(“A”);if (condition1) { System.out.println(“B”); if (condition2) { System.out.println(“C”); }}

… NESTED STRUCTURES

possible output:AABABC

pattern:always Asometimes Bwhen B, sometimes C

Rewrite sequential-if to cascading-if

… NESTED STRUCTURES

if (age < 18) {System.out.print(“Child”);

}if (age >= 18 && age < 65) {

System.out.print(“Adult”);}if (age >= 65) {

System.out.print(“Senior”); }

if (age < 18) {System.out.print(“Child”);

} else if (age < 65) {System.out.print(“Adult”);

} else {System.out.print(“Senior”);

}

Loops inside loops:Draw a rectangle:

Get w, h, and char from user

Conditionals inside loops:Repeated menu with switch

… NESTED STRUCTURES

Inside loopsInside if-elseInside method (variables and parameters)Inside class (instance and class variables)

SCOPES OF A VARIABLE/PARAMETER

top related