Java Basics
Java Basics
GEEN163
Introduction to Computer Programming
What's in a name? That which we call a rose by any other name would smell as sweet.
William Shakespeare
Clickers
Clickers will be required on Friday, January 17
Textbook
Java Illuminated, 3rd edition,
Brief edition by Anderson and Franceschi, ISBN 9781284021301, 9781449632021 or 9781449604400
The full edition is compatible ISBN 9781449632014
MyCodeLab
Do 25 out of the 68 possible questions in sections 2.1 and 2.2 of MyCodeLab on the www.turingscraft.com website You will earn 4 points for each correct answer up to a maximum of 100 points You can retry incorrect answers Due by midnight on Wednesday, January 22
Bytecode Libraries
Bytecode Libraries
JIT
machine language
Programming Assignments
Programming assignments will define a program you are to create The assignment will explicitly define what the program is supposed to do Sample data and the expected results for that data is often provided Your program should work with any data, not just the sample data
Example Assignment
Write a program to input two numbers and display the average of the numbers
Sample input 2 6 Sample output Average is 4.0
Errors
When programming you will make mistakes. There are three types of programming errors
Compile errors When you compile your program, the compiler might detect an error (i.e. missing semicolon) Run time errors An error can occur when you program is running (i.e. division by zero) Logic errors Your program might not produce the correct results
You will have errors You will correct them Seek help if you dont understand the error
Class Programs
Requirements faculty will define briefly Specifications faculty will define Design student responsibility Coding student responsibility Testing informal student responsibility
import
If you use existing objects in your program, you need to tell Java where to find them import tells Java to look in this package for classes you are using The import statement allows you to use the short name of a class import is not required. You can always use the full name of an object.
import example
import javax.swing.JOptionPane;
other parts of the program ... JOptionPane.showMessageDialog();
or without import
javax.swing.JOptionPane.showMessageDialog();
Class statement
Java is an Object-Oriented language Objects are defined by classes All Java programs are an object Much more on objects to come later
main method
Methods are functions or programs that can be executed The method with the name main is always executed first main has an array of String parameters that can be used to pass command line arguments
Names
Names or identifiers in Java (such as program names, class names, variable names, etc.) can be as long as you like Names can contain letters, numbers underscores (_), and dollar signs ($) Spaces are not allowed in a name Names cannot start with a number Java is case sensitive, upper and lower case letters are different
Meaningful Names
Names should make sense to humans Variable names such as:
i X v027 v028
are not very meaningful The variable name should describe the data
balance numWidgets xCoordinate
Why Animals?
Java Conventions
While the Java language allows you to use upper and lower case letters as you please, tradition dictates you follow some rules: Variables and method names start with a lower case letter Class names start with an Upper Case letter Constants are all UPPER CASE When you combine two English words, upper case the first letter of the second word (i.e. firstPlace, mySchool, whoCares)
Variables
Variables hold data. They represent a memory location. Variables have a:
Name: so you can identify them in Java Type: the format of the data Value: assigned at run time and often changes
Memory Locations
owner
Fred
sum
47
price
665.95
When you create a variable in Java, it reserves memory to hold the data
Constants
int 1 2 3 -6 123456 double -1.234 0.000123 1.23e-4 String "inside double quotes" boolean true false character 'x' // a single character in single quotes
Declaring Variables
All variables must be declared before they are used in the program A variable is declared by writing the data type followed by the variable name More than one variable may be declared on the same line as the same type by separating the variable names by commas
Example Declarations
double int String boolean int interestRate; numPenguins; myName; doit; first, second;
Assigning Values
You can set a variable to a value during execution by putting the name of variable, an equals sign followed by a value and semicolon numPenguins = 6; first = 3; interestRate = 4.75; The type of the variable and the value must match.
Sequential Execution
Java programs are executed sequentially one line at a time int cat, dog; dog = 5; cat = dog; // cat has the value 5 dog = 7;
cat still has the value 5 while dog now has the value 7
Moving Data
cat ? dog ?
Moving Data
cat ? dog 5
dog = 5;
Moving Data
cat 5 dog 5
cat = dog;
Moving Data
cat 5 dog 7
dog = 7;
Variable Initialization
When you declare a variable, you can give it an initial value. If you dont give a variable an initial value, it will have some unknown random value. In the declaration after the variable name, put an equals sign followed by the value. The type of the variable and the value must match.
Example Initializations
double int String boolean int int interestRate = 0.075; numPenguins = 7; myName = "Ken"; doit = false; first = 1, second = 2; bad = "This is wrong";
Clickers
Clickers will be required on Friday, January 17
Textbook
Java Illuminated, 3rd edition,
Brief edition by Anderson and Franceschi, ISBN 9781284021301, 9781449632021 or 9781449604400
The full edition is compatible ISBN 9781449632014
MyCodeLab
Do 25 out of the 68 possible questions in sections 2.1 and 2.2 of MyCodeLab on the www.turingscraft.com website You will earn 4 points for each correct answer up to a maximum of 100 points You can retry incorrect answers Due by midnight on Wednesday, January 22