Download presentation
Presentation is loading. Please wait.
1
Java 101: Intro to Java Programming
Intoduce either this PPT or Edwar-Variables and Primative Types-Section 2.2.pptx Or combined. Show exercises, helloworld, Hello2, Hello3, StatementsWhiteSpacesAndIndentations, FloatAndDouble, CharAndBoolean,ByteShortInt, Strings, SimpleCalcuations, Interest. Then students do the exercises found in introductiontojava-session1-and exercises.pptx
2
Java 101 Java Fundamentals Setting up your development environment
Language Overview How Java Works Writing your first program Built-in Data Types Conditionals and Loops
3
Java 102 Object-oriented Programming Classes and Objects
Polymorphism, Inheritance and Encapsulation Functions and Libraries
4
Java 103 Data Structures Arrays Collections Algorithms
5
Java 101: Introduction to Java
Setting up your Development Environment
6
Installing Java Development Kit
Download latest Java SE 8 JDK (not JRE) from For Windows, download the X86 version, double click the .exe file and follow the instructions, accepting all default For MACs, check if java already installed (javac –version) and if not, download the JDK dmg file, run it and follow the instructions. After installation is complete, type javac –version in the Command window (Terminal window on MAC OS)- The reported version should be If not, you may need to modify the system variable PATH to include the bin directory of JDK
7
What is an IDE? IDE = Integrated Development Environment
Makes you more productive Includes text editor, compiler, debugger, context- sensitive help, works with different Java SDKs Eclipse is the most widely used IDE Alternatives: IntelliJ IDEA (JetBrains) NetBeans (Oracle)
8
Installing Eclipse Download and install the latest Eclipse for Java EE (32 Bit version) from Unzip the content of the archive file you downloaded To start Eclipse On PC, double-click on Eclipse.exe On Mac, double click Eclipse.app in Application folder
9
Intelli j Setup & Demo https://www.jetbrains.com/idea/download/
Hands-on Exercise Intelli j Setup & Demo
10
Java 101: Introduction to Java
Language Overview
11
Java Language Overview
12
Java Editions Java SE: Java Standard Edition
Java EE: Java Enterprise Edition (a.k.a. J2EE) includes a set of technologies built on top of Java SE: Servlets, JSP, JSF, EJB, JMS, et al. Java ME: Java Micro Edition Java Card for Smart Cards All Java programs run inside the Java Virtual Machine (JVM)
13
JDK vs. JRE Java Development Kit (JDK) is required to develop and compile programs Java Runtime Environment (JRE) is required to run programs. Users must have JRE installed, Developers must have the JDK installed JDK includes the JRE
14
Java 101: Introduction to Java
How Java Works
15
How Java Works
16
Java File Structure
17
Java 101: Introduction to Java
Writing Your First Program
18
Writing Your First Java Program
Create a new project in your IDE named Java101 Create a HelloWorld class in the src folder inside the Java101 project as illustrated below. System is a final class from java.lang package. out is the reference of PrintStream class and a static member of System class. println is a method of PrintStream class. Show the student the example I have HelloWorld, then ask them to create it-By Edwar System.out.print is a subroutine . This subroutine is used to display information to the user. For example, System.out.print("Hello World") displays the message, Hello World. System is one of Java's standard classes. One of the static member variables in this class is named out. Since this variable is contained in the class System, its full name -- which you have to use to refer to it in your programs -- is System.out. The variable System.out refers to an object, and that object in turn contains a subroutine named print. The compound identifierSystem.out.print refers to the subroutine print in the object out in the class System.
19
Compiling Your First Java Program
Save the HelloWorld class in the IDE Run your program: right-clicking and selecting Run As>Java Application This automatically compiles the HelloWorld.java file into into a HelloWorld.class file Go to the folder you created the Java101 project on your hard disk and open the src(eclipse) or out (Intelli j)folder. What do you see?
21
Anatomy of a Java Application
Comments Class Name Arguments Access modifier System is a built-in class... System is a final class from java.lang package. out is the reference of PrintStream class and a static member of System class. println is a method of PrintStream class. Function/static method
22
Built-in classes
23
Introduction to Java Built-in Data Types
24
Built-in Data Types Data type are sets of values and operations defined on those values.
25
Basic Definitions Variable - a name that refers to a value.
Assignment statement - associates a value with a variable. Variables hold the state of the program, and methods operate on that state. If the change is important to remember, a variable stores that change. That’s all classes really do. Java calls a word with special meaning a keyword It probably comes as no surprise that Java has precise rules about identifier names. Luckily, the same rules for identifiers apply to anything you are free to name, including variables, methods, classes, and fields. There are only three rules to remember for legal identifiers: The name must begin with a letter or the symbol $ or _. Subsequent characters may also be numbers. You cannot use the same name as a Java reserved word. As you might imagine, a reserved word is a keyword that Java has reserved so that you are not allowed to use it. Remember that Java is case sensitive, so you can use versions of the keywords that only differ in case. Please don’t, though.
26
String Data Type Useful for program input and output.
Data Type Attributes Values sequence of characters Typical literals “Hello”, “1 “, “*” Operation Concatenate Operator +
27
String Data Type
28
String Data Type Meaning of characters depends on context.
29
String Data Type Expression Value “Hi, “ + “Bob” “Hi, Bob”
“1” + “ 2 “ + “ 1” “ 1 2 1” “1234” + “ + “ + “99” “ ” “1234” + “99” “123499”
30
Command Line Arguments
Hands-on Exercise Command Line Arguments
31
Exercise: Command Line Arguments
Create the Java program below that takes a name as command-line argument and prints “Hi <name>, How are you?” import java.util.Scanner; public class Hello4 { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String usersName; System.out.print("What is your name?"); usersName = stdin.nextLine(); System.out.print("Hi, "); System.out.println(usersName + ". How are you?"); } NOTE: Show Hello4 example Challenge: Change the usersName to Uppercase
32
Integer Data Type Useful for expressing algorithms.
Data Type Attributes Values Integers between -2E31 to +2E31-1 Typical literals 1234, -99 , 99, 0, Operation Add subtract multiply divide remainder Operator * / % The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.
33
Integer Data Type Expression Value Comment 5 + 3 8 5 – 3 2 5 * 3 15
5 / 3 1 no fractional part 5 % 3 remainder 1 / 0 run-time error 3 * 5 - 2 13 * has precedence 3 + 5 / 2 5 / has precedence 3 – 5 - 2 -4 left associative (3-5) - 2 better style 3 – (5-2) unambiguous The last thing you need to know about numeric literals is a feature added in Java 7. You can have underscores in numbers to make them easier to read:
34
Double Data Type Useful in scientific applications and floating-point arithmetic Data Type Attributes Values Real numbers specified by the IEEE 754 standard Typical literals e Operation Add subtract multiply divide Operator * / Literals are valid values assigned to data types
35
Double Data Type Expression Value 3.141 + 0.03 3.171 3.141 – 0.03
3.111 6.02e23 / 2 3.01e23 5.0 / 2.0 10.0 % 3.141 0.577 1.0 / 0.0 Infinity Math.sqrt(2.0) To sum up: - float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the mantissa (or what follows from a scientific-notation number: *1012; is the mantissa). - Double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of mantissa. By default, Java uses double to represent its floating-point numerals (so a literal 3.14 is typed double). It's also the data type that will give you a much larger number range, so I would strongly encourage its use over float. There may be certain libraries that actually force your usage of float, but in general - unless you can guarantee that your result will be small enough to fit in float's prescribed range, then it's best to opt with double.
36
Java Math Library Methods Math.sin() Math.cos() Math.log() Math.exp()
Math.sqrt() Math.pow() Math.min() Math.max() Math.abs() Math.PI
37
Hands-on Exercise Integer Operations
38
Exercise: Integer Operations
Create a Java class named IntOperationin that performs integer operations on a pair of integers from the command line and prints the results. NOTE: Show example IntOperationinInteger_parseIntMethod. Also, show example IntOperationsin and ask the students to add division and remainder. Also Improve the printout to show clearly in words the sum, multiplication, division, and remainder.
39
Solution: Integer Operations
40
Boolean Data Type Useful to control logic and flow of a program.
Data Type Attributes Values true or false Typical literals true false Operation and or not Operator && || !
41
Logical operators True False ^ (EXCLUSIVE OR) & (AND) | (OR)
& (AND): Only TRUE if both operands are TRUE | (OR): Only FALSE if both operands are FALSE ^ (EXCLUSIVE OR): Only TRUE if both operands are DIFFERENT
42
Logical operators & (AND): Only TRUE if both operands are TRUE
| (OR): Only FALSE if both operands are FALSE ^ (EXCLUSIVE OR): Only TRUE if both operands are DIFFERENT
43
Conditional & Negation operators
b a && b a || b True False false true
44
Boolean Comparisons Take operands of one type and produce an operand of type boolean. operation meaning true false == equals 2 == 2 2 == 3 != Not equals 3 != 2 2 != 2 < Less than 2 < 13 2 < 2 <= Less than or equal 2 <= 2 3 <= 2 > Greater than 13 > 2 2 > 13 >= Greater than or equal 3 >= 2 2 >= 3
45
Type Conversion Convert from one type of data to another. Implicit
no loss of precision with strings Explicit: cast method.
46
Type Conversion Examples
expression Expression type Expression value “1234” + 99 String “123499” Integer.parseInt(“123”) int 123 (int) 2 Math.round( ) long 3 (int) Math.round( ) (int) Math.round( ) 11 * 0.3 double 3.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11 * 0.3)
47
Hands-on Exercise Leap Year Finder
48
Exercise: Leap Year Finder
A year is a leap year if it is either divisible by 400 or divisible by 4 but not 100. Write a java class named LeapYear that takes a numeric year as command line argument and prints true if it’s a leap year and false if not
49
Solution: Leap Year Finder
NOTE: delate the args [0] and change it with “2004”…etc
50
Data Types Summary A data type is a set of values and operations on those values. String for text processing double, int for mathematical calculation boolean for decision making In Java, you must: Declare type of values. Convert between types when necessary Why do we need types? Type conversion must be done at some level. Compiler can help do it correctly. Example: in 1996, Ariane 5 rocket exploded after takeoff because of bad type conversion.
51
Conditionals and Loops
Introduction to Java Conditionals and Loops
52
Conditionals and Loops
Sequence of statements that are actually executed in a program. Enable us to choreograph control flow.
53
Conditionals The if statement is a common branching structure.
Evaluate a boolean expression. If true, execute some statements. If false, execute other statements.
54
If Statement Example
55
More If Statement Examples
56
While Loop A common repetition structure.
Evaluate a boolean expression. If true, execute some statements. Repeat.
57
For Loop Another common repetition structure.
Execute initialization statement. Evaluate a boolean expression. If true, execute some statements. And then the increment statement. Repeat.
58
Anatomy of a For Loop
59
Loop Examples
60
For Loop
61
Hands-on Exercise Powers of Two
62
Exercise: Powers of Two
Create a new Java project in Eclipse named Pow2 Write a java class named PowerOfTwo to print powers of 2 that are <= 2N where N is a number passed as an argument to the program. Increment i from 0 to N. Double v each time
63
Solution: Power of 2
64
Straight line programs
Control Flow Summary Sequence of statements that are actually executed in a program. Conditionals and loops enable us to choreograph the control flow. Control flow Description Example Straight line programs all statements are executed in the order given Conditionals certain statements are executed depending on the values of certain variables If If-else Loops certain statements are executed repeatedly until certain conditions are met while for do-while
65
Java 101: Introduction to Java
Homework Exercises
66
Random Number Generator
Hands-on Exercise Random Number Generator
67
Exercise: Random Number Generator
Write a java class named RandomInt to generate a pseudo-random number between 0 and N-1 where N is a number passed as an argument to the program
68
Solution: Random Number Generator
69
Hands-on Exercise Array of Days
70
Exercise: Array of Days
Create a java class named DayPrinter that prints out names of the days in a week from an array using a for-loop.
71
Solution: Arrays of Days
public class DayPrinter { public static void main(String[] args) { //initialize the array with the names of days of the week String[] daysOfTheWeek = {"Sunday","Monday","Tuesday","Wednesday", "Thuesday","Friday”,"Saturday"}; //loop through the array and print their elements to //stdout for (int i= 0;i < daysOfTheWeek.length;i++ ){ System.out.println(daysOfTheWeek[i]); } % javac DayPrinter.java % java DayPrinter Sunday Monday Tuesday Wednesday Thuesday Friday Saturday
72
Print Personal Details
Hands-on Exercise Print Personal Details
73
Exercise: Print Personal Details
Write a program that will print your name and address to the console, for example: Alex Johnson 23 Main Street New York, NY USA
74
Hands-on Exercise Sales Discount
75
Exercise: Sales Discount
Create a new project in Eclipse named Sale Create, compile, and run the FriendsAndFamily class as illustrated below Debug this program in your IDE to find out how it works
76
Further Reading Java Tutorials - Java Language Basics - Eclipse IDE Workbench User Guide - Intelli J IDE User Guide - Eclipse Tutorial -
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.