Chapter 5: Data Types, Variables, and Arithmetic

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the general syntax for the cast operator?

(sometype) variable (sometype) (expression)

What type of variable data types are there? Hint: two types

- Primitive Data types (int, double, boolean) - Class Data types (String, Color)

What does a variable's data type determine ? Hint: VSO acronym

- Range of values that can be stored in the variable (V) - the amount of space allocated for it in memory (S) - the kind of operations that can be performed with it. (O)

What is the range of values for an int?

-2^31 --> 2 ^31 -1

System.out.println("1 + 2 = " + 1 + 2);

1 + 2 = 12

System.out.println( "1 + 2 = " + (1+2));

1 + 2 = 3

What do you do if you want to round a double value to the nearest integer?

1. Add 0.5 to a positive number (or subtract 0.5 from a negative number) 2. Cast result into an int int miles = (int)(km * 1.61 + 0.5);

Why use symbolic constants?

1. Easier to change the value throughout the program 2. Additional data type checking by compiler 3. Easy to change into a variable

Names of Variables are usually: 1. Lowercase or Uppercase? 2. Begin with digit?

1. Usually lowercase 2. Never begins with digit

int a = 7, b = 2; double ratio = (double) (a / b);

3.0

int a = 7, b = 2; double ratio = a / b; What is ratio?

3.0

int a = 7, b = 2; double ratio = (double) a / b;

3.5

System.out.println( 1 + 2 + "1 + 2 = ")

31 + 2 =

What is a constant?

A "variable" whose value does not change while the program is running

What is a variable?

A named "container" that holds a value

What does final mean?

A value once assigned, cannot change ( it's a constant)

How can an object be converted into a String?

Call the toString method

What's the easiest way to convert x into a string?

Concatenate x with an empty string String s = "" + x;

T/F: String is a primitive data type.

False, String is an object.

T/F: Scope is a run-time concept.

False. Scope is a compile-time concept

Where can a field's value be initialized?

In the field declaration itself, in a constructor, or in a method

What are some examples of Math methods?

Math.abs(x) Math.sqrt(x) Math.max(x,y)

Before it can be used, what must happen to a local variable?

Must be declared and assigned a value before it can be used. Local variables don't get default values.

Can local variables be declared with public or private?

Never

Can a variable be declared more than once in its scope?

No, it is a syntax error.

Is this acceptable? int sum; int sum = m + n;

No, it should be: int sum; sum = m + n; OR int sum = m + n;

Does a constant have to be initialized right away in its declaration?

No, it's "final" value can be set in a constructor.

Where are fields usually declared?

Outside of any constructor or method, usually at the top of the class's body

How would you tell the compiler to print "Hello World!" with a string literal?

String str = "Hello World!";

If I used to toString method, what does System.out.print(obj); equivalent to?

System.out.print(obj.toString());

What is reference?

The address of the object in RAM when the program is running

What is the scope?

The area in the source code where a variable is visible.

What happens if we accidentally write: Color color = Color.BLUE; instead of color = color.BLUE;

The compiler will treat this statement as a declaration of a local variable color and leave the field color uninitialized (null). If one of color's methods is called, there will be a NullPointerException.

What is a string literal?

They are string objects that are strings of characters in double quotes.

T/F The + operator can be applied to strings.

True. String str = "Chapter" + "5"; is the same as String str = "Chapter5";

When is a cast to double used?

When the operands are ints but we want the correct result as a double

When is a cast to int used?

When you want to truncate all values after a decimal

Are these legal declarations? double x, y, z; newline = '\n', char tab = '\t', a = 'a';

Yes they are

Is it okay to give the same name to variables in different methods?

Yes, and it is actually desirable if the variables have similar roles.

What is the general form of a symbolic constant's declaration?

[optional modifiers] final sometype someName = expression;

Where is a reference stored?

a reference variable when a new object is created

Class type variables are the _____ of an object.

class

When the + operator is used for _________ , at least _______ of its two operands must be a value or an expression of the _________ type

concatenation one String

Because of the possible overlap of names between fields and local variables, what type of names should you give to class fields? Examples?

conspicuous names like: radius, sideLength NOT x, y, or r

What is a literal constant and give examples of the four types.

decimal representations of integers and real numbers -ints like 7 and 3 -doubles like 1.05 and 2.2 Characters in single quotes -char like 'y' and 'H' characters in single quotes, and text in double quotes. -strings like "Hello World"

What is the local variable in this distance method? public double distance (intx, int y) { double dx = Math.abs( x - getX()); double dy = Math.abs( y - getY()); return Math.sqrt( dx*dx + dy*dy); }

dx & dy

Java allows you to use the same name for a ______ and a ____________. This might cause bugs that are hard to find.

field, local variable

What three categories do variables fall into?

field, local variable, and parameters (in the constructor or method)

String fileName = "flower"; fileName += ".gif"; What is fileName equal to?

flower.gif

The scope of a local variable is ________

from its declaration to the closing brace of the block it was declared

What are symbolic constants usually represented by?

initialized final fields private static final double LBS_IN_KG = 2.20462

What two types of constants are there?

literal constant symbolic constant

What happens to the local variables when a method is finished?

local variables are discarded

If x is a double, Math.round(x) returns a _____ type value

long

If a field is not initialized, what is the default value for a field of 1. numeric types? 2. boolean? 3. objects?

numeric types: 0 boolean: false objects: null Ex: private int xCenter; //xCenter is set to 0

What happens when two operands have different types? (an int and double for example)

operand of "smaller" type is promoted to "larger type"

What is the general format for a field declaration?

private sometype someName; private sometype someName = expression;

What are some examples of unnecessary cluttered code with symbolic constants?

public static final int FULL_CIRCLE = 360; public final int ONE = 1;

What is the general format for a local variable declaration?

sometype someName; sometype someName = expression;

What are local variables?

temporary variables that are declared inside a constructor or method

What happens when you use a variable outside its scope?

the compiler reports a syntax error

What happens when the scope of two variables overlap?

the variable defined in the inner scope takes precedence over the variable with the same name in the outer scope

The scope of the field is __________

the whole class


Set pelajaran terkait

algebra 2b unit 7 lesson 2: angles and the unit circle

View Set

Molecular Biology - Chapter 12 Homework

View Set

Root Word: "leg / lect", sed, sid, sess, ono, nymn, onym

View Set

Ancient Greece: Delian League and Athenian Empire

View Set

ENZIMAS Y MECANISMO DE REACCIÓN I

View Set

Module 19 Reconstruction (questions)

View Set

LGS 200 Quiz 17 Antitrust & Regulation of Competition

View Set