Download Prints "Hello, World" - Computer Science at UVA

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1.1 Your First Java Program
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Parlez-vous Java?
Group Exercise:
Form teams of 3 students
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
2
Code Snippet 1
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
3
Code Snippet 2
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
4
Code Snippet 3
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”);
Checks whether “n” is prime
Program will print YES
5
Create, Compile, Execute
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
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
A Rich Subset of the Java Language
Built-In Types
System
Math Library
int
double
System.out.println()
Math.sin()
Math.cos()
long
String
System.out.print()
Math.log()
Math.exp()
char
boolean
System.out.printf()
Math.sqrt()
Math.pow()
Math.min()
Math.max()
Math.abs()
Math.PI
Flow Control
Parsing
if
else
Integer.parseInt()
for
while
Double.parseDouble()
Boolean
Punctuation
Primitive Numeric Types
+
-
*
/
%
++
true
false
{
}
--
>
<
||
&&
(
)
<=
>=
==
,
;
!=
!
String
Arrays
Objects
+
""
a[i]
class
static
length()
compareTo()
new
public
private
charAt()
matches()
a.length
toString()
equals()
new
main()
8
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
Programming in Java
/*******************************************
* Prints "Hello, World"
* Everyone's first Java program.
*******************************************/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
HelloWorld.java
11
Using an Interactive Development Environment (IDE) to
Create, Compile, Execute






An IDE is a software tool that:
Includes a “smart” editor for your language
Lets you compile all your Java from within the tool
Lets you run the compiled program from within the tool
Supports debugging
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?
Instructions for installing Java and Mac are
available on Collab slides page
13
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
command-line
% javac HelloWorld.java
(or click the Compile button in your IDE)

This creates a Java bytecode file named: HelloWorld.class
14
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
command-line
% javac HelloWorld.java
% java HelloWorld
Hello, World
(or click the Run button in your IDE)
15
Recap of Syntax
/*******************************************
* Prints "Hello, World"
* Everyone's first Java program.
*******************************************/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}





A Java program is created in a .java file.
First line defines the class/program and the name.
Name there must match name of .java file
Second line defines main() method.
(For now) your program is set of statements in
main().
16
Recap of Syntax
/*******************************************
* Prints "Hello, World"
* Everyone's first Java program.
*******************************************/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}






A Java statement ends in a semi-colon.
Sometimes statements are grouped inside { and }.
Curly-brackets must nest properly.
Comments are indicated by // or by /* ... */
How Java treats whitespace.
Indentation and spacing are important of human
readers.
17
Recap of Creating and Running Java Programs




Java programs (.java files) are compiled.
This creates a .class file.
The class file contains machine instructions.
To run the program, execute the class file.
18
Variables



Every data item used by a program is
stored in a variable
Variables also provide a way of
communicating to the machine hardware
that the data needs to be stored
somewhere
A variable has three parts:
1.
2.
3.
Name (e.g., “x”)
Type (e.g., int, double, String)
Value
19
Variables


Variables are declared by the programmer
– E.g.,
int x;
How does a variable get a value?
– Can be initialized in the program
Literal Value
 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
Methods

Methods have a name and invoke some
operation or function on some data.
– System.out.println("Hello,

World!");
Some methods return a value that can be
used.
– Math.sqrt(100)

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
Next Class – Built-In Java Datatypes
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
24