Download Slides-Sept 7 - Allegheny College Department of Computer Science

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
Introduction to Computer Science I
Introduction to Programming
Janyl Jumadinova
September 7, 2016
Programming in Java
I
Java is an object-oriented programming language
2/11
Programming in Java
I
Java is an object-oriented programming language
I
Objects are fundamental elements that make up a program
2/11
Programming in Java
I
Java is an object-oriented programming language
I
Objects are fundamental elements that make up a program
I
Java has a library of software, called Java API, that is available
for your use
2/11
Java program development process
3/11
Simple first Java Program: “Hello World”
// This is the first program people write in a new language,
// the "Hello World!". In Java, this file must be named
// Welcome.java, with the first part of the name, Welcome, being
// the same as the name of the class. The filename itself
// (not the class name) must always end in .java to indicate
// to the operating system that it’s a java source file.
public class Welcome
{
public static void main ( String args[] )
{
System.out.println ( "Hello World!" );
}
}
4/11
Comments
Comments in Java can be one of three styles:
I
Single line: starts at // anywhere on a line, ends at the end of
that line
I
Multi-line: starts with character sequence /* anywhere, ends
with character sequence */ anywhere after that can span
multiple lines
I
javadoc: starts with character sequence /** anywhere, ends
with character sequence */ anywhere, after that uses javadoc
utility to create HTML documentation from code
5/11
I
public class Welcome:
I
I
public means that something is available across packages
(reserved word)
Name of the class has to be the same as the name of the .java
file
6/11
I
public class Welcome:
I
I
I
public means that something is available across packages
(reserved word)
Name of the class has to be the same as the name of the .java
file
public static void main ( String identifier[] ):
I
I
I
I
The particular form of main is required by Java.
JVM starts executing here!
main is a static method, it is part of its class and not part of
objects.
Strings in Java are sequence of characters
6/11
I
public class Welcome:
I
I
I
public static void main ( String identifier[] ):
I
I
I
I
I
public means that something is available across packages
(reserved word)
Name of the class has to be the same as the name of the .java
file
The particular form of main is required by Java.
JVM starts executing here!
main is a static method, it is part of its class and not part of
objects.
Strings in Java are sequence of characters
Braces { } are used to collect statements into a ”block”
6/11
I
public class Welcome:
I
I
I
public means that something is available across packages
(reserved word)
Name of the class has to be the same as the name of the .java
file
public static void main ( String identifier[] ):
I
I
I
I
The particular form of main is required by Java.
JVM starts executing here!
main is a static method, it is part of its class and not part of
objects.
Strings in Java are sequence of characters
I
Braces { } are used to collect statements into a ”block”
I
Statements in Java end with semicolons.
6/11
Printing
I
println: New line after printing
I
print: No new line
I
printf: Can specify format - may learn this later
7/11
Character Strings
string literal in class String
“ABC”
“This is interesting”
“”
“91”
8/11
Character Strings
string literal in class String
“ABC”
“This is interesting”
“”
“91”
I
Use print or println methods to print a character string to the
terminal
I
System.out.println(“CMPSC 111”);
I
the string “CMPSC 111” is a parameter: data sent to a method
8/11
String Concatenation
appending one string to the end of another: use + operator
“This is ”+ “interesting”
“Your grade is ”+ “91”
9/11
String Concatenation
appending one string to the end of another: use + operator
“This is ”+ “interesting”
“Your grade is ”+ “91”
I
+ is also used for arithmetic addition
I
System.out.println(”Adding ”+ 12 + 23); is not the same as
System.out.println(”Adding ”+ (12 + 23));
9/11
Escape Sequences
I
Escape sequences, or escape characters, begin with a slash and
are immediately followed by another character.
I
This two-character sequence, inside “ ” allows you to control
your output (\n, \t, \b) or output characters you wouldn’t
otherwise be able to (\\, \”) inside a string.
10/11
Escape Sequences
Seq
\n
\t
\b
\\
\”
Meaning
New line
Horizontal tab
Backspace
Backslash
Double quote
Example Code
System.out.println(”Hi\nThere”);
System.out.println(”What’s\tup?”);
System.out.println(”Hi\b Hey”);
System.out.println(”Back\\Slash”);
System.out.println(”Dbl\”Quote”);
11/11