More advanced topics for Java

Object oriented programming

Variables, local/instance/class variables

Modifiers

Abstraction/Interface

Examples of using class/object

//define a class named Student
public class Student {
	//name, department are instance variables
	public String name = "unk";
	private String department = "unk";
	public Student(String name) {
		this.name = name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public String getDepartment() {
		return department;
	}
	public void display() {
		System.out.println("Student Information");
		System.out.println("Name: " + name);
		System.out.println("Department: " + department);
	}
	public static void main(String[] argv) {
		//name is a local variable
		String name = "Ray";
		
		//create an instance from the Student class
		Student stu1 = new Student(name);
		stu1.setDepartment("CS");
		
		//create an array of students
		Student[] stuList = new Student[2];
		stuList[0] = stu1;
		stuList[1] = new Student("Ben");
		stuList[1].setDepartment("Biology");
		stuList[0].display();
		stuList[1].display();
	}
}

Getting data from System.in, a local file, or a URL (using Scanner)

Need to import java.io.*; & import java.net.*;

See a sample code (note the throws IOException declaration for handling exceptions)

Three steps for reading from a URL: 1) create a scanner; 2) read data; and 3) close the scanner

        //Step 1: create a scanner

        //From a URL
        URL url = new URL("http://homes.soic.indiana.edu/classes/spring2016/csci/c343-yye/tweet-data-Jan16.txt");
        Scanner in = new Scanner(url.openStream()); 

        //From System.in (user's inputs)
        //Scanner in = new Scanner(System.in);

        //From a local file (e.g., tweet-data-Jan16.txt on your local machine)
        //Scanner in = new Scanner(new FileReader("tweet-data-Jan16.txt"));

        //Step 2: read data
        while (in.hasNext()) {
                //nextLine() reads a line;
                //Scanner class has other methods to allow the user to read values of various types, eg.., nextInt()
                String str = in.nextLine();
        }

        //Step 3: close the scanner
        in.close();