CS 149 Test 1

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Multiple Methods vs One Method

- Creating a new method allows you to name a block of statements, which makes the code easier to read and understand - Introducing methods can make the program shorter by eliminating repetitive code - Common problem-solving technique is to break problems down into sub-problems. Methods allow you to focus on each sub-problem in isolation, and then compose them into a complete solution. - Organizing your code into multiple methods allows you to text individual parts of your program separately

Assignment Statement

A statement that uses variables to store values. When you assign a variable, you update it's value. The assignment operator is sued to store a value in memory. Ex) message = "Hello!"; or hour = 11;

Computer

Any type of device that stores and processes data. Two most important hardware components are processors and memory.

Initialize

Assigning variables for the first time, must happen before a variable is used

Parameter Passing

Before the method executes, the argument gets assigned to the parameter. Parameters and other variables only exist inside their own methods.

Programming

Breaking down complex tasks into smaller subtasks. Sequence of instructions that specifies how to perform a computation on computer hardware.

Processors

CPUs, perform simple calculations

Pros of Java Methods

Can be composed to solve complex problems. Can use one method as a part of another. Can use expressions as an argument to a method, as long as the result value has the correct type.

Floating-point numbers

Can represent fractions as well as integers. called a double. You should always assign floating-point values to floating-point variables. Main problem is that rounding errors result in incorrect results.

Decisions

Check for certain conditions and execute the appropriate code

Automated Style Checker

Check the indenting/spacing of your code, warm you if you forgot to write comments

Find the class, comment, method, and statement in the following lines of code. 1 public class Hello { 2 public static void main(String[] args) { 3 //generate some simple output 4 System.out.println("Hello, World!"); 5 } 6 }

Class - line 1 Comment - line 3 Method - line 2 Statement - Line 4

Find the name of the class and the method(s) it contains. public class NewLine { public static void newLine() { System.out.println(); } public static void main(String[] args); System.out.println("First line."); newLine(); System.out.println("Second line."); } }

Class name - NewLine Methods - newLine (public and void) and main (public and void)

Scanner class

Class that provides methods for inputting words, numbers, and other data. Provided by java.until, package that contains various "utility cases."

System class

Class the provides methods related to the "system" or environment where programs run

Class

Collection of methods, conventional to start it with a capital letter. The name of the class has to match the name of the file it is in. You can find multiple classes in one package. Each class defines it's own methods. Ex) If the class name is DukeDog, then the source file name is DukeDog.java

Package

Collection of related classes. Java applications are organized into packages that include multiple classes. Ex) java.io contains classes for "I/O", or java.util

Return Methods

Compared to void methods, value-returning method differ in two ways: - They declare the type of return value (the return type) - They use at least one return statement to provide a return value

Math.toRadians

Converts degrees to radians

Math.toDegrees

Converts radians to degrees

%d

Decimal integer (12345)

Declaration Statement

Declares that a specific variable has a type. Ex). double trouble; or String message;

Type

Determines what kind of value a variable can store. Ex) int or char

Rounding error

Difference between number we want and floating-point number we get. Ex. .1+.1+.1+.1+1+.1+.1+.1+.1+.1 = .999999999999 no 10

Output

Display data on the screen, or send data to a file or other device

Run-Time Errors

Do not appear until after the program has started running. Occur while the interpreter is executing byte code and something goes wrong. Ex) dividing by a 0

What does the following code output? System.out.print("Do you \"like\" my hat? _/\\_");

Do you "like" my hat? _/\_

printf

Does not append a new line, uses command to separate each value

Byte Code

Easy and fast to interpret. You type "java DukeDog" to interpret the byte code in DukeDog.class

Comment

English text that explains the code, line begins with two slashes (//), comments have no effect on the execution of the program

Compile-Time Errors (Syntax Errors)

Errors occur when you violate the rules of the Java language. Error messages from compiler usually indicate where in the program the error occurred, and sometimes exactly where it was. Sometimes the compiler reports where the error was detected, not where it actually occurred. Ex) Forgetting a semicolon, or string s; instead of String s;

Object code

Executable, low-level program that high-level program is translated into.

Argument

Expression in parenthesis

Java Library

Extensive collection of classes that you can use in your programs.

%f

Floating-point (6.789000)

Input

Get data from the keyboard, a file, a sensor, or some other device

Calling a Method

In order to call another method in the same class, it does not have to be declared before (above) the method that calls it. When calling a method, the arguments are the values passed to the method.

Javac

Java compiler generates byte code. Translates .java files into .class files that store resulting byte code. It is possible to compile a Java program on one machine, transfer the byte code to another machine, and run the byte code on the other machine.

Division

Java performs integer division when the operands are integers which always rounds towards 0.

Concatentation

Joining end-to-end, the + operator performs this with strings

Void Methods

Keyword that indicates method doe into return a result.

Statement

Line of code that performs a basic action. Series of statements make up a method. Performs one or more computations, depending on how many expressions it has. Ex) System.out.println("Hello, World!"); or hour = in.nextInt();

Low-Level Language

Machine language. Can execute hardware-specific instructions, can only run on one kind of computer.

Value

May be numbers, text, images, sound, and other types of data

Public Methods

Means they can be invoked (or called) from other classes.

Stack Diagram

Memory diagram that shows currently running methods. For each method, there is a box called a frame that contains the method's parameters and local variables. The methods that are executed first are on the top.

Type cast

Molds or "casts" a value from one type to another. Java converts an int to a double automatically, since no information is lost in the process Ex. (int)

Token

Most basic element of a program, including numbers, variable names, operators, keywords, parenthesis, braces, and semicolons

newline

Moves to the beginning of the next line, adds a new line AFTER the line being printed. System.out.println(); or \n

Declaring a variable

Must be done before you store a value. When you declare a variable, you create a named storage location.

Variable

Named location in memory that stores a value. Names usually begin with a lowercase letter, the second word may be capitalized. You cannot use a key word as a variable name. A variable's scope ends at the closing brace of the code block in which it is declared. Ex) firstName

Method

Named sequence of statements. Defined in a class. Should being with a lower case and use camel case (jammingWordsTogetherLikeThis). Changing the order that methods are declared in a source file has no effect on the run-time behavior of the program. Ex) main method in hello world public static void main(Strings[] args)

High Level Languages

Need to be translated before they can run. Easier to read, write and understand. Portable. Ex). Source code, java

How do you extract the right most digit?

Number % 10

Flow of execution

Order program actually runs. -Programs always begin at the first statement of main, regardless of where it is in the source file - Statements are executed one at time, in order, until you reach a method invocation

%08d

Padded with 0s, at least 8 digits wide (00012345)

Repetition

Perform some action repeatedly, usually with some variation

Strings

Phrases that appear in quotation marks, contain a sequence of characters strung together in memory

Parsing

Process of reading a program before translating it, if the compiler test to the end of the file while still parsing, that means something was omitted. "Reached end of file while parsing" most likely means your code is missing a brace

Javadoc

Processes /** comments.

Logic Error

Program will compile and run without generating error messages, but it will not do the right thing. Ex. Using System.out.println when you should have used System.out.print

Bugs/Debugging

Programming errors and the process of tracking them down and correcting them

Math class

Provides common mathematical operations. Is a java.lang package, so you don't have to import it. Ex) double root = Math.sqrt(17.0); double angle = 1.5; double height = Math.sin(angle);

Memory

RAM, temporarily stores information

Interpreter

Reads a high-level program and executes it. Processes a little at a time, alternately reading lines and performing computations.

Compiler

Reads the entire high-level program and translates it completely before the program starts running. Once a program is compiled, you can execute it repeatedly without further translation. Often run faster than interpreted programs.

< and >

Redirect input and output

Address

Represented as a hexadecimal, is its location in the computer's memory. Storage location of a variable or object memory.

Expression

Represents a single value to be computed. Generally, a combination of numbers, variables, and operators. When compiled and executed they become a single value. Found in statements. Ex) hour * 60

Math.sin(#);

Returns sin (cos, or tan) of number. Values must be in radians (not degrees).

Math.sqrt(#);

Returns square root of number

%.2f

Rounded to 2 decimals (6.79)

nextLine in scanner

Scanner in = new Scanner(System.in); System.out.print("Type something: "); line = in.nextLine();

Computer Science

Science of algorithms

Algorithms

Sequence of steps that specifies how to solve a problem

What does the following code output? System.out.println("She said \"Hello!\" to me.");

She said "Hello!" to me.

Format Specifier

Special sequence that starts with a percept sign Ex) %d, %08d, %f, %.2f, %s

System.out

Special value that has additional methods (like println) for displaying output, PrintStream. Belongs to java.lang packages, which is imported automatically (so we don't have to import it)

Format String

Specifies how the output should be displayed. Contains ordinary text followed by former specified, everything in the quotations below. Ex) System.out.printf("Four thirds = %.3f", 4.0 / 3.0);

Incremental Development

Start with a working program and make small, incremental changes. At any point, if there is an error, you will know where to look. Use variables to hold intermediate values so you can check them, either with print statements, or by using a debugger. Once the program is working, you can consolidate multiple statements into compound. If you're doing incremental development, all the code you write will not be part of the final solution. When writing new methods, it is helpful to think about the test case first.

%s

String of characters ("Hello")

Arithmetic Operators

Symbols that represent simple computations. Ex) +, -, /, *

Import Statement

Tells the compiler that when you refer to the Scanner (or an inputed class) you mean the one defined in java.util. Usually at beginning of file. If you forget to import it, you will get an error stating "cannot find symbol"

Portable

They can run on different kinds of computers with few or no modifications.

Escape Sequence

Two characters of source code that represent a single character. Do not add a space between the escape sequence and the following words. Ex) \n, \t, \", \\

Temporary Variables

Useful for debugging but unnecessary in the final version of the code

Literal

Value that appears in a program. Best practice is to avoid using literals (magic numbers) and instead assign them to variables with meaningful names. Ex) 123, "home"

Operands

Values that the operators work with. Can be a variable, literal, or expression.

Reference

Variable that refers to something else and can be used as an alias for something else. Anything that is not one of the primitive types is a reference. Ex) Scanner and String

Constant

Variables that are declared as final (keyword for constant). Means that they cannot be reassigned once it has been initialized. Names are usually all uppercase with underscores separating words. Ex) final int CM_TO_INCH = 3;

Parameters

Variables that indicate what arguments are required in a method

Order of operations

When more than one operator appears in an expression, they are evaluated in this order... 1. Multiplication and division take precedence over addition and subtraction 2. If operators have same precedence, they are evaluated from right to left 3. Any time you want to override the order of operations, you can use parenthesis

Method Invocation

Where you jump to the first line of the invoked method, execute all the statements there, and then come back and pick up exactly where you left off

Double Quote

\"

Backslash

\\

Tab

\t

How do you import scanner class?

import java.util.Scanner;

JDK

java development kit

JRE

java runtime environment

Math.round

rounds a floating-point value to the nearest integer and returns a long. Ex) would round 62.83098 to 63


संबंधित स्टडी सेट्स

Med-Surg Ch. 33-35 Lippincott Questions

View Set

Food and Human Environment Final

View Set

Environmental Science 1127 Test 4

View Set

Google Apps, Plug-ins and Extensions

View Set