�CC22: Introduction to Java�B.Sc. Honours� Second Semester
Amit Ghosh Roy
Assistant Professor
Department of Computer Science
Sukanta Mahavidyalaya
Date: 08/02/2022
Topics of the Review
SMV_AGR: Introduction to Java
2
Some Salient Characteristics of Java
SMV_AGR: Introduction to Java
3
Java Processing and Execution
SMV_AGR: Introduction to Java
4
Compiling and Executing a Java Program
SMV_AGR: Introduction to Java
5
Classes and Objects
SMV_AGR: Introduction to Java
6
Grouping Classes: The Java API
SMV_AGR: Introduction to Java
7
A Little Example of import and main
import javax.swing.*;
// all classes from javax.swing
public class HelloWorld { // starts a class
public static void main (String[] args) {
// starts a main method
// in: array of String; out: none (void)
}
}
SMV_AGR: Introduction to Java
8
Processing and Running HelloWorld
SMV_AGR: Introduction to Java
9
References and Primitive Data Types
SMV_AGR: Introduction to Java
10
Primitive Data Types
SMV_AGR: Introduction to Java
11
Primitive Data Types
SMV_AGR: Introduction to Java
12
Data type | Range of values |
byte | -128 .. 127 (8 bits) |
short | -32,768 .. 32,767 (16 bits) |
int | -2,147,483,648 .. 2,147,483,647 (32 bits) |
long | -9,223,372,036,854,775,808 .. ... (64 bits) |
float | +/-10-38 to +/-10+38 and 0, about 6 digits precision |
double | +/-10-308 to +/-10+308 and 0, about 15 digits precision |
char | Unicode characters (generally 16 bits per char) |
boolean | True or false |
Primitive Data Types (continued)
SMV_AGR: Introduction to Java
13
Operators
SMV_AGR: Introduction to Java
14
Operators
SMV_AGR: Introduction to Java
15
Type Compatibility and Conversion
SMV_AGR: Introduction to Java
16
Declaring and Setting Variables
square = n * n;
SMV_AGR: Introduction to Java
17
Referencing and Creating Objects
SMV_AGR: Introduction to Java
18
Java Control Statements
SMV_AGR: Introduction to Java
19
Java Control Statements (continued)
SMV_AGR: Introduction to Java
20
Java Control Statements (continued)
SMV_AGR: Introduction to Java
21
Methods
SMV_AGR: Introduction to Java
22
The Class Math
SMV_AGR: Introduction to Java
23
Escape Sequences
SMV_AGR: Introduction to Java
24
The String Class
SMV_AGR: Introduction to Java
25
Comparing Objects
(You will compare the pointers, not the objects!)
SMV_AGR: Introduction to Java
26
The StringBuffer Class
SMV_AGR: Introduction to Java
27
StringTokenizer Class
SMV_AGR: Introduction to Java
28
Wrapper Classes for Primitive Types
SMV_AGR: Introduction to Java
29
Defining Your Own Classes
SMV_AGR: Introduction to Java
30
Class name
Field values
Class name
Field signatures: type and name
Method signatures: name, argument types, result type
Defining Your Own Classes (continued)
SMV_AGR: Introduction to Java
31
The Person Class
// we have omitted javadoc to save space
public class Person {
private String givenName;
private String familyName;
private String IDNumber;
private int birthYear;
private static final int VOTE_AGE = 18;
private static final int SENIOR_AGE = 65;
...
SMV_AGR: Introduction to Java
32
The Person Class (2)
// constructors: fill in new objects
public Person(String first, String family,
String ID, int birth) {
this.givenName = first;
this.familyName = family;
this.IDNumber = ID;
this.birthYear = birth;
}
public Person (String ID) {
this.IDNumber = ID;
}
SMV_AGR: Introduction to Java
33
The Person Class (3)
// modifier and accessor for givenName
public void setGivenName (String given) {
this.givenName = given;
}
public String getGivenName () {
return this.givenName;
}
SMV_AGR: Introduction to Java
34
The Person Class (4)
// more interesting methods ...
public int age (int inYear) {
return inYear – birthYear;
}
public boolean canVote (int inYear) {
int theAge = age(inYear);
return theAge >= VOTE_AGE;
}
SMV_AGR: Introduction to Java
35
The Person Class (5)
// “printing” a Person
public String toString () {
return “Given name: “ + givenName + “\n”
+ “Family name: “ + familyName + “\n”
+ “ID number: “ + IDNumber + “\n”
+ “Year of birth: “ + birthYear + “\n”;
}
SMV_AGR: Introduction to Java
36
The Person Class (6)
// same Person?
public boolean equals (Person per) {
return (per == null) ? false :
this.IDNumber.equals(per.IDNumber);
}
SMV_AGR: Introduction to Java
37
Arrays
SMV_AGR: Introduction to Java
38
Array Example
float grades[] = new float[numStudents];
... grades[student] = something; ...
float total = 0.0;
for (int i = 0; i < grades.length; ++i) {
total += grades[i];
}
System.out.printf(“Average = %6.2f%n”,
total / numStudents);
SMV_AGR: Introduction to Java
39
Array Example Variations
// possibly more efficient
for (int i = grades.length; --i >= 0; ) {
total += grades[i];
}
// uses Java 5.0 “for each” looping
for (float grade : grades) {
total += grade;
}
SMV_AGR: Introduction to Java
40
Input/Output using Class JOptionPane
SMV_AGR: Introduction to Java
41
Input/Output using Class JOptionPane (continued)
SMV_AGR: Introduction to Java
42
Converting Numeric Strings to Numbers
SMV_AGR: Introduction to Java
43
Input/Output using Streams
SMV_AGR: Introduction to Java
44
Opening and Using Files: Reading Input
import java.io.*;
public static void main (String[] args) {
// open an input stream (**exceptions!)
BufferedReader rdr =
new BufferedReader(
new FileReader(args[0]));
// read a line of input
String line = rdr.readLine();
// see if at end of file
if (line == null) { ... }
SMV_AGR: Introduction to Java
45
Opening and Using Files: Reading Input (2)
// using input with StringTokenizer
StringTokenizer sTok =
new StringTokenizer (line);
while (sTok.hasMoreElements()) {
String token = sTok.nextToken();
...;
}
// when done, always close a stream/reader
rdr.close();
SMV_AGR: Introduction to Java
46
Alternate Ways to Split a String
String[] = s.split(“\\s”);
// see class Pattern in java.util.regex
SMV_AGR: Introduction to Java
47
Opening and Using Files: Writing Output
// open a print stream (**exceptions!)
PrintStream ps = new PrintStream(args[0]);
// ways to write output
ps.print(“Hello”); // a string
ps.print(i+3); // an integer
ps.println(“ and goodbye.”); // with NL
ps.printf(“%2d %12d%n”, i, 1<<i); // like C
ps.format(“%2d %12d%n”, i, 1<<i); // same
// closing output streams is very important!
ps.close();
SMV_AGR: Introduction to Java
48
Summary
SMV_AGR: Introduction to Java
49
Contd…
SMV_AGR: Introduction to Java
50