CS 144

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

Program Structure;

- A program contains any number of classes - Classes contain any number of methods and variables - Methods contain some statements or instructions for the computer

Primitive Data Types; Boolean

-Boolean Values

Primitive Data Types; Integers

-byte (8 bits) -short (16 bits) -int (32 bits) -long (64 bits)

Primitive Data Types; Characters

-char

Primitive Data Types; Floating Point Numbers

-float (32 bits) -double (64 bits)

Error Types; Compile Time Errors

1. Compile time errors are error that occur during compile time A. Syntax error - an error from not using the grammar rules of the programming language B. Lexical error - an error from misspelling a variable or having the wrong case (upper or lower) of letters.

Compiling

1. Compilers translate high level code, like Java, to lower level code which is closer to machine language 2. The Java Compiler translates Java code to byte code which is run by the Java Virtual Machine

Machine Code/Machine Language

1. Machine code/Machine Language consists of an instruction set in binary code 2. Each CPU family has its own instruction set 3. Nobody programs in this (except for some computer architecture students)

Java Code; Program Structure

1. Program Structure - A program contains any number of classes - Classes contain any number of methods and variables - Methods contain some statements or instructions for the computer

Printing to the monitor/standard output

1. System.out.print("Sample Text") A. Prints a string of text to the monitor and the cursor is located at the end of the text Sample Text| 2. System.out.println("Sample Text") A. Prints a string of text to the monitor and the cursor is located at the beginning of the next line Sample Text |

Program Process; Java

1. The language we will use in this class is Java - An Object-Oriented programming language developed by Sun Micro-systems in 1995 It runs on its virtual machine which makes very portable. Gained in popularity because it is used on Android devices

Java Code; Reserved Words

2. Reserved words - The highlighted words are reserved words or key words - Reserved words have a special meaning in a programming language. They may only be used for their intended purpose. - Some reserved words in Java; abstract, else, extends, false, break, byte, catch, const, class..

Error Types; Run Time Errors

2. Run time errors are errors that occur during run time A. Exceptions - when Java cannot run a piece of code for some reason it will throw an exception. - input/output exception - cannot find, read or open a file - divide by zero - type mismatch

Java Code; Identifiers

3. Identifiers A. Used to identify variables, methods and classes B. Rules for Identifiers 1. Made up of letters, digits, underscore '_' and the dollar sign '$'. 2. May not begin with a digit 3. They are case sensitive count is not the same as Count which is not the same as COUNT. 4. By convention class names start with an upper case letter Person 5. By convention variable and method names start with a lower case letter - index - getValue() 6. By convention constants are all upper case letters code; (final double FEET_IN_A_METER = 3.28084;) C. Which of these identifiers are valid? -class -CLASS -1time -abc123 -bank-account -bank_account -$hello

Error Types; Logic Time Errors

3. Logic errors are errors that occur as a result of faulty design of code

Java Code; Writing a program

4. Writing a program A. Edit - Write the source code in a text editor. We will be using Atom in this class. - Save it a .java file - The file name must match the class name. B. Compile - The javac compiler takes the source code and translates it into byte code - At the command prompt type in javac programName.java - If there aren't any problems, usually there some, it will produce a .class file. C. Execute - The Java Virtual Machine interprets the byte code in the .class file to execute the program - At the command prompt type in java programName - The program will run if it doesn't find any exceptions

Program Process; Algorithm

A step by step process to solve a particular problem recipe A process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.(wikipedia) 1. Example - Automobile CO2 Calculator - Ask the user for miles per gallon - Ask the user for the number of miles driven - Calculate the gallons of fuel used - Calculate pounds of CO2 used (gallons of fuel used times the emissions factor of 19.6) -Display gallons of fuel and CO2 used

Variables; Declarations

A. Reserves space in memory for a value to be stored. B. Assigns a value if it is assigned a value. total = 10 C. Is assigned a null value if it is not assigned a value.

Variables; Choose variable names that are descriptive

A. They are easier to remember. B. If you need an identifier with multiple words: tryCamelCase or use_underscores C. AVOID x! 'what the hell is x anyway?'

Practice; Algorithm

Algorithm - assign the number of short flights taken (numShort) - assign the number of medium flights (numMedium) - calculate total carbon emissions (totalEmissions) numShort * shortDist * shortEmission + numMedium * medDist * medEmission display total carbon emissions

Arithmetic Expressions;

An expression is a combination of one or more operands and their operators

Variables; Assignments

Assignments A. A variable may only hold one value at a time. int count = 1; count = count + 1; B. The variable on the left of the assignment operator is assigned a value - if it is already holding a value, then it is overwritten. C. The expression on the right of the assignment operator is evaluated and assigned to the variable on the left D. Value assigned to a variable must be of the same type as the variable

Primitive Data Types; Boolean

Boolean A. A boolean value represents a true or false condition B. A boolean also can be used to represent any two states, such as a light bulb being on or off C. The reserved words true and false are the only valid values for a boolean type boolean success = true;

Primitive Data Types; Characters

Characters A. A char variable stores a single character from the Unicode character set. B. A character set is an ordered list of characters, and each character corresponds to a unique number C. The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters D. It is an international character set, containing symbols and characters from many world languages E. Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n'

Variables; Constants

Constants A. Declaration final in MIN_HEIGHT = 69; B. The use of the keyword final makes it so the value cannot be changed. MIN_HEIGHT = 72; C. Purpose - give names to otherwise unclear literal values - facilitate updates of values used throughout a program -prevent inadvertent attempts to change a value

Two parts of CPU

Control Unit: Coordinates all the computer's operations Determines where to get the next instruction Regulates the other major components of the computer with control signals Arithmetic and Logic Unit (ALU) Performs the mathematical Operations

Variables; Example of Assignment

Example public class Geometry { public static void main(String[] args) { int sides = 7; // declaration with instantiation System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; // assignment statement System.out.println ("A dodecagon has " + sides + " sides."); } }

Variables; Example Code

Example Code 1 public class CO2Calculator { 2 public static void main(String[] args) { 3 int milesDriven = 360; 4 double mpg = 24.5; 5 double gallonsUsed, co2Used; 6 double emissionsFactor = 19.6; 7 8 // calculate gallons of fuel used 9 gallonsUsed = milesDriven/mpg; 10 11 // calculate and display pounds of C02 emitted 12 co2Used = gallonsUsed * emissionsFactor; 13 System.out.println("You used " + co2Used + " pounds of C02"); 14 } 15 } A. Lines 3 through 6 contain variable declarations

Arithmetic Operations; 3.

If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 12 / 3 = 4 10 / 3 = 3 5 / 6 = 0

Arithmetic Operations; 2.

If either or both operands associated with an arithmetic operator are floating point, the result is a floating point

Program Process; Java Code

Java Code public class CO2Calculator { public static void main(String[] args) { int milesDriven = 360; double mpg = 24.5; double gallonsUsed, co2Used; double emissionsFactor = 19.6; // calculate gallons of fuel used gallonsUsed = milesDriven/mpg; // calculate and display pounds of C02 emitted co2Used = gallonsUsed * emissionsFactor; System.out.println("You used " + co2Used + " pounds of C02"); } }

Arithmetic Operations; 6.

Operator Precedence A. The contents of Parenthesis must be resolved prior to the operator outside the parenthesis B. Unary - : right to left C. * / % : left to right D. + - : left to right Practice a - b + c - d a + b * c / d a * b / c * d a - (b - ( c - d) - e) a + (b - c) * d - e (a + b * c) + (d + e) % f

Arithmetic Expressions; Operators

Operators + Addition - Subtraction * Multiplication / Division % Modulus - Remainder

Arithmetic Operations; 5.

Practice 12 / 5 6 / 12 5 / 2 5 / 2.0 11 % 3 8 % 4 29 % 5 3 % 7

File Structure; A tree structure

Roots or dirves are identified with a letter C: D: X:

Computer Structure; CPU

The heart of a computer Fetches instructions Follows instructions Produces some resulting data Consists of two parts;

Arithmetic Operations; 4.

The modulus or remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3 = 2 8 % 12 = 8

Branches or folders are identified by their name

The path of a folder back to its root uses a backslash to identify each folder and drive in which it is located C:\users\ X:\cs144\labs\

Java Code; Variables

Variables A. Used to store data

Practice;

Writing a program to calculate your carbon footprint with air travel


Ensembles d'études connexes

Gene regulation in bacteria 2 - operons

View Set

U1 UST Installation/Retrofitting

View Set

Chapter 35: The Adolescent and Family

View Set

Sociology Unit 3: Social Structure & Stratification (old)

View Set