Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes: Ch 2: Data and Expressions (1 of 2)
Logistics
Topic Outline:
class MyCode {
public static void main(String[] args) {
System.out.print("This is");
System.out.print("a test");
System.out.println("that will run, albeit");
System.out.println();
System.out.println("with some problems...");
}
}
class MyCode {
public static void main(String[] args) {
System.out.println("a" + "b");
System.out.println("a" + 1 );
System.out.println("a" + 1 + "2");
System.out.println("a" + 1 + 2 );
System.out.println( 1 + 2 + "a");
}
}
class MyCode {
public static void main(String[] args) {
System.out.println(1);
System.out.println(1 + 2);
}
}
class MyCode {
public static void main(String[] args) {
System.out.println("This is a double-quote: \"");
}
}
class MyCode {
public static void main(String[] args) {
System.out.println("These items are tab-delimited, 3-per-line");
System.out.println("abc\tdef\tg\nhi\tj\\\tk\n---");
}
}
a) Declare a variable and use it
class MyCode {
public static void main(String[] args) {
int x = 3;
System.out.println(x);
}
}
b) Same, but with a nicer UI
class MyCode {
public static void main(String[] args) {
int x = 3;
System.out.println("x = " + x);
}
}
c) Use a variable without declaring it
class MyCode {
public static void main(String[] args) {
System.out.println("x = " + x); // ERROR! No such variable as x
}
}
d) Declaring and assigning on different lines
class MyCode {
public static void main(String[] args) {
int x; // declare the variable
x = 5; // initialize it (assign it its first value)
System.out.println("x = " + x);
}
}
e) Assigning and re-assigning
class MyCode {
public static void main(String[] args) {
int x;
x = 1;
System.out.println("x = " + x); // prints "x = 1"
x = 2;
System.out.println("x = " + x); // prints "x = 2"
}
}
f) Using before assigning a value
class MyCode {
public static void main(String[] args) {
int x; // declared, but not assigned a value
System.out.println("x = " + x); // ERROR! x is not initialized
}
}
g) Using two variables
class MyCode {
public static void main(String[] args) {
int x = 1;
int y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
h) Declaring two variables on one line
class MyCode {
public static void main(String[] args) {
int x = 1, y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
i) Using two variables in an expression
class MyCode {
public static void main(String[] args) {
int x = 1, y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("x + y = " + (x + y));
}
}
j) Same, but with a nicer UI
class MyCode {
public static void main(String[] args) {
int x = 1, y = 2;
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("x + y = "
+ x + " + " + y
+ " = " + (x + y));
}
}
a) Reading an "int" variable from the console
import java.util.Scanner; public class ReadFromConsole { public static void main(String[] args) { // declare local variables Scanner scanner = new Scanner(System.in); int x; // get the input (with a suitable prompt) System.out.print("Enter an integer: "); x = scanner.nextInt(); // display result System.out.println("You entered: " + x); } }
b) Reading an "int" variable from a String
import java.util.Scanner;
public class ReadFromString {
public static void main(String[] args) {
// declare local variables
Scanner scanner = new Scanner("5"); // simulated input in a string!
int x;
// get the input (with a suitable prompt)
System.out.print("Enter an integer: ");
x = scanner.nextInt();
// display result
System.out.println("You entered: " + x);
}
}
c) Reading an "int" variable from a file
import java.util.Scanner; import java.io.File; public class ReadFromFile { public static void main(String[] args) throws Exception { // declare local variables Scanner scanner = new Scanner(new File("SampleFile.txt")); int x; // get the input (with a suitable prompt) System.out.print("Enter an integer: "); x = scanner.nextInt(); // display result System.out.println("You entered: " + x); } }
d) Writing to a file
import java.io.File; import java.io.PrintStream; public class WritingToAFile { public static void main(String[] args) throws Exception { PrintStream out = new PrintStream(new File("SomeFile.txt")); out.println("This will output to the file 'SomeFile.txt'"); } }
a) Overflow
class MyCode {
public static void main(String[] args) {
int x = 1234567890; // about 1.2 billion
int y = 1355779246; // about 1.3 billion
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("x + y = "
+ x + " + " + y
+ " = " + (x + y));
// Prints: x + y = 1234567890 + 1355779246 = -1704620160
}
}
b) Integer division
class MyCode {
public static void main(String[] args) {
System.out.println("20/3 = " + (20/3));
System.out.println(" 6/3 = " + ( 6/3));
System.out.println(" 5/3 = " + ( 5/3));
System.out.println(" 2/3 = " + ( 2/3));
}
}
c) The modulus or remainder operator (%)
class MyCode {
public static void main(String[] args) {
System.out.println("20%3 = " + (20%3));
System.out.println(" 6%3 = " + ( 6%3));
System.out.println(" 5%3 = " + ( 5%3));
System.out.println(" 2%3 = " + ( 2%3));
System.out.println(" 0%3 = " + ( 0%3));
}
}carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem