Ap Computer Science Unit 1: Primitive Types

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

Scanner Class

A class within java.util. It contains code specifically designed to help with user input.

Double Division

A double divided by a double is a double! Ex) double x = 10; double y = 4; x / y > 2.5 While ints truncate the values of divided numbers, doubles do not. The value of a double division is what would be expected of a normal mathematical division that involves decimal places. A double divided by a double will remain a double, and is capable of storing decimal values.

String Literals

A sequence of characters enclosed in double quotations " ".

Compound Assignment Operators

Allows programmers to shortcut variable assignments that include the variable being assigned a new value: ex) x = x + y; shortcut: x += y; x = x - y; shortcut: x -= y; x = x * y; shortcut: x *= y; x = x / y; shortcut: x /= y; x = x % y; shortcut: x %= y;

variable.nextLine()

Allows users to input String values.

variable.nextDouble()

Allows users to input double values.

variable.nextInt()

Allows users to input int values.

Mixed Division

An int divided by double or a double divided by int will result in a double Ex) double x = 10; int y = 4; x / y > 2.5 y / x > 0.4 In the case where a double is divided by an int, or an int is divided by a double, the result will always be displayed as a double value. Note that in this example, the double that has been initialized has been written as 10 without the decimal point. If we were to print the value of x from this example, the output would be 10.0. Because the value was initialized as a double, even if we do not include the decimal place, Java knows to include it in its calculations.

char

Characters ('A', 'B', 'C', etc) Stores single character values in single quotations.

public class MyProgram

Class. The name of MyProgram must match the name of the file.

Declaration

Data type (int) + Variable name (numApples) ex) int numApples;

Initialized

Data type (int) + Variable name (numApples) = 5 < initialized ex) int numApples = 5;

Decrement

Decrease the value of a variable by one. variable --;

System.out.println

Displays output on the computer monitor and moves cursor to next line.

System.out.print

Displays output on the computer monitor.

Arithmetic Exception

Exception that is thrown to warn programmers about arithmetic errors in their code.

Java Main Skeleton

Includes the class and main method arguments. Must include both in order to run successfully.

Increment

Increase the value of a variable by one. variable ++;

public static void main(String args[])

Main method. Code to be run must be placed within the main method.

Modulus (%)

Means divide and take the remainder of two values. Ex) What is 17 % 5? 17 / 5 = 3 remainder 2 So: 17 % 5 = 2

Variable name (input)

Once the data type is declared, the next part is naming the variable. While in this example we use the name input, you can use any variable name when writing the initial steps for user input.

Package

Packages are used to group code into a folder for easy use. Java.util is a package that stores various classes of code that are used to add functionality to our programs.

Final

Prevents variables from changing value. ex) final numApples = 5;

(int)(x + 0.5)

Rounds the value of a double to the nearest whole number.

Initialized Scanner

Scanner input = new Scanner(System.in);

String input

String name = input.nextLine();

Data Type

The call Scanner at the beginning of this line of code is actually a data type. Scanner is its own type of data type that is defined in the Scanner class.

Import Statement

The first bit of code that is needed is the import statement. Essentially, this code is saying "import the class Scanner from java.util for use in this program".

Literal

The fixed value being assigned to a variable. Often primitive data types.

Integer.MAX_VALUE

The highest value Java is able to access. 2147483647 ex) int max = Integer.MAX_VALUE;

Argument or Actual Parameter

The last component is the argument, or actual parameter that is being input into the scanner. An actual parameter is a variable or value that you want to use in a given program. In order to create this new Scanner, we must give it the input value of System.in. System.in is a value that typically corresponds to keyboard input. Including this allows us to input keyboard data into our programs. This belongs to the same class as System.out.println.

Integer.MIN_VALUE

The lowest value Java is able to access. -2147483648 ex) int min = Integer.MIN_VALUE;

Creates A New Scanner

The next part of this line is the creation of a new Scanner object. In order to create a Scanner that will search for user input, we need to first create a new Scanner. The keyword new is used to indicate that we are creating a new Scanner object.

Order of Operations

The order in which mathematical expressions should be evaluated. Starts with Parentheses, Exponents, Multiplications and Division, Addition and Subtraction. (PEMDAS)

Casting

Turning something of one type into another type! ex) int x = (int) 5.3 = 5

Implicit Casting

When Java automatically casts the value correctly without the programmer needing to do so. Java will cast an int to a double, but will not cast a double to an int value.

Overflow

When a calculation relies on a number outside the acceptable number range, Java will wrap back to the MIN or MAX value depending on the value.

Dividing by Zero

Will cause an ArithmeticException. NO VALUE! Ex) int x = 5 / 0; double y = 3.0 / 0;

double

decimals (5.67 ,4.5, etc) Stores decimal value numbers.

Double input

double decimal = input.nextDouble();

User Input Skeleton

import java.util.Scanner; class MyProgram { public static void main(String[] args) { Scanner input = new Scanner(System.in); } }

Integer Division

int division is not equivalent to double division! Since the result is an integer, it gets truncated Ex) 5 / 2 = 2.5 29 / 10 = 2.9 5 / 2 = 2 29 / 10 = 2 This is because integer values cannot have decimal points. Since that is the case, the result must be an integer. Ints round down to preserve the existing ones place value, regardless of the decimal point value.

Integer input

int number = input.nextInt();

int

integers (5, 6 ,7, etc.) ; Stores whole number values . - In Java, an int is represented by 32 bits - Because there are 2 options (zero or one) that can be stored in each bit, the total number of potential combinations is 232 .

Partial Line Methods

nextInt() and nextDouble()

Full Line Method

nextLine()

Boolean

true/false (true) Store true/false value.


Ensembles d'études connexes

Speech Chapter 20 "Choose and Rehearse a Method of Delivery"

View Set

Constitution Clauses & Related Cases Quizlet

View Set

ATI: DM, ENDOCRINE & CONNECTIVE TISSUE DISORDER TEST

View Set

Quiz 8: Managing Interpersonal Conflict

View Set