Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Introduction to Programming w/ Java – Week 1
Week 1 Agenda:
Introduction to the Elective, Overview of Java, Introduction to MS-DOS, Writing a Simple program
Elective Overview:
* This is a hands-on elective but during each class, we will talk for 10min before we code – pay attention!
* Prepare to feel like a hacker – you will get into details of computers and compilers – but ASK questions if you don’t
understand – much of this information is not intuitive!
* We are here to learn Java, not surf the internet – this is not computer free time…..
Two golden rules of programming:
1. Programming is a TEAM sport – if your neighbor is struggling, help them out!
2. Don’t type too much at once – add a couple lines of code at a time, Save, and Test.
JAVA Overview:
How to create a Java program - overview:
1. Write the code – at first we will use a text editor called Notepad++
2. Save the file as a “.java” file, for example “helloPanthers.java”
3. Compile the javafile using the java compiler command “javac”
4. Run the program using the Java Runtime Environment with the command “java”
Where to Save Files? on your memory stick, each week create a new folder called “Week1”, “Week2”, etc.
- Cleanliness is Divine!
- Names must be descriptive, no spaces - use camelCase
- Files will end with “.java”
Let’s get started – First we need to open our text editor – Notepad++, then we can type in the following program:
First Program: helloPanthers.java
----------------Program 1:
public class helloPanthers {
public static void main(String[] args) {
System.out.println( "Hello Peabody Panthers!");
}
}
-----------------------In many programming languages, we write our programs as one continuous unit. Java is a little different.
In Java, we create things called a ‘class’, which is a framework used to define specific “objects”, hence the name “OOP –
Object Oriented Programming”. Our objects can implement pieces of our program.
Example – we could define a class called bicycle, and then “instantiate” or create instances of that class, such as “benBicycle,
ianBicycle, jonahBicycle”. In this case our class is “helloPanthers”.
Inside our classes we will often have smaller units called “Methods” - which typically perform a function.
To start our program running, somewhere in one of the classes we create we need to have a “main” Method – i.e.the
procedure which gets the program going.
Keywords – specific words which tell the Java Compiler what to do:
public – anything declared public is visible everywhere (outside the class), and can be called from other classes (remember in
programming you and your friends will work together – all writing different classes (chunks of code).
class – immediately preceeds the class name – in this case helloPanthers
main – inside our program somewhere we need a Method named main – (remember this gets the program running).
static – makes something (variable, Method) associated with the class, not just a specific instance (object). Things that are
static are known to all new instances (objects) of this class.
void – this keyword is used when a Method does not return a value, as often methods will return information, such as
numbers or strings.
String[] args – this allows us to run our java program with some command line arguments, for instance if we wanted a
program named “MyProgram” to use a data file named “ianData.txt” we could run our program by typing “java MyProgram
ianData.txt”. More on this another time.
Writing our first program: To create, compile, and run a java program, we must do several things:
1. Write the code and save it with class name and the .java extension. This program will be saved as helloPanthers.java
We will use a text editor called Notepad++. There are many other intelligent text editors out there – Sublime, Textpad, etc.
Later we will use a fancier IDE – “Integrated Development Environment” such Eclipse or Netbeans – these IDEs combine
the editor, the debugger, the compiler, the runtime environment, and many other tools.
2. Compile the program. This is where the computer translates our code into instructions (bytecode) that can be understood
by the JVM (Java Virtual Machine). You can download a java compiler for free as part of the JDK – Java Development
Kit. To see which version we are running type the following line: javac –version, and you should see something like this
“javac 1.8.0_05”
To compile our program, we go to the command line of Microsoft Windows, with a command prompt window cmd (found
under the start button). We will need to know some other commands to find our files:
dir – shows us the files in our current directory. Using dir /w works better if there are many files.
cd – change directory
cd .. – goes backwards (up) one directory in our tree
del – delete a file
javac helloPanthers.java - this runs the Java compiler which translates our program into bytecode.
This command creates a .class file.
3. Run the program. This is where our bytecode (.class file) is fed into the JVM – Java Virtual Machine. The JVM is a set
of programs that are specific for the underlying computer (IBM PC, Mac, iphone, Droid). The JVM further interprets the
bytecode, and translates (compiles) into machine language – which are instructions which can be understood by the
computers CPU (Central Processing Unit). The JVM, along with some other software called APIs (Application
Programming Interface) is called the JRE – Java Runtime Environment. Very important –your JRE (Java Runtime
Environment) must match – or be a later release – than your JDK, meaning you can’t run a program compiled with a new
compiler on an old runtime environment. To see which version of the JRE you have, type the following:
java –version and you should see something like this – “java version 1.8.0_05”
To run our program, we will use the following command:
java helloPanthers note – to make this work we are using the file “helloPanthers.class” – but we leave off the .class
extension when running the program.
Now that you have the basics, let’s write another program, save it (as a .java file), then compile it using “javac
gradeCalculator.java” to make our .class file, then run it “java gradeCalculator”.
----------------Program 2:
public class gradeCalculator {
public static void main(String args[]) {
float finalGrade = 0.0;
int testGrade1 = 90;
int testGrade2 = 100;
finalGrade = (testGrade1 + testGrade2)/2;
System.out.println("Your final grade is "+finalGrade);
}
}
------------------------
In each class, we typically have three sections:
-Fields (variables) – here we define two integers and a floating point (decimal) number variable.
-Constructors(objects) – where we would “instantiate” or create objects from classes. none here….
-Methods (procedures) – our only Method is our main Method.
Introduction to Java – where from and Why?
- Fortran (FORmula TRANslator) – IBM
- COBOL (Common Business Oriented Languages) – Federal Government
1960s – “B” Programming Language
1972 – “C” Programming Language – allowed programmers to dynamically allocate memory
1980 – “C++” – objects with attributes/functions – Object Oriented Programming
1995 – “Java” – universal language – no recompiling for different computers (Mac vs PC)
Java Compiler vs. Java Runtime Environment vs. Java Virtual Machine
how it all fits together….