Studydroid is shutting down on January 1st, 2019

by Weston

Bookmark and Share

Front Back
Programming-code is stringent
Means that it must be perfect in terms of spelling and grammar.
Sequential-execution programs
Are programs in which all the program's statements are executed in the order in which they are written.
2 types of Comments
One-line-comment Syntax:
//Line of code that follows is //not read by the computer
//each subsequent line needs
//to have them.

Block-Comment Syntax
/*The computer ignores anything written after the "/*" sign and it will remain to be ignored until it read the closing sign "*/" */
It's a special example of a block comment.  It is at the top of every program.
Include these items in your program's prologue section:

  • a line of *'s
  • Filename
  • programmer's name
  • a line with a single * at its left
  • program description
  • a line of */s
Program Readability
A program is readable if a programmer can easily understand what the program does.
Class heading
public class Dream

it's the heading for the definition of the program's class. What's a class? right now think of a class simply as a container for your program's code.

public class Dream

Dream: dream is the name of the class.  The computer will allow you to use any name but you should make it words that describe the program.
public class Dream
The words public and  class are reserved words/keywords.

class: the word class is a marker that signifies the beginning of the class.  With our simple one-class programs, the word class also signifies the beginning of the program.
reserved words/ keywords
reserved words or keywords are words that are defined by the Java language for a particular purpose.
public clase Dream
Public in this statement is and access modifier--it modifies the cleass's permissions so that the class is accessible by the "public" .  This is crucial so that the user will be able to run it.
The Java compiler distinguishes between lowercase and uppercase letters.  So therefor it is case-sensitive.
The main Methods's Heading
The heading that goes below the class heading.  Computer looks for the main method heading and then execution begins with the first statement after the main method heading.  The main meathod heading must have this form:

public static void main (String[] args) /*But it should only be on one line*/
System.out.println ("");
System refers to the computer system.
System.out refers to the output part of the computer system--the computer's moniter---.
The word println refers to the Java println method that's in charge of printing a message to the computer screen.

System.out.println ("") is normally referred to as a println method call.
What is a call?
You call a method when you want to execute it.
split a string literal
To split a string literal, you need to put opening and closing quotes around each of the two split-apart substrings, and you need to insert a + sign in between the substrings.
The + connects/concatenates the split-apart strings.

System.out.println ("Your face" +
"is covered with cheese!");
Class name and file-name
The programs must have the same class name and file-name. ex:  Dream was the class name so it therefore must be the program name.
What does a compiler generate?
The compliler generates a bytecode programefile whose name is comprised of the class name plus a .class extension. ex: Dream.class
An Identifier is the technical term for a program componet's name--te name of a class, the name of a method, etc.
When should you not use lowercase for your identifiers?
Start class names with an uppercase letter

Run together the words in a multiple-word identifier, printFavoriteColor.
What is a Java variablle?
A place to store values and variables.  A Java variable can hold only one type of value. Either an integer variable can hold only integers or a string variable that can hold only strings in it.
Declaration Statement syntax
<type> <list-of-variables-separated-by--comcommas>;

  • String firstName;
  • Int row, col;
  • int studentId

  1. Don't forget to put a semi-colon at the end of each declaration statement
  2. When you declare more than one variable whith on declaration statement, don't forget to separate the variables with commas.
Declaration statement
Specifies a data type for a particular variable.  pg 69
Assignment statement
pg. 69   An assignment statement puts a value into a variable
initialization  statement
An initialization statement is a combination of the declaration and assignment statement--it specifies a data type for a variable, and it puts a value into that variable.

Syntax of an intiialization statement:
<type> <variable> = <value>;
String name - "John Doe";
int creditHours = 0;
int vs. long
int: uses 32 bits of memory.  it can hold variable from approximately -2 billion to +2 billion.

long: uses 64 bits of memory.  I can hold value from -9 X 10^18 up to +9X 10^18.

Floating-point numbers
In Java, numbers that contain a decimal point (e.g., 66. and -1234.5) are called floating-point numbers.  Why? Because a floating-point number  an be written with different forms by shifting (floating) its decimal point.  Th number 1234.5 can also be written 1.2345 X 10 ^3.
float vs. double
Use double more frequently even though it is 64 bits and float is 32 bits.  double can have 15 significant digits while float can only have 6 witch is rarely sufficient.
Numeric and string values are called constants.  They're called constants because their values are fixed--they do not change.
final double SPEED_OF_LIGHT = 299792458.0

This statement is different from the ones previously taught, it has final before the double.  What does final mean/do?
the reserved word final is a modifier--it modifies SPEED_OF_LIGHT so that its value is fixed or "final".
++  and --
Increment and Decrement operators

++: This special operator counts up by one.
--:  This special decrement operator counts down by one.

x+1 = x++
x-1 = x--
+= operator
The += operator updates a variable by adding a specified value to the variable.   EX:
Here's one way to increment by 3:
x=x+3;   or use the +=
x +=3;
-= operator
-= operator updates a variable by subtracting a specified value from the variable.
x = x-3;  or with the -= operator

The second form is how experienced programmers do it.
*=, /=, %= operators
Parellel to the += and -= operators.
x *=4  = x=x*4
The cast operator
in writing a program, you'll sometimes need to convert a value to a different data type.  The cast operator can be used to perform that sort of conversion.

Cast Operator syntax:
<type> <value>
interestInDollars = (int) interest;
before interest was a float.

* this does not change the value of interest because you are actually assigning the new number to a new variable.
Expression with Cast operator
Must use parentheses...

double interestRate;
double balance;
int interestInDollars;

interestInDollars = (int) (balance * interestRate);
How can you get back a number with a decimal point when you are doing division with two int?

(the problem is that they will only return the quotiant with division or the remainder with the percentage thing)
gpa = earnedPoints / numOfClasses;

needs to come back in a decimal form.

so we need to change the earnedPoints and numOfClasses into a double or a float.

gpa= (double) earnedPoints / numOfClasses;

Ta Da!
double quote control character
tells the computer that the subsequent characters are part of a string literal
Single quote control character
tells the computer that the subsequent character is a char literal
backslash control character "\"
tells the computer that the next character is to be interpreted as an escape sequence character.
How would you print this
""  (including the quotations)
System.out.println ('\"' + "" + '\"');

Or you could do this..(much easier)

System.out.println ("\"\"");

What do these do? \t
\t: move the cursor to the next tab stop

\n:  newline---go to first column in next line

\r return to first column in current line
Primitive variable
stores a single piece of data. EX:
int, long float, double, char
Reference variable
a reference variable stores a memory location that points to a collection of data.  This memory location is not a literal memory address, it's a coded abbreviation.

In Java the value in a reference variable acts exactly like a literal memory address, so you should pretend that it is one.
realize that an object is a collection of related data wrapped in a protective shell.  To access an object's data, you need to use a reference variable (or reference for short) that points to the object.
Reference type
A reference type is a type that provides for the storage of a collection of data.
charAt method
A string object stores a collection of characters, and a string object's characters can be accessed only through its charAt method.
4 popular string methods
  1. charAt method
  2. length
  3. equals
  4. equalsIgnoreCase
x of y cards Next >|