AP CSA Unit 1

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

how many bytes can an integer store and what happens it an integer stores more than that amount

. There is an Integer.MAX_VALUE defined as 2147483647 and an Integer.MIN_VALUE defined as -2147483648. If you try to store any number larger or smaller than these numbers in an int variable, it will result in an integer overflow where an incorrect value could be stored

COmmand lines end in { } are used to // and /* */ are used for

; semicolon enclose blocks of code comments

what is a type

A type is a set of values (a domain) and a set of operations on them. For example, you can do addition with int's and double's but not with booleans and Strings.

What is a variable

A variable is a name associated with a memory location in the computer. Computer memory can store a value and that value can change or vary. The following video explains what a variable is and gives a couple of real word examples of variables.

arithmetic operation that uses at least one double value

An arithmetic operation that uses at least one double value will evaluate to a double value.

An attempt to divide an integer by zero will result in an

ArithmeticException

In place of the assignment operator___

Compound assignment operators (+=, -=, *=, /=, %=) can be used in place of the assignment operator.

bits

Computers store all values using bits (binary digits). A bit can represent two values and we usually say that the value of a bit is either 0 or 1. When you declare a variable, you have to tell Java the type of the variable because Java needs to know how many bits to use and how to represent the value. The 3 different primitive types all require different number of bits. An integer gets 32 bits of space, a double gets 64 bits of space and a boolean could be represented by just one bit.

What is a class in java

Every program in Java is written as a class

Integer overflow

For example, int values are stored in 4 bytes of memory. There is an Integer.MAX_VALUE defined as 2147483647 and an Integer.MIN_VALUE defined as -2147483648. If you try to store any number larger or smaller than these numbers in an int variable, it will result in an integer overflow where an incorrect value could be stored. Try it below.

How to make comments?

In Java and many text-based coding languages, // is used to mark the beginning of a comment. For multi-line comments, use /* to start the comment and */ to end the comment. The compiler will skip over comments. However, it is a good idea to use comments to make notes to yourself and other programmers working with you. Here are some examples of good commenting:

Doing division with integers and double special circumstances

Java assumes that if you are doing division with integers that you want an integer result and it will truncate and throw away the part after the decimal point. But, if you use a mixture of integers (int) and decimal (double) numbers Java will assume that you want a double result. If there is at least one double in the operation, Java will widen the type of the other operand to double too and return the result in a double. If you have integers and you want a double result from some mathematical operation cast one of the integers to a double using (double) as shown above.

Java is what type of language

Java is an object-oriented language

==

Java uses the operator == to test if the value on the left is equal to the value on the right and != to test if two items are not equal.

note about the exam for x++ or ++x

On the exam you can use x++ or ++x to add one to the value of x. These two shortcuts only have different results if you assign the value of x to another variable as in int y = ++x; (where the value of x is incremented before y is set to it) or int y = x++; (where y is set to a copy of x's value before x is incremented). The AP exam will never use a shortcut in an assignment statement, so you don't need to worry about the difference between ++x or x++.

operators can be used to construct ____

Operators can be used to construct compound expressions.

what is a string

String is one of the object types on the exam and is the name of a class in Java. A string object has a sequence of characters enclosed in a pair of double quotes - like "Hello". You will learn more about String objects in Unit 2.

Two different print commands in Java?

System.out.println(value) : prints the value followed by a new line (ln) System.out.print(value) : prints the value without advancing to the next line

camel case

The convention in Java and many programming languages is to always start a variable name with a lower case letter and then uppercase the first letter of each additional word. Variable names can not include spaces so uppercasing the first letter of each additional word makes it easier to read the name. Uppercasing the first letter of each additional word is called camel case. Another option is to use underscore _ to separate words, but you cannot have spaces in a variable name.

declaring a variable

To create a variable, you must tell Java its data type and its name. Creating a variable is also called declaring a variable.The type is a keyword like int, double, or boolean, but you get to make up the name for the variable.

how to declare a variable

To declare (create) a variable, you specify the type, leave at least one space, then the name for the variable and end the line with a semicolon (;). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false)

How can values of type doubles be rounded to the nearest integer for NEGATIVE numbers?

Values of type double can be rounded to the nearest integer by (int)(x + 0.5) or (int)(x - 0.5) for negative numbers.

Primitive variable

When you create a primitive variable Java will set aside enough bits in memory for that primitive type and associate that memory location with the name that you used.

what can a variable not have

While you can name your variable almost anything, there are some rules. A variable name should start with an alphabetic character (like a, b, c, etc.) and can include letters, numbers, and underscores _. It must be all one word with no spaces.

dividing by 0 results in what

With division, another thing to watch out for is dividing by 0. An attempt to divide an integer by zero will result in an ArithmeticException error message.

What is a compile time error?

an error detected by the compiler.

Boolean

boolean - which store Boolean values (either true or false).

During execution,

expressions are evaluated to produce a single value

Value of an expression

has a type based on the evaluation of the expression

Object variables

hold a reference to an object of a class

casting operators

int and double

an arithmetic operation that uses two int values will evaluate to an ____

int value. With integer division, any deciminal part in the result will be thrown away

three types of primitive variables

int, double, boolean

when a variable is declared final___

its value cannot be changed once it is initialized.

Inside the class there can be a

main method that starts the program.

int values can also be

negative

arithmetic expressions inclue expressions of

type int and double

common mistake - don't forget to specify the _____

type when declaring a variable (using name = value; instead of type name = value;)

primitiive variables

that hold primitive types A reference is a way to find the object (like a UPS tracking number helps you find your package).

assignment statements

Assignment statements initialize or change the value stored in a variable using the assignment operator =. An assignment statement always has a single variable on the left hand side.

What is code tracing

Code Tracing is a technique used to simulate a dry run through the code or pseudocode line by line by hand as if you are the computer executing the code. Tracing can be used for debugging or proving that your program runs correctly or for figuring out what the code actually does. Trace tables can be used to track the values of variables as they change throughout a program. To trace through code, write down a variable in each column or row in a table and keep track of its value throughout the program. Some trace tables also keep track of the output and the line number you are currently tracing.

Evaluation, operands are associated

During evaluation, operands are associated with operators according to operator precedence to determine how they are grouped. (*, /, % have precedence over + and -, unless parentheses are used to group those.)

Note about curly brackets

In Java every open curly brace { must have a matched close curly brace }. These are used to start and end class definitions and method definitions.

Type casting

In Java, type casting is used to convert variables from one type to another. By casting we don't mean something to do with fishing, but it is a similar idea to casting a pot in clay. In Java when you cast you are changing the "shape" (or type) of the variable.

Note about camptializing command key words and class names - what's the difference?

Most command keywords in Java must be in lowercase, but class names such as System and String are capitalized. Commands in Java must end with a semicolon (;). Think of the semicolon (;) in Java like a period (.) in English. You use a semicolon (;) to show the end of a Java statement, just the way you use a period (.) to show the end of an English sentence. You will not be penalized on the exam if you forget the semicolon. However, your programs won't run without it.

compound expressions are evalueated _______are used so that *, /, and % are done before + and -

Operators can be used to create compound expressions with more than one operator. You can either use a literal value which is a fixed value like 2, or variables in them. When compound expressions are evaluated, operator precedence rules are used, so that *, /, and % are done before + and -. However, anything in parentheses is done first. It doesn't hurt to put in extra parentheses if you are unsure as to what will be done first.

What are syntax errors

Syntax errors are reported to you by the compiler if your Java code is not correctly written. Examples of syntax errors are a semicolon ; missing or if the code has a open curly brace { or open quote ", but no close curly brace } or close quote ". Informally, a syntax error is called a bug, and the process of removing errors is called debugging.

What is a string literal

The "Hi there!" is called a string literal, and it can have zero to many characters enclosed in starting and ending double quotes. A string literal is enclosed in double quotes ('' '').

Assignment operator

The assignment operator (=) allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.

how to use casting operators

The casting operators (int) and (double) are used right next to a number or variable to create a temporary value converted to a different data type. For example, (double) 1/3 will give a double result instead of an int one. Run this code to find how Java handles division and what casting can do to the results. Notice what happens when you divide an int by an int or an int by a double or an int casted to a double divided by an int.

Keyword final

The keyword final can be used in front of a variable declaration to make it a constant that cannot be changed. Constants are traditionally capitalized.

The modulo operator

The percent sign operator (%) is the mod (modulo) or remainder operator. The mod operator (x % y) returns the remainder after you divide x (first number) by y (second number) so 5 % 2 will return 1 since 2 goes into 5 two times with a remainder of 1. Remember long division when you had to specify how many times one number went into another evenly and the remainder? That remainder is what is returned by the modulo operator.

value of expression for the assignment statement

The value of the expression (which can contain math operators and other variables) on the right of the = sign is stored in the variable on the left.

some programming code causes int values to be automatically cast to double values. Values of type double can be rounded to the nearest integer by

Values of type double can be rounded to the nearest integer by (int)(x + 0.5) or (int)(x - 0.5) for negative numbers.

Java limits decimal numbers special crcumstances

What happens to repeating decimal numbers like 3.333333...? Java limits the number of digits you can save for any double number to about 14-15 digits. You should be aware that the accuracy of any calculation on a computer is limited by the fact that computers can only hold a limited number of digits.

string concatentation operator

When you are printing out variables, you can use the string concatenation operator + to add them to another string inside System.out.print. Never put variables inside quotes "" because that will print out the variable name letter by letter. You do not want to print out the variable name, but the value of the variable in memory. If you're not sure what this means, try putting quotes around the variable and see what happens. In the print out, if you want spaces between words and variables, you must put the space in the quotes. If you forget to add spaces, you will get smushed output like "HiJose" instead of "Hi Jose".

Examples of Doing division with integers and double special circumstances

int nearestInt = (int)(number + 0.5); int nearestNegInt = (int)(negNumber - 0.5);

Two types of vairables

primitive variables and object variables t

Template for a simple Java program with a main method:

public class MyClass { public static void main(String[] args) { // Put your code here! } }

how a decimal number can be formatted to show 2 digits after the decimal point.

public class TestFormat { public static void main(String[] args) { double number = 10 / 3; System.out.println(number); System.out.println( String.format("%.02f", number) ); } } 3.0 3.0 3.00 3.00

double

which store floating point numbers (decimal numbers like 6.3 -0.9, and 60293.93032)

Int

which store integers (numbers like 3, -76, 20393) stores integers

shortcut to x=x%2

x %= 2;

shortcut to x=x*2

x *= 2;

shortcut to x=x+1

x += 1; and x++;

shortcut to x=x-1

x -= 1; and x- -;

shortcut to x=x/2

x /= 2;


Ensembles d'études connexes

Introduction to Proof Assignment and Quiz

View Set

Intro to Psychology - Chapter 11 Exam

View Set

Social Psychology - Lecture 8 (Empathy)

View Set

Comptia Project + - Project Basics

View Set

Chapter 6 Multiple Choice (section 2)

View Set