Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © 2007 1.1 Your First Java."— Presentation transcript:

1 CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © 2007 http://www.cs.Princeton.EDU/IntroCS 1.1 Your First Java Program

2 CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © 2007 http://www.cs.Princeton.EDU/IntroCS Parlez-vous Java? Group Exercise: Form teams of 3 students 2

3 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

4 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

5 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”);

6 CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © 2007 http://www.cs.Princeton.EDU/IntroCS Create, Compile, Execute

7 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 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

9 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

10 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 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"); }

12 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

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

14 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 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)

16 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"); }

17 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"); }

18 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

19 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

20 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

21 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

22 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

23 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

24 CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © 2007 http://www.cs.Princeton.EDU/IntroCS Next Class – Built-In Java Datatypes 24


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

Similar presentations


Ads by Google