Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
USACO WORKSHOP – Department of Computer Science
Pace University
USACO Workshop - Day 1.
Outline
Time: 3 hours
Instructor: …
Goal:
1. Learn the structure of Java program for USACO
2. Learn how to read from file and write to file
3. Learn primitive data type
4. Learn common operators
5. Skill training section
Content:
Goal 1:
Basic Hello World Program (refer to Section 3.1)
public class USACO {
public USACO(){
// TODO Auto-generated constructor stub
System.out.println("Hello World");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new USACO();
}
}
Goal 2: (Part I)
Read from file “usaco.in” and write to file “usaco.out”
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class usaco {
public usaco() throws Exception {
// TODO Auto-generated constructor stub
Scanner s = new Scanner(new File("usaco.in"));
String line = s.nextLine();
s.close();
System.out.println(line);
PrintWriter p = new PrintWriter("usaco.out");
p.println(line);
p.close();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new usaco();
}
}
1
USACO WORKSHOP – Department of Computer Science
Pace University
Goal 3:
int: 1,3,4,…
double: 3.14, 1.1, 5.0 …
char: 'a', 'A', '1', '?'…
Boolean: true, false
Practice 1.1
Compare 3, 3.0, '3' (3 is an int; 3.0 is a double; '3' is a char)
Skill 1.1
Data type can assist computers to find out the errors in your program.
e.g. Math.sqrt("hello"); // this cannot be operated
Goal 2: (Part II)
Read int and double data from file “usaco.in” and write them to file
“usaco.out”
public class usaco {
public usaco() throws Exception {
// TODO Auto-generated constructor stub
// Assume “usaco.in” contains “10 25.15 Hello”
Scanner s = new Scanner(new File("usaco.in"));
String line = s.nextLine();
2
USACO WORKSHOP – Department of Computer Science
Pace University
s.close();
// Split input line into tokens separated by space
String[] token = line.split(" ");
// Convert token[0]== "5" into integer 5
int i = Integer.parseInt(token[0]);
// Convert token[1]== "25.15" into double 25.15
double d = Double.parseDouble(token[1]);
PrintWriter p = new PrintWriter("usaco.out");
//write 3 1.05 Hello; the successive outputs are on the same line
p.print(i+" " +d+" "+token[2]+" ");
//write 2; the successive outputs will be on the next line
p.println(2);
p.close();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new usaco();
}
}
Goal 4:
Java Operators (basic) (refer to Section 3.5)
Arithmetic Operators
+, -, *, /, % (mod)
Compare Operators
<, >, >=, <=, !=, ==
Logic Operators
&& (and), || (or), ! (not)
Assignment Operators
=, +=, -=,…
Note: 1. int/int = int double/int=double int/double=double
2. == (is equal to) = assignment
Practice 1.2
Arithmetic Operators: 1+5 =? 1+5.0 =? 2×10 =? 22/10 =? 22/10.0 =?
52%10 =? 52/10.0 =? 'a'+1=? (Refer to Appendix 1)
Compare Operators: 111<1111 2>5.0 3!=3.0 'a'<'b' 'a'<'A' 'a'=='A'
Logic Operators: true && false 111<1111 && 2>5.0
Assignment: i=3 i=3+5
Solution to Arithmetic, Compare, and Logic
public class practice102 {
public practice102() {
// TODO Auto-generated constructor stub
System.out.println(111<1111); //put expression into the brackets
}
public static void main(String[] args) {
// TODO Auto-generated method stub
3
USACO WORKSHOP – Department of Computer Science
Pace University
new practice102();
}
}
Solution to Assignment
public class practice102 {
public practice102() {
// TODO Auto-generated constructor stub
int i;
i=3+5;
System.out.println(i);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new practice102();
}
}
Goal 5: Skill training section
Solve the following question by using Java.
There is a set of capital letter from A to Z. A is 1, B is 2, C is
3,…, and Z is 26.
Your mission:
Use Java to find the number with the letter that is read from
training1.in.
Solution:
import java.io.File;
import java.util.Scanner;
public class training1 {
public training1() throws Exception {
// TODO Auto-generated constructor stub
Scanner s = new Scanner(new File("training1.in"));
String line = s. nextLine();
s.close();
System.out.println(line.charAt(0)-'A'+1 );
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new training1();
}
}
4
USACO WORKSHOP – Department of Computer Science
Pace University
Appendix 1. Basic ASCII (American Standard Code of Information Interchange)
Table (All data stored in the computer should be binary numbers so that
all chars should be transferred into numbers. For example, in Practice 1.2,
'a'+1, Java transfers 'a' into int that is 97. So the result of 'a'+1
= 98)
5