CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © 2007 1.1 Your First Java.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CSE 131 Computer Science 1 Module 1: (basics of Java)
IB Computer Science II Paul Bui
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Program.
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CS107 Introduction to Computer Science Java Basics.
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CSC204 – Programming I Lecture 4 August 28, 2002.
1.1 Your First Program Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Lecture 2: Classes and Objects, using Scanner and String.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CSE 131 Computer Science 1 Module 1: (basics of Java)
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Fall 2006Slides adapted from Java Concepts companion slides1 Introduction Advanced Programming ICOM 4015 Lecture 1 Reading: Java Concepts Chapter 1.
Working with arrays (we will use an array of double as example)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introduction to Java Java Translation Program Structure
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
CSc 201 Introduction to Java George Wells Room 007, Hamilton Building
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Methods.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CS 177 Recitation Week 1 – Intro to Java. Questions?
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 1: Introduction to Computers and Programming.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
Basic concepts of C++ Presented by Prof. Satyajit De
The eclipse IDE IDE = “Integrated Development Environment”
The need for Programming Languages
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CompSci 230 Software Construction
Programming In Any Language With “Hello, World!” Examples
The Boolean (logical) data type boolean
IB Computer Science II Paul Bui
F II 2. Simple Java Programs Objectives
How to Run a Java Program
Presentation transcript:

CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java Program

CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Parlez-vous Java? Group Exercise: Form teams of 3 students 2

Code Snippet 1 3 int a = 20; int b = 30; int t; t = a; a = b; b = t; Swap the contents of variables a and b a = 30 b = 20

Code Snippet 2 4 int a = 15; if(a % 2 == 0) System.out.println(“Yes”); else System.out.println(“No”); Checks whether a number is even or odd. % - Modulo operator – Calculates the reminder == - Checks for equality Program will print NO

Code Snippet 3 5 Checks whether “n” is prime Program will print YES int n = 7; int i = 2; int flag = 0; while(i < n) { if(n % i == 0) { System.out.println(“No”); flag = 1; break; } i = i+1; } if(flag == 0) System.out.println(“Yes”);

CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Create, Compile, Execute

Create the program – You express the computation you wish to perform (algorithm) using a language that YOU can understand – The language should also be precise enough for the computer to understand – High-level language (Java) – Java provides:  A set of constructs (analogous to vocabulary)  A set of rules on how to use these constructs in a program (analogous to grammar) 7

8 A Rich Subset of the Java Language Primitive Numeric Types != ==>=<= <>-- / + % - ++ * Integer.parseInt() Double.parseDouble() Parsing Math.max()Math.min() Math.pow()Math.sqrt() Math.PIMath.abs() Math.log() Math.sin() Math Library Math.exp() Math.cos()System.out.println() System.out.print() System.out.printf() System for if Flow Control while else ! || true Boolean && false ;, ( { Punctuation ) } a[i] new a.length Arrays matches()charAt() length() + String compareTo() "" booleanchar long int Built-In Types String double equals()toString() main()new public class Objects private static

How Does Your Program Run on the Hardware? Hardware itself uses a very low-level language called Machine Language to run the program Machine language consists of a set of very simple constructs called instructions that manipulate 0s and 1s Hardware also provides the means to store the 1s and 0s that are generated by a program as it runs a computation Need to convert the Java program to an equivalent machine language program that the hardware understands 9

Create, Compile, Execute Compile the program – Compiler is itself a (very sophisticated) program – Takes the Java program as input and converts it to the corresponding sequence of machine instructions – Inspects the program you wrote to see whether you have followed the conventions of the language (syntax) correctly – Compiler doesn’t know what you are trying to compute though! (semantics) Execute – Computer runs the sequence of machine instructions produced by the compiler 10

11 Programming in Java HelloWorld.java /******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }

Using an Interactive Development Environment (IDE) to Create, Compile, Execute An IDE is a software tool that: n Includes a “smart” editor for your language n Lets you compile all your Java from within the tool n Lets you run the compiled program from within the tool n Supports debugging n Supports many other programmer needs, especially for large programs Example IDEs for Java: DrJava (for beginners) Eclipse (powerful!) Important to know how Java programs are built and run without using IDEs! 12

Dr. Java Demo Where do I get this software? n Instructions for installing Java and Mac are available on Collab slides page 13

14 Programming in Java Programming in Java. Create the program by typing it into a text editor or Dr. Java editor, and save it as HelloWorld.java Compile it by typing at the command-line: javac HelloWorld.java This creates a Java bytecode file named: HelloWorld.class command-line % javac HelloWorld.java (or click the Compile button in your IDE)

15 Programming in Java Programming in Java. Create the program by typing it into a text editor, and save it as HelloWorld.java Compile it by typing at the command-line: javac HelloWorld.java Execute it by typing at the command-line: java HelloWorld % javac HelloWorld.java % java HelloWorld Hello, World command-line (or click the Run button in your IDE)

Recap of Syntax n A Java program is created in a.java file. n First line defines the class/program and the name. n Name there must match name of.java file n Second line defines main() method. n (For now) your program is set of statements in main(). 16 /******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }

Recap of Syntax n A Java statement ends in a semi-colon. n Sometimes statements are grouped inside { and }. n Curly-brackets must nest properly. n Comments are indicated by // or by /*... */ n How Java treats whitespace. n Indentation and spacing are important of human readers. 17 /******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }

Recap of Creating and Running Java Programs n Java programs (.java files) are compiled. n This creates a.class file. n The class file contains machine instructions. n To run the program, execute the class file. 18

Variables n Every data item used by a program is stored in a variable n Variables also provide a way of communicating to the machine hardware that the data needs to be stored somewhere n A variable has three parts: 1. Name (e.g., “x”) 2. Type (e.g., int, double, String) 3. Value 19

Variables n Variables are declared by the programmer – E.g., int x; n How does a variable get a value? – Can be initialized in the program  E.g., int x = 10 – Can be computed when the program is running  E.g., x = y + z; // y and z are integers too 20 Literal Value

Methods n Methods have a name and invoke some operation or function on some data. – System.out.println("Hello, World!"); n Some methods return a value that can be used. – Math.sqrt(100) n Methods like these are part of Java's Standard Library. 21

A More Complicated Example Program // This is a comment at the top of class/program Demo1 public class Demo1 { public static void main(String[] args) { // program statements go inside method main System.out.println("Hello world!"); int x = 3; System.out.println("The value of variable x is: " + x); x = x + 3; System.out.println("Now the value of variable x is: " + x); // continued…. 22

More Complicated Example Program Continued double someValue = Math.sqrt(x); System.out.println("The sqrt of x is: " + someValue); System.out.println("The sqrt of 100 is: " + Math.sqrt(100)); // we're done! } 23

CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Next Class – Built-In Java Datatypes 24