Course Development Environment

Download Report

Transcript Course Development Environment

LECTURE 1 : INTRODUCTION
MPCS 51036 - JAVA
PROGRAMMING
LAMONT SAMUELS, PH.D.
▸ Email: [email protected]
▸ Office: Ryerson 176
▸ Office Hours: Mondays 8:30pm to 10:30pm
AGENDA
▸ Course Overview
▸ Introduction to Java
▸ Course Development Environment
CLASS SURVEY
▸ How many people have taken MPCS 50101 (Immersion
Programming)?
▸ Ice breaker
▸ Name
▸ What you studied in undergrad?
▸ What you want to accomplish with this degree?
EVALUATIONS
_01Control 5%
_02Arrays 5%
_03Objects 5%
_04Interfaces 5%
_05Dice 5%
_06Design 5%
_07BlackJack 5%
_08Final 25%
Quarterterm exam 10% (third session)
Midterm exam 20%
Class participation 10%
TURNING IN PROJECTS
▸ Be sure to push your latest commit(s) to your remote repo prior to the deadline.
▸ Projects are automatically fetched from your remote proJava at the due date/time-typically 11:59pm.
▸ The master branch will be graded.
▸ Your projects are evaluated based on the state that they were in when the TA
fetched them. NO EXCEPTIONS.
PROJECT GRADING
▸ 34% Does the program perform per spec, or as expected?
▸ 33% Is the algorithm elegant/efficient with respect to
resources such as memory, have you handled exceptions
properly?
▸ 33% Is your code style; naming variables, formatting, easeof-reading, well-documented?
COURSE OVERVIEW
▸ All course information is located on the course website:
http://java-class.cs.uchicago.edu/samuels/
AGENDA
▸ Course Overview
▸ Introduction to Java
▸ Course Development Environment
JAVA
▸ Designed by James Gosling and Patrick Naughton at Sun
Microsystems (now Oracle) consumer devices, but it was first
successfully used to write Internet applets.
▸ Main Goals: Safety and Portability
▸ Modeled after C++ but without some of confusing features of
C++
JAVA IN PRACTICE
▸ Highly popular language: http://www.tiobe.com/tiobe-index/
▸ Large number of job postings for the language:
http://www.indeed.com/jobtrends/q-Java-q-C%23-q-pythonq-javascript-q-c-c++-q-php.html
▸ Most used language in mobile smartphone development:
http://www.idc.com/prodserv/smartphone-os-marketshare.jsp
Images Copyright © 2013 by John Wiley & Sons. All rights reserved.
JAVA VIRTUAL MACHINE
▸ The Java compiler compiles Java source code and
generates .class files which are interpreted as byte code by
any Java Virtual Machine (JVM).
▸ The emergence of Just-In-Time (JIT) compilers has greatly
improved performance and now there is little difference
between natively compiled code and bytecode.
TEXT
JAVA VERSIONING
▸ Java uses different version numbers for the Java Platform
Standard Edition.
▸ For example: Version "5.0" is the product version, while "1.5.0" is
the developer version
▸ Jdk1.5 == Java 5.0
▸ Jdk1.6 == Java 6.0
▸ Jdk1.7 == Java 7.0
▸ Jdk1.8 == Java 8.0
TEXT
CORE LANGUAGE FEATURES
SIMPLE JAVA PROGRAM
▸ Java is a strict Object-Oriented language. All code is required to
be inside a class and must define one main function.
public class HelloMPCS
{
public static void main (String[] args)
{
System.out.println(“Hello MPCS 51036 Students!”);
}
}
▸ Java files must match the name of class defined within the file:
HelloMPCS must be defined in a filed called HelloMPCS.java
PRIMITIVES VS. OBJECTS
Primitives
Objects
Primitives are represented by
variables in Java.
Objects are represented by references in
Java.
Syntax: type name = expression;
Syntax: type name;
Syntax: type name = expression;
Syntax: type name;
A variable name is an unique identifier
that is either lower_case or
A reference name is an unique identifier
camelCase.
that is either lower_case or camelCase.
For example: nStudents, dHeight,
dWidth, fPerimeter
A variable type is the name of the
primitive it represents.
For example: int, double, byte, short,
long
For example: perTeacher, arrStudents,
A reference type is the name of the
class it represents:
For example: Student, Bicycle, Account,
ArrayList, Hashtable, List
JAVA PRIMITIVE DATA TYPES
Type
int
Description
Signed Integer type, 32 bits, 4 bytes.
Range: -2,147,483,648 to +2,147,483,647.
Integers can be cast to all other numeric types (byte, short long, float, double).
byte
Signed Integer type consisting of 8 bits, 1 signed byte (two's complement).
Range: -128 to +127.
short
Signed short integer type, 2 bytes, signed (two's complement)
Range: -32,768 to +32,767
long
Signed long integer type, 64 bits, 8 bytes signed (two's complement)
Range: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
double
Double-precision floating-point type, 8 bytes IEEE 754, About 15 decimal digits
Range: +- 10^308
float
Single-precision floating-point type, 4 bytes IEEE 753, About 7 decimal digits
Range: +- 10^38
char
Character Type, 2 bytes, unsigned, Unicode, Not the same as other integer numeric types or
Strings (No math operators).
Range: 0 to 65,535
boolean
1-bit, takes only the values of “true” or “false”, which are defined keywords in the language. No
casting of any sort is permitted.
▸ Note: Java is implementation independent. For example, int is always 32 bits and a long is
always 64 bits, etc.. This not necessarily the same for all languages (e.g. C++).
PRIMITIVE DECLARATIONS,
ASSIGNMENTS,
EXPRESSIONS
▸ Primitive Declarations:
int nSum = 0;
boolean bIsDone = false;
double dTotal = 0.0;
‣ Use the assignment operator (=) to update a variable:
double dDifference = 0;
double dValue1 = 2.0, dValue2 = 4.0;
dDifference = dValue1 - dValue2; // dDifference = -2.0
‣ Java supports all common arithmetic operations (+,-,/,*) that can build
expressions, which are assigned to numeric types.
float fBase = 3.0, fHeight = 4.5, fArea = 0.0;
fArea = (fBase * fHeight) / 2.0;
ORDER OF PRECEDENCE : PEMDAS
Operators
Precedence
Postfix
expr++ expr--
Unary
++expr --expr +expr -expr ~ !
Multiplicative
*/%
Additive
+-
Shift
<< >> >>>
Relational
< > <= >= instanceof
Equality
== !=
Bitwise AND
&
Bitwise exclusive OR
^
Bitwise inclusive OR
|
Logical AND
&&
Logical OR
||
Ternary
?:
Assignment
= += -= *= /= %= &= ^= |= <<= >>= >>>=
OPERATOR OVERLOADING
▸ Java does not allow operator overloading like other
languages such as C++.
▸ With the exception of the String type allows for string
concatenation of the numeric types:
int nValue1 = 23;
int nValue2 = 6;
String strName = “The values are : “;
System.out.println(strName + nValue1 + “,” + nValue2);
PREFIX AND POSTFIX UNARY
OPERATORS
▸ Prefix Operator (e.g.: ++nX, --nX): the value of the variable is
incremented, and the value of the expression is the new value of the
variable:
int nX = 0;
int nY = 0;
nY = ++nX; // nY = 1, nX = 1
▸ Postfix Operator (e.g.: nX++, nX--): the expression uses the current
value of the variable, and then the variable is incremented.
int nX = 0;
int nY = 0;
nY = nX++; // nY = 0, nX = 1
CONVERTING BETWEEN NUMERIC
TYPES
▸ It is safe to convert from an integer type to a floating-point type. There is
not precision that is lost.
▸ But going the other way can be dangerous. Fractional portion is lost (not
rounded).
▸ You can intermix integer and floating-point types in an expression without
losing precision. The expression yields a floating-point value:
float fArea= 0.0, fPi = 3.14;
int nRadiux = 3;
fArea = nRadiux * nRadiux * fPi;
▸ Integer division loses all fractional parts of the result and does not round.
▸ int nResult = 5/8 // nResult = 0 and not 0.625
FLOATING-POINT TO INTEGER
CONVERSION
▸ Java does not allow the direct conversion from a floating-point value to an
integer value.
double dTotal = 123.0, dTax = 3.56;
double dBalance = dTotal + dTax;
int dollars = balance; // Error
▸ Use the ‘cast’ operator to force the conversion.
▸ Syntax: (type), E.g.: (int), (double), (float)
double dTotal = 123.0, dTax = 3.56;
double dBalance = dTotal + dTax;
int dollars = (int) balance; // No Error
‣ Remember the fractional portion is lost. (no rounding occurs).
CHARACTERS AND STRINGS
▸ Characters are represented as a 16bit Unicode character set
that encompasses all languages.
▸ Characters can also be treated as integers but cannot be
used with the numeric integer operators.
▸ A character literal is represented in single quotes, a string
literal is represented in double quotes:
char cLetter = ‘a’;
String strLetter = “a”;
STRING METHODS
▸ The following are some helpful and commonly user string methods:
char charAt(int index)
int compareTo(String anotherString)
boolean endsWith(String suffix)
int indexOf(multiple)
int length()
String substring(int begin, int end)
String trim()
‣ Example Uses:
String strName = “Lamont”;
int nLen = strName.length();
char cIndex2 = strName.charAt(2); // cIndex2 = ‘m’
boolean bIsTrue = strName.endsWith(“ont”); // bIsTrue = true
CONTROL-FLOW STRUCTURES
▸ Java has the following branching (conditional) statements:
if
if-else
switch
ternary
▸ Java has the following looping statements:
for
while
do-while
break/continue
READING IN INPUT
▸ Use the Scanner class to read keyboard input from the user.
▸ You need to import the class from the java.util library
▸ You’ll need to declare the scanner object as such:
Scanner in = new Scanner(System.in);
▸ Use the “next” functions to read in data from the user.
nextInt() // read in integer
nextDouble() // read in double
… etc
‣ For example:
import java.util.Scanner;
.
.
.
Scanner in = new Scanner(System.in);
System.out.println(“Please enter in the number of students: “);
int nStudent = in.nextInt();
JAVA API
▸ The Java library declares a large number of useful classes and
methods for developing Java programs:
▸ Math library, Scanner library, etc.
▸ Java classes are grouped into packages. Use the import statement to
use classes from packages:
▸ E.g.: import java.util.Scanner;
▸ The full documentation for the Java Standard Library is located here:
▸ http://docs.oracle.com/javase/8/docs/api/
NAMING CONVENTIONS
LETTER CASING IN JAVA
▸ All class names begin with an upper case letter.
▸ public class Bicycle { … }
▸ All letters for constants and enumerations are upper case and are separated by an
underscore (_).
▸ public final double PI = 3.1415926;
▸ public final int
▸ public enum Color { RED, BLUE, GREEN }
▸ All other names for declarations use camel case notation.
▸ private int nTravelers;
▸ public double computeArea(double length, double width) {…}
▸ private String strName;
NAMING NOTATION
▸ Use Hungarian Notation, which is naming convention in which the type
and/or scope of a variable is used as a naming prefix for that variable.
▸ int value; // non-Hungarian
▸ int nValue; // the n prefix denotes an integer
▸ Many different flavors of Hungarian Notation, which have varying degrees
of complexity. We will use a simplified version called Caste Hungarian
Notation:
▸ A prefix describes metadata about the reference, primitivevariable, or collection.
▸ Helpful since you do not have to refer back to the declaration to
determine the type of the primitive or variable.
LOCAL VARIABLE CONVENTIONS
Type prefix
Meaning
Example
b
boolean
boolean bDone;
y
byte
byte yLetter;
c
char
char cMiddleInitial;
s
short
short sAge;
n
int
int nStudentId;
l
long
long lPopulation;
f
float
float fSum;
d
double
double dThreshold;
str
String
String strName;
(first three letters of class name)
Object
Shape shaRectangle;
CLASS MEMBERS
▸ Affix a “m” to the beginning of a class member declaration.
▸ Affix a “s” to the beginning of a class static member declaration.
public class Student {
String mName;
static String sCollege ="UChicago";
int mId;
double mGPA;
}
LOCAL ARRAYS AND COLLECTIONS
▸ Affix a “s” to the end of a array or collection declaration.
ArrayList<Movies> movDramas
double bAreas
String[] strNames
▸ Avoid using “s” with names that are associated with single
objects or single primitives.
▸
Eg: dRadiux, instead of dRadius
WHY NAMING CONVENTIONS
▸ Makes it easier to distinguish metadata by looking at the
naming of a local variable or reference, which leads to easier
to debug and read code.
▸ Helps with formalizing expectations and promote
consistency within a team.
SOLVING PROBLEMS
(ALGORITHM DESIGN)
STEPS TO SOLVING A PROGRAMMING
PROBLEM
1. (Design) : Identify the problem and throughly understand it.
‣ Think about potential test cases that are associated with
the problem.
2. (Analyze): Write Pseudocode
3. (Implementation): Write the Java code
4. (Experimentation): Develop test-cases and refactor/modify
code as required.
PSEUDOCODE
▸ A simplified (intermediate) programming language.
▸ Describes the algorithm in simple terms.
▸ Well written pseudocode makes it easier to implement the
actual algorithm.
▸ When using control-flow structures (e.g., looping and
branching logic) each structure has a body which you must
indent.
PSEUDOCODE EXAMPLE 1
▸“Implement an algorithm for counting the number of vowels
in a sequence of letters that is provided by the user. Print the
number of vowels to the user.”
//Prompt user to “Enter in a sequence of letters”
//Store sequence of letters in a String
//Define a counter variable to hold the number of vowels
//Set the counter to zero
//For each char in the sequence
//If the word is a vowel (‘a’,’e’,’i’,’o’,’u’)
//Increment the counter
//Print the number of vowels to the user
PSEUDOCODE EXAMPLE 2
▸“Implement an algorithm that asks the user to enter in a sentence and
prints out the sentence with only the even words capitalized. ”
//Prompt user to “Enter in a sentence:”
//Store sentence in a String
//Split the String using space as a delimiter
//For each word in the sentence
//If the word is even
//Print the word in all upper-case
//Else (word is odd)
//Print the word normally
//Print space
AGENDA
▸ Course Overview
▸ Introduction to Java
▸ Course Development Environment
COURSE DEVELOPMENT
ENVIRONMENT
GIT
▸ Modern version control system.
▸ Is an example of a DVCS (Distributed Version Control
System).
▸ As Atlassian states, “Rather than have only one single
place for the full version history of the software as is
common in once-popular version control systems like CVS
or Subversion (also known as SVN), in Git, every
developer's working copy of the code is also a repository
that can contain the full history of all changes"
QUOTE FROM HTTPS://WWW.ATLASSIAN.COM/GIT
GIT ARCHITECTURE
IMAGE FROM HTTP://WWW.SLIDESHARE.NET/AMMARLAKIS/GIT-WORKSHOP-33610244
GIT SETUP
▸ Download Git from here: git-scm.com/downloads
▸ After installing Git and placing it in your PATH environment variable
then you want to configure your email and name
▸ git config --global user.name “Lamont Samuels"
▸ git config --global user.email “[email protected]”
▸ Normally, you’ll want to clone a repository. Cloning copies an existing
Git repository.
▸ git clone https://[email protected]/lsamuels/projava.git
GIT COMMANDS
▸ Add and Commit Files
▸ Add files from your working-dir to stage-dir
git add .
git add src/.
git add src/MyFile.java
▸ Moving files back from stage-directory to working directory.
git reset HEAD .
git reset head src/.
git reset head src/MyFile.java
GIT COMMANDS
▸ Once you add a file/directory you need to commit them to
HEAD, but not in your remote repository yet.
git commit -m "Commit message”
▸ Once your changes are in HEAD then you send those
changes to your remote repository.:
git push origin master
▸ To fetch changes from your remote directory use fetch:
git fetch --all
INTELLIJ ULTIMATE IDE