Download Java TA - CE Sharif

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
Java TA
Session 1
Software
• Java Runtime Environment (JRE)
• java.exe
• Java Development Kit (JDK)
• java.exe, javac.exe
• Version 1.6 = Version 6, Version 1.7 = Version 7
• Edit System Variables:
• JAVA_HOME=C:\Program Files\Java\jdk1.6.0_32
• PATH=[…];C:\Program Files\Java\jdk1.6.0_32\bin
• Editor: Notepad++, gedit
Java Startup
• Open Test.java
class Test {
public static void main(String[] args) {
System.out.println("Salam");
}
}
• Run javac:
• C:\Users\user\Documents>javac Test.java
• Run java:
• C:\Users\user\Documents>java Test
• Salam
Errors
C:\Users\user\Documents>javac Add1.java
Add1.java:3: cannot find symbol
symbol : class Scanner
location: class Add1
Scanner s = new Scanner(System.in);
^
Add1.java:3: cannot find symbol
symbol : class Scanner
location: class Add1
Scanner s = new Scanner(System.in);
^
2 errors
Add Import statement
import java.util.Scanner;
class Add1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt(), b = s.nextInt();
int c = a+b;
System.out.println("Sum is " + c);
}
}
What to Import?
• You can read the Java API Specification at:
• http://docs.oracle.com/javase/6/docs/api/
• Use search engines to find what you need:
• For example you can use the following query to
search for “scanner” in the Java 6 API, using
google:
scanner site:docs.oracle.com/javase/6/docs/api/
Functions
• Use functions declared as public and static:
public static int sum(int x, int y)
• Rewriting the previous code with a function:
import java.util.Scanner;
class Add1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
int c = sum(a, b);
System.out.println("Sum is " + c);
}
public static int sum(int x, int y) {
return x+y;
}
}
Related documents