day-1-intro
day-1-intro
====
Day1:
/*
The following program accepts a String as command line argument and reverses it using
charAt() method.
Find the errors, correct it so that when you run it as
Java Fundamentals:
variable
datatype v arName = init_val;
local variable - within the method
data type
prim non-prim/class/ref
---- ---------
byte Array
short String
long ...
int
float
double
char
Boolean
/* The class below should print the string "Bag" when you run the class as
java TestClass
It should extract the characters B a and g from the String Bangalore one by one, concatenate
them and print "Bag".
*/
class TestClass {
public static void main(String[] arg) {
String x="Bangalore";
char c1=x.charAt(0); //'B' ASCII
char c2=x.charAt(1); //'a'
char c3=x.charAt(3); //'g'
System.out.println(c1+""+c2+c3);
}
}
control flow
if
if..else
if..else if..
switch
loop
for
while
do..while
enhanced for
Array
declare
datatype arrName[];
arrName = new datatype[size]
int nums[] = new int[5] // |0|0|0|0|0|
01 2 3 4
String words[] = new String[3] // |null|null|null|
/* Correct the errors in the following program, so that when it is executed, it prints the
following output
12345
*/
Object:
Employee
properties: empid, name, salary,...
behaviours: print, …..
emp:
empid = 100
name = John
salary = 30000
class ClassName
{
properties
behaviours
}
constructor - obj initialization
className(){
}
className(datatype par1,datatype par2,...){
}
this:
curr class obj/ref
package emppkg;
class Employee{
int empid; //instance var
String name; //instance var
int salary; //instance var
Employee(){
}
Employee(int empid, String name, int salary){ //a= b
this.empid = empid;
this.name = name;
this.salary = salary;
}
void print(){ //instance method
System.out.println(empid +" "+name+" "+salary);
}
}
package testpkg;
import emppkg.Employee;
public class TestEmployee{
public static void main(string[] a)
{
Employee emp = new Employee(); // to initialize emp, invoke Employee class
(default) constructor
emp.print();
emp = new Employee(100,"John",30000);
emp.print();
Employee em = new Employee(200,"Smith",40000);
em.print();
}
}
_________
Predict the output:
-----
package demopkg;
final
----
int n = 10;
System.out.println(n) => 10
EmpMS
--------
Employee
empid
name
salary
EmpArray size= 5
addEmp
emp
id must be unique
name should not be null, must have min 2 char
salary should not be less than base salary
updEmp
delEmp
searchEmp
viewallEmp
inheritance
reuse the code
class Parent{
public void speak(){
System.out.println("Parent is speaking");
}
}
class Child extends Parent{
public void sing(){
System.out.println("Child is singing");
}
public void speak(){
System.out.println("Child is speaking");
}
class Tester{
public static void main(String[] a)
{
Parent p = new Parent();
p.speak(); // Parent is speaking
Child c = new Child();
c.speak(); // Child is speaking
c.sing(); // Child is singing
//p.sing(); XXXXX
p = new Child();
p.speak(); // Child is speaking
}
}
polymorphism
abstraction
encapsulation
obj
properties
private
accessors/mutators
public getter/setter methods
Exception
unexpected event - affects normal exec prog
Handle:
try..catch..finally
try{
stm1
stm2
}
catch(Exception ex1)
{
System.out.println("exc message");
}
finally{
}
stm....
}
to raise/throw
throw ref exception
---------------
constr
I/O stream
Wrapper
9.30am
11.am t-break - 15 minutes
1.00pm
2.00pm
3.15 -t-break
4.00
EmpMS
=======
Employee.java
------------
package emp;
}
************************
EmpManager.java
-------------------
package mgr;
import emp.Employee;
import empexc.EmpSalException;
import emp.Employee;
import mgr.EmpManager;
public class MainClass {
}
**********************
tomorrow:
I/O stream - Serialization
Collection
rdbms
sql