Lecture Notes, class 1 for csc211.
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
Basic Terms.
- Computer Program - A sequence of instructions that a computer can interpret and execute.
- Computer Programming Language - A collection of words and symbols, together with rules describing how the words and symbols can be combined to form a program. When writing a computer program you need to obey the grammar of that language.
- Processor A unit that interprets and executes instructions.The processor will execute programs using its own instruction set. An instruction specifies one operation and any operands that it uses.
- Machine Language - Each type of processor has its own machine language that it can understand. Machine language is expressed as a series of binary digits representing instructions used directly by processor.
Example...
011111 110110 111100 111101.
Programs written in Machine Language for an Intel processor will not execute on a Sun Workstation that uses a Sparc processor.
Example..
ADD AX,BX,CX
Example..
totalPay = basePay + overTimePay.
In order to run a program written in a high level language we need a way to convert it into Machine Language.
Our First Program.
class Welcome{ public static void main( String [] args ){ System.out.println("Welcome to csc211"); System.out.println("Good Luck and have a great quarter"); } }
Getting the program to run.
If you will be working from home you will need to install some software.
- The Java Developement Kit.
- TextPad (An editor)
Note getting the software to work can be a pain at first, so be patient. Note the software is available and works in the depaul labs so you will always have a place to practice and work on your homework.
See resource section of the course homepage
Basic Features.
- Java programs are built from classes.
Everything in Java lives inside a class definition.
For this week we will use this prototype;
class XXX{ public static void main(String [] args){ : : Your code : : } } Where XXX is the name you choose to give your class. This name is called an identifier. See rules for Identifiers below.
- The Welcome program must be saved to a file named
Welcome.java
- Java is case-sensitive.
class Class clAss etc.. are all different.
- The line containing the word main marks the beginning of a Java application. For now ignore the words public, static and void. These will be explained later. Just make sure they are there. main is called a method.
- Braces are used to mark the beginning... { and ending }for code that is related. All class definitions and method definitions must be enclosed in { }
- Semicolon ; means end of statement.
- Java is a free-form language.
The Java compiler uses white spaces to separate tokens, hence we can say the compiler ignores them.
A white space is created using a tab space, blank and/ or new line. Welcome could of been written... (Don't do this)
class Welcome{ public static void main( String args[] ) : : }
- Comments
- // Single Line comment   Everything after the double slash to the end of line is ignored by the compiler.
- /*     */ Block comments   Everything between is ignored by the compiler.
- /** */ Javadoc comment We will use this later.
/* ignored by the compiler */
Calculator Program
Write a program that gets two numbers from the user and displays to the console their sum, difference, product and ratio.
Since getting input is not as straight forward as producing output,lets assume the user entered the numbers 17 3.
See Calculator1.java
Some Terms
- Data type- Specifies a group of values and the operations that can be performed on those values. In Java, every
piece of data is classified according to its type. Their are two main categories of data in Java.
- Primitive data
- Objects
- Primitive data- Simple values like numbers and characters. Java has eight primitive data types.
- byte ( 1 byte )
- short ( 2 bytes )
- int ( 4 bytes )
- long ( 8 bytes )
- float ( 4 bytes )
- double ( 8 bytes )
- char ( 2 bytes)
- boolean (uses 1 bit of 32 )
- Object - An object is an entity that encapsulates one or more data items (variables) and methods
Think of an object as an abstraction of an entity or concept, such as a bank account, a point in the plane, a car, StringTokenizing, DecimalFormatting, temperature etc.. An object has state or attributes (data), behavior (methods).
An object is an instance of a class.
The data type of an Object is some class. - Variable - The name our program uses to refer to a memory location that stores a value. A variable is used to store primitive data or a value used to locate an object. Each variable has a type and a size. All variables must be declared before they are used.
int age; double salary; String name; DecimalFormat round2; When the type of variable is some class, we call that variable a reference variable.String and DecimalFormat are Java classes that come with the Java API.
- Assignment - The process of giving a variable a value. To give a value to a variable we use the assignment operator =.
age = 65; salary = 30000.00; name = "Joe Pass"; round2 = new DecimalFormat("0.00"); Note: We are not saying age is equal to 65, we are giving age the value 65.
- Identifiers The words used when writing a program. Identifiers fall into three categories.
- Words that we make up ... Welcome, radius, area etc...
- Words that have special meaning in the language. These are called reserved words or key words.
- Words that other programmers make up. String, System, Math , main etc..
class, int, public etc ...
Reserved wordsWhen creating an identifier the word can be made up from any combination of letters, digits, the underscore _ and dollar $ characters. Except the first letter cannot be a digit.
- Method - A named group of statements that perform a task. A method is always a member of a class. Two types of Methods.
- Static (or class) methods.
- Instance methods.
The type of method dictates how we can call it. Recall everything in Java must live inside a class definition. In addition to the main method, a class can contain the definition of other methods. If the method is static we can use the name of the class to access the method. If the method is an instance method we have to use a reference variable. (Reference variables refer to Objects).
Example: The Math class has a static method named
sqrt
that can be used to take the square root of a number. Since this method is static we can use the name of the class and the dot operator '.' to access this method.double result = Math.sqrt(16); Each class that is part of the Java API has a documentation file describing its public members.
Look for method sqrt, notice the word static before the name.
Example: The String class has an instance method named
toLowerCase
that will return a new String with the characters converted to lower case.name = name.toLowerCase(); Look for method toLowerCase, notice NO keyword static before it.
Arithmetic Operators
+     Addition
-     Subtraction
*     Multiplication
/     Division
%   Modulus
Integer division occurs if both operands are integer literals or variables of integer type.
Floating point division occurs if at least one of the operands is a floating point literal or variables of floating point type.
EXAMPLES.
- 5 / 3 = 1
- 7 / 2 = 3
- 5 / 9 = 0
- 7.0 / 2 = 3.5
- 4 % 4 = 0
- 5 % 4 = 1
- 6 % 4 = 2
- 7 % 4 = 3
- 8 % 4 = 0
- 9 % 4 = 1
- Evaluate the operators within parentheses first.
If parentheses are nested, evaluate the operators
in the inner most pair first.
- Next do, multiplication, division and modulus in the order they appear from left to right.
- Then do, Addition and subtraction from left to right.
EXAMPLES
- 6 / 2 * 4 = 12
- 6 / ( 2 * 4 ) = 0
- 2 * 4 * 3 + 5 * 2 + 6 = 40
- 6 - 2 + 8 = 12
- 25 % 6 + 1 = 2
- 34 % 6 + 1 = 5
- 55 % 6 + 1 = 2
One way to get input into our programs
Programming in Java involves writing classes, defining methods, using primitive data, using objects and using predefined classes that contain code already written for you to perform various tasks. When using a class you need to know the services( methods) that the class provides and the package that it resides in. A package is a group of related classes.
One way to get input from the user is to use a class named...JOptionPane
JOptionPane lives in package... javax.swing
To use this class you must import it. Note: Classes that are part of java.lang
do not need to be imported. They are automatically accessible.(System,String,Math etc...)
See InputDialog.java and Calculator2.java