Lecture 1: Introduction to Java Programming

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Java Programs + Algorithms
Basic Java programs with println statements. 2 Compile/run a program 1.Write it –code or source code: the set of instructions in a program 2.Compile it.
Building Java Programs Chapter 1 Introduction to Java Programming.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Outline Java program structure Basic program elements
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods, Avoiding Redundancy reading: self-check:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1: Introduction to Java Programming.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Static methods. 2 Algorithms algorithm: a list of steps for solving a problem Example algorithm: "Bake sugar cookies" –Mix the dry ingredients. –Cream.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Chapter 2: Java Fundamentals
Introduction to Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Welcome to CSE 142! Zorah Fung University of Washington, Summer Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Zorah Fung University of Washington, Winter 2016
Lecture 1: Basic Java Syntax
Static Methods and Method Calls
Working with Java.
Welcome to CSE 142! Whitaker Brand and Benson Limketkai
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Lecture 2: Data Types, Variables, Operators, and Expressions
CSE 190D, Winter 2013 Building Java Programs Chapter 1
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Benson Limketkai and Marty Stepp University of Washington, Spring 2010
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Topic 3 static Methods and Structured Programming
Chapter 1: Computer Systems
Benson Limketkai University of Washington, Spring 2011
Building Java Programs
Whitaker Brand University of Washington, Winter 2018
Units with – James tedder
Units with – James tedder
Advanced Programming CS206
Lecture 1: Basic Java Syntax
Building Java Programs
Topic 2 Java Basics “When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers.
Building Static Methods
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Welcome to CSE 142!.
CSE 142, Summer 2012 Building Java Programs Chapter 1
Building Java Programs
Focus of the Course Object-Oriented Software Development
Building Java Programs
Building Java Programs
Building Java Programs Chapter 1
CSE 142, Spring 2012 Building Java Programs Chapter 1
Zorah Fung University of Washington, Spring 2015
Chap 2. Identifiers, Keywords, and Types
CSE 142, Winter 2014 Building Java Programs Chapter 1
Agenda Types and identifiers Practice Assignment Keywords in Java
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Lecture 1: Introduction to Java Programming Dr. Kyung Eun Park Summer 2017

Computer and Me?

How to communicate with computer? How does computer listen to? How does computer talk? How does computer think?

~~~ Analog Sound Wave ~~~ Our Communication Hi Joy! Hi Jack! ~~~ Analog Sound Wave ~~~

Computer with us? Hi Com! Hi Java! I got it! Hello Boys!

Computer Knows Only 0s and 1s In computer systems, data is stored and represented in binary digits, called bits. A bit says two states: On or Off Open or Close True or False 0 or 1 Black or White 0 or 1 More Colors for Red and Blue ? 00: Black 01: White 10: Red 11: Blue Two Bulbs 0 or 1 0 or 1

Bit and Byte Bit Byte 1KB, 1MB, 1GB, 1TB, 1PB Binary digit 0 or 1 21 (=2) states cf) 22 (=4), 23 (=8) Byte 8-bit sequence 28 (=256) states 256 different colors 256 different letters 256 different cards 1KB, 1MB, 1GB, 1TB, 1PB 210 , 220 , 230 , 240 , 250 ,

Our Number System: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 : One blank  9 + 1? : Two blanks  10, 11, ~, 99 Three blanks  100, 101, ~, 999 Decimal Number System using 10 Digits!

Computer’s Number System: 0, 1: One blank (bit)  2, 3? : No digit 2, 3 for a bit  Two bits 4, 5, 6, 7  Three bits? 1 00 01 10 11 000, 001 010, 011 100, 101 110, 111 0000, 0001, 0010, 0011 0100, 0101, 0110, 0111 1000, 1001, 1010, 1011 1100, 1101, 1110, 1111 Binary Number System using Two (0 and 1) Digits!

Base-10 Decimal Number The decimal number 5872 is interpreted as follows. 5 8 7 + 2 5 8 7 2 = 5 x 103 + 8 x 102 + 7 x 101 + x 100 2

Base-2 Binary Number Used in machine language (language that computers understand) The binary notation 1011 is interpreted as follows 1 1 1 = 1 x 23 + x 22 + 1 x 21 + 1 x 20 = 1 x 8 + x 4 + 1 x 2 + 1 x 1 = 8 + + 2 + 1 = 11 (eleven, in decimal notation)

Binary Number System in Computer Sequence of 0s and 1s B. N. Bit 3 Bit 2 Bit 1 Bit 0 D. N. 1 10 2 100 4 1000 8 1011 11 1111 15

Bits for Text Symbols

Sound Processing with Computer Audio files: mp3, wav

Analog Sound Wave to Digital Sound Sampling and Quantizing

Digitizing Software

How to Store Alpha-Numeric Data? Convert Data to Digital Format Data Categorization: Integer Character Real number String

Java’s Types Primitive Types: Special (Object) Type: String Java supports 8 simple types for numbers, text, etc. byte, short, int, long, float, double, char, & Boolean Special (Object) Type: String

Real Number Decomposition Sign, Mantissa, and Expoent 1. 6 3 7 x 10 5 Start with 1.637 Shift point 5 places to the right 163700

Real Number Representation in Computer Mantissa and Exponent 0.10100111 x 2 5 Start with 0.10100111 Shift point 5 places to the right 10100.111 20 and ½+ ¼+⅛ (=⅞) 20. 875

Java’s Operators Operators: Combines multiple values Simple Expression: Left_Operand Operator Right_Operand + addition - subtraction (or negation) * multiplication / division % modulus (a.k.a. remainder) Expression evaluation 1+1 evaluates 2 3*4 evaluates 12 (1+5)*5 evaluates 30 9/2 evaluates 4 156%10 evaluates 6

Your First HelloWorld Program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } Save it as HelloWorld.java (case sensitive)

Java Programming Writing code Compiling code (Interpreting it into machine code, bytecode) Running code on Java Virtual Machine source code compile byte code run output

Structure of a Java Program class: a program public class name { public static void main(String[] args) { statement; ... } Every executable Java program consists of a class, that contains a method named main, that contains the statements (commands) to be executed. method: a named group of statements statement: a command to be executed

System.out.println() A statement that prints a line of output on the console. pronounced "print-linn“ Prints the given text message System.out.println("text"); Prints a blank line of output System.out.println(); Console: Text box into which the program's output is printed.

Naming Conventions User Defined Names: Identifiers Identifiers Program (Class) Name Methods Variables Constants Identifiers Must start with a letter or _ (underscore) or $ Construct using letters [A-Za-z], digits [0-9], and ‘_’ and ‘$’ symbols

Keywords (Reserverd Words) keyword: An identifier that you cannot use because it already has a reserved meaning in Java. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

Programming Errors Syntax Error Semantic Error (Logic Error) Found at compile time by the compiler Java language grammar Every Java statement ends with a semicolon, ; Contents of a class or a method enclosed between { and } * Program organizing tip: Use indentation for better readability and debugging Semantic Error (Logic Error) Found at program run-time by the virtual machine Results in program failure

Comment for Your Program A Good Programmer explains his/her program with comments as much as possible Comments are neither compiled nor executed. Syntax // comment text, on one line or, /* comment text; may span multiple lines */ Example // This is a one-line comment. /* This is a very long * multi-line comment * generally for program header */

Structured Programming w/ Static Methods First design the logic solving your problem Start writing a program class Include a static main() method as the program entry Declare static methods if necessary  modular programming rather than one single method program Organize the main() method with multiple method invocations

Static methods Static method: A named group of statements. denotes the structure of a program eliminates redundancy by code reuse Procedural decomposition: dividing a problem into methods Writing a static method is like adding a new command to Java. class method A statement method B method C

Algorithm Design for Baking Cookies Problem // This program displays a delicious recipe for baking cookies. public class BakeCookies1 { public static void main(String[] args) { // Step 1: Make the cake batter. System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); // Step 2a: Bake cookies (first batch). System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); // Step 2b: Bake cookies (second batch). // Step 3: Decorate the cookies. System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles."); }

Declaring a Static Method Syntax: public static void name() { statement; statement; ... statement; } Example: public static void printStop() { System.out.println(" ______"); System.out.println(" / \\"); System.out.println("/ \\"); System.out.println("| STOP |"); System.out.println("\\ /"); System.out.println(" \\______/"); System.out.println(); }

Calling a Method Syntax: method_name(); Example: printStop();

Final Cookie Program // This program displays a delicious recipe for baking cookies. public class BakeCookies2 { public static void main(String[] args) { makeBatter(); bake(); // 1st batch bake(); // 2nd batch decorate(); } // Step 1: Make the cake batter. public static void makeBatter() { System.out.println("Mix the dry ingredients."); System.out.println("Cream the butter and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); // Step 2: Bake a batch of cookies. public static void bake() { System.out.println("Set the oven temperature."); System.out.println("Set the timer."); System.out.println("Place a batch of cookies into the oven."); System.out.println("Allow the cookies to bake."); // Step 3: Decorate the cookies. public static void decorate() { System.out.println("Mix ingredients for frosting."); System.out.println("Spread frosting and sprinkles.");

String String: A sequence of characters Starts and ends with a " character " … " Examples: "hello" "This is a string. It's very long!" Restrictions: May not span multiple lines. "This is not a legal String." May not contain a " character. "This is not a "legal" String either." Use Escape Sequence!

Escape Sequences Escape sequence: A special sequence of characters used to represent certain special characters in a string \t tab character \n new line character \" quotation mark character \\ backslash character Example: System.out.println("\\hello\nhow\tare \"you\"?\\\\"); Output: \hello how are "you"?\\

Questions What is the output of the following println statements? System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral"); Write a println statement to produce this output: / \ // \\ /// \\\

Answers Output of each println statement: a b c \\ ' """ C: in he downward spiral println statement to produce the line of output: System.out.println("/ \\ // \\\\ /// \\\\\\");

Setting Programming Environment Install Java Language Library at Oracle.com http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html Windows x64 Offline Mac OS X Dr. Java (Simple Java Development Environment) http://www.drjava.org/ Download Windows App Download Mac OS X App Eclipse Windows x64 : https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R3/eclipse- inst-win64.exe Mac OS X: NetBeans https://netbeans.org/downloads/