DSA Refresher
Module
Introduction
Course Structure
• 2 credits Course
• Class Timings: 11:00-12:30pm(Mon-Fri)
• Lab. Timings: 2:00-4:00pm(Mon-Fri) (Home Assignments will be given)
• Course Classroom code: m7fd3ha
Course Structure
• Java refresher
– Classes and Objects, Strings
• DSA refresher module
– Data structures
– Run Time analysis
– Sorting algorithms
– Graphs and Trees
Evaluation
• Homework Programming Assignments : 30% (2 x 15% )
• Quiz : 20% (3 )
• Final Exam : 30%
• Final Lab Test : 20%
Plagiarism Policy
Follow institute policy
– First instance of cheating – one letter grade reduction
– Second instance of cheating – F in the course.
Tasks
• Register (Join) for the course on Classroom page.
Contact
• Email id: bhawna@iiitd.ac.in
• Office hours
– Thursday: 2-4pm
– Location: Google Meet
• Teaching Assistants (TAs) allotted
shivank20096@iiitd.ac.in Shivank Agrahari
vasudev19126@iiitd.ac.in Vasudev Singhal
harshit20009@iiitd.ac.in Harshit Singh Chhabra
atif20306@iiitd.ac.in Atif Abdus Samad
saksham19385@iiitd.ac.in Saksham
gaurav20112@iiitd.ac.in Gaurav
pragya20309@iiitd.ac.in Pragya Pal
Introduction to
Java
11 Buzzwords for
• Simple • Portable
• Object Oriented • Interpreted
• Network Savvy • High performance
• Robust • Multithreaded
• Secure • Dynamic
• Architecture
Neutral
Java:
• Syntax is cleaned up version of C++
• No need of header files
• No pointer arithmetic/syntax
• No operator overloading
Java: Object
• Object Oriented Design: Focus on Data (objects) and interfaces.
• Object oriented features comparable to C++
• More simplified because of different handling of
multiple inheritance using ‘interfaces’.
Java: Network Savvy
• Much easier network programming compared to C++
• Simpler remote method invocation mechanism.
Java:
• No pointer model: Eliminates possibility of overwriting
memory or corrupting data
• Improved compiler: Detects problems that wouldotherwise
show at runtime in other languages
Java: Secure
• Stack overrun, Memory corruption andReading/Writing
files without permission are much more difficult inJava.
Java: Architecture Neutral and
• Java Virtual Machine
• Sizes of primitive data types are specified
Java: High Performance
● Just in time compilation.
● The bytecodes translated on the fly (at runtime)
into machine code for the particular CPU the
application is runningon.
Java:
• C++: Different multi-threading models exist for different
operating systems,
• Java: Consistent API. Different implementations for JVMs
Java: Dynamic
• Support for libraries etc.- adding new methods to libraries
without affecting client
• Possible to find runtime type information.
Java Programming
• JDK Java
• JRE Java Compiler
(JDK)
Byte Code
• .java
Java Loader
• .clas (JRE)
s Window Linux Mac
s WORA: Write Once
• .jar JVM JVM Run Everywhere
We will use JDK 1.8.0_45 in this course
Java Programming
• IDE: Eclipse, Netbeans, Intellij etc.
• In this course, we will use Intellij
• Version: Mars
• Release: 4.5.0
• Build id: 20150621-1200
• For developing/compiling/running from command line use
‘javac’ and ‘java’ commands
Your First Java Program: Hello
public class MainClass { Note:
public static void main(String[] args) { 1. Naming Convention
//comment 1 a. Class
b. File
/*
1. Braces
* more comment formats 2. Comments
*/ 3. Console output
System.out.println("Hello World");
Ignore for now:
}
1. Class
} 2. Access Modifier
Java Variable
• Java is a strongly typed language
• Every variable must have a type.
• Restrictions on types intermixing: string can not added to a int or double.
• Java basic types: Platform independent
• Integers: int(4 bytes), short(2 bytes), long(8 bytes), byte(1 bytes)
• Reals: float(4), double(8) .
• Default is double precision. Use ‘f’ for floats:1.234f
• Boolean: boolean (true/false)
• Character: char (Unicode: UTF 16)
Java Variable
• Special support for character strings via the java.lang.String class.
• Enclosing your character string within double
quotes will automatically create a new String object;
String s = "my string object";
• String objects are immutable: once created, their values
cannot be changed
Java
• Basic: +, -, *, /
• Incremental: ++, --
• Difference between b = a++ and b = ++a
• Combined: +=, -=, *=, /=
• Remainder (Mod): %
• Relational: <, >, >=, <=, ==, !=
• Logical: ||, &&
• Bitwise: |, &, ^ (XOR)
Sample
public class MainClass {
public static void main(String[] args) { int a = 10;
a+ +;
float f = 20.0f;
++f ;
System.out.printf("int: %d, float: %.3f",a, f);
//We almost never use this function ‘printf’
}
}
Type
• Implicit conversion
• Conversion of value from one type to another
without any directive from the programmer.
• Example: char to int implicit conversion
char c = 'a'; int k = c; long x = c;
• Explicit conversion
• Casting double d = 5.6; int k = (int)d;
Operator
Operators Precedence
!, ++ ,-- (unary operators) First (Highest)
*, /, % Second
+, - Third
<, <=, >=, > Fourth
==, != Fifth
&& Sixth
|| Seventh
= (asignment operator) Last(lowest)
Scop
Block Scope
• Java statements surrounded by a pair ofbraces
• Define the scope of your variables
p u b l i c static vo i d ma i n ( S t r i n g [ ] a r g s ) p u b l i c static vo i d mai n( St r i ng[ ] ar gs )
{ {
int n; int n;
. . . . . .
{ {
int k; int k;
. . . i n t n ; / / ERROR--can't redefine
} / / k i s only defined u p to here n in i n n e r b l o c k
} . . .
}
}
Control Statements: If-else
if (yourSales > = 2 * target)
{
performance = "Excellent";
}
else if (yourSales > = target)
{
performance = "Satisfactory";
}
else
{
S y s t e m . o u t . p r i n t l n ( " Yo u ' r e f i r e d " ) ;
}
Control Statements: Switch case
p u b l i c c l a s s Te s t {
p u b l i c static vo i d m a i n ( S t r i n g args[]){ c h a r grade = 'C';
s wi t c h ( g r a d e )
{
c a s e ' A ' : S y s t e m . o u t . p r i n t l n ( " E x c e l l e n t ! " ) ; break;
case 'B' :
c a s e 'C' : S ys t e m. o u t . pr i n tl n ( "Wel l d o n e " ) ; b r e a k ;
c a s e 'D' : S ys t e m. o u t . p r i n tl n ( " Yo u p a s s e d " ) ;
c a s e ' F ' : S y s t e m . o u t . p r i n t l n ( " B e t t e r t r y a g a i n " ) ; break;
default :System.out.println("Invalid grade");
}
}
}
Control Statements: while, do-while
while (balance < goal) do
{ {
balance += payment; balance += payment;
double double
interest=balance*interestRate/100; interest=balance*interestRate/100
; b a l a n c e + = interest;
b a l a n ce+= interest;
ye a r + + ;
years++;
// print current balance
}
. . .
S ys t e m. o u t . pr i nt l n( ye ar s + " years."); / / a s k i f r e a d y to retire a n d g e t input
. . .
}
wh i l e ( i n p ut.e qual s(" N"));
Control Statements: for
f o r ( i n t i = 10; i > 0; i - - )
{
System.out.println("Counting d o w n . . . " +i);
}
S ys t e m . o u t . p r i n t l n ( " Ti m e up!"); Scope of i ?
int i;
f o r ( i = 10; i > 0 ; i - - )
{
System.out.println("Counting d o wn . . . " + i ) ;
}
Scope of i ?
S ys t e m . o u t . p r i n t l n ( " Ti m e up!");
Strings in
• operations on strings
public class StringDemo {
p u b l i c static vo i d m a i n ( S t r i ng a r g s [ ] ) {
S trin g p a l i n d r o m e = " D o t saw I was To d " ;
i n t len = palindrome.length();
System.out.println( "String Length i s : " + len ) ;
}
}
Array
• 1D, 2D array(array of arrays)
• length P u b l i c c l a s s Te s t A r r a y {
p u b l i c static vo i d ma i n ( S t r i n g [ ] a r g s ) {
• Array class d o u b l e [ ] m yL i s t = {1.9, 2 . 9 , 3.4, 3.5};
• Forloop on arrays
/ / P r in t a l l th e a rra y e le m e n ts fo r
(double element: myList) {
System.out.println(element);
}
}
}
Input output from/to
• Scanner public class InputTest
{
• Print, println p u b lic static v o i d m a in (S t rin g[ ] a r g s )
{
Scanner in = new Scanner(System.in);
/ / get f i r s t input
S ys t e m . o u t . p r i n t ( " W h a t is y o u r name?");
S t r i n g name = i n . n e x t L i n e ( ) ;
/ / get second input
S y s te m .o u t.p rin t(" H o w o ld a re y o u ? " ) ;
i n t age = in.nextInt();
/ / display output o n console
S y s t e m . o u t . p r i n t l n ( " H e l l o , " + name + " . Next y e a r, yo u 'll be "
+ (age + 1 ));
}
}
Functions in
public class MainClass { ● Ignore accessmodifier
● Parameter Passing
● return
public MainClass() {
}
public int Mult(int a, int b ) {
return a*b;
}
public static void main(String[] args) {
M ainClass m = new MainClass();
S ys t e m . o u t . p r i n t l n ( “ F u n c t i o n o u t p u t = ” + m . M u l t ( 1 0 , 20));
}
}