JAVA BATCH CLASS 1
NOTES THE DEBUGGING
SOCIETY
What does a computer do?
Basically it takes an input ,processes it and produces some output.
What is a Program?
Sequence of instructions that tells a computer how to perform a task.
-> Computer operates using binary code but we can’t write our code in binary so we
developed concept of language like C++,java,Python etc.Every language has some
grammatical rules .These grammatical rules are known as “SYNTAX”.
What is a compiler?
The compiler converts code written in human reading programming language into
machine code representation which is understood by the processor.
What is a linker?
Once the compilation step is done ,another step is needed to create a working executable
that can be invoked and run, that is, associate the function calls (for example) that your
compiled code needs to invoke in order to work.To link some code in other libraries to
your written code is what a linker does.Basically a linker combines these object code
files into an executable.
What is an Algorithm ?
Self contained step by step set of operations to be performed in order to solve a
problem.
Ways to represent an Algorithm.
->Flowcharts
->Pseudo Code
->Code (using c++,java etc)
Two basic concepts of programming
1) Data -> We need variables(envelopes/bucket) to store our data.
2) Instructions
Operations on a variable
Create ,Put some information,Get
Six Basic computer instructions
->Reading/Receiving some information
->Outputting/Printing some information
->Performing arithmetic operations
->Assigning a value to variable
->Conditional Execution (If ,If Else,Else)
-> Repeat a set of actions (Loops)
Notations for Flowchart ‘
Homework
1)Given N, print all numbers from 1 to N.
2) Take 5 numbers and print their average.
3) Given N, find sum of even numbers from 1 to N.
4) Check if N is prime or not (Use modulo operator discussed in class)
Starting with JAVA
import java.util.*;
import is a directive processed by preprocessor which is program invoked by
compiler. So basically this tells compiler to include a file java.util
java.util - Contains the collections framework, legacy collection classes, event model,
date and time facilities, internationalization, and miscellaneous utility classes (a string
tokenizer, a random-number generator, and a bit array).
public static void main(String[] args)
Returns void. Execution of every JAVA program begins with main() function,no
matter where it is located.
Every statement is terminated with a semicolon.
DATA TYPES
byte,int,long (to hold integer )
float,double(to hold decimal values)
char (to hold characters)
bool (TRUE/FALSE 1 for true and 0 for false)
/ -> to give single line comments
/*
Multiline comments
*/
Note: Commenting is very important part of software development
Now we will discuss various operations
1) Arithmetic Operations (/,*,+,-)
a) Binary (On two operands) (a+b)
b) Unary (On a single operand e.g a++, a--)
a++ is equivalent to a=a+1
a-- is equivalent to a=a-1
% operator gives us the remainder. E.g 5%2= 1;
Here we will discuss post increment and pre increment.
a++ (Post increment) its value is preserved temporarily until the execution of this
statement and it gets updated before the execution of the next statement
++a (Pre increment) value is incremented instantly.
Sample Programs to understand the difference
1.public class testSpace {
public static void main(String[] args) {
Int i=10;
System.out.println(i++); //Output will be 10
System.out.println(i); //Output will be 11 i.e basically after semicolon value is
incremented
}
}
2.public class testSpace {
public static void main(String[] args) {
Int i=10;
System.out.println(++i); //Output will be 11
}
}
2) Relational operations (<, > , <=, >= )
int a=10; //Assigning a value of 10 to a
int a=10;
int b=10;
if(a==b) { // To check if a is equal to b we use “==”
System.out.println(“ARE EQUAL”);
Note : a+=1 is equivalent to a=a+1;
Similarly a-=1, a*=2
Logical operations
AND (&&) When two statements both are true only then it will give true (Similar To AND
GATE which we studied in class 12)
OR (||)
NOT (!)
Decision making in JAVA
If -
if(condition) {
//execute if condition is true
}
IF ELSE
If (condition) {
}
Else {
//If above condition not satisfied
}
IF ELSE IF
if(condition ){
}
else if(condition ){
}
And so on ……
LOOPS
FOR LOOP
or (initialization expr; test expr; update expr)
{
/ body of the loop
/ statements we want to execute
}
WHILE LOOP
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
DO WHILE
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);
SAMPLE PROGRAMS
1) To calculate simple interest
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int p=scn.nextInt();
int r=scn.nextInt();
int t=scn.nextInt();
System.out.println(“SI = ” + ((p*r*t)/100) );
2) Print first 10 number of Arithmetic Progression
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int a=scn.nextInt();
int d=scn.nextInt();
for(int i=0;i<10;i++){
System.out.println( a+(i*d) );
}
If we chose while loop then
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int a=scn.nextInt();
int d=scn.nextInt();
int i=0;
while(i<10){
System.out.println( a+(i*d) );
i++;
3) Largest of 3 Numbers (All 3 are unique numbers)
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int a=scn.nextInt();
int b=scn.nextInt();
int c=scn.nextInt();
if(a>b && a>c){
System.out.println( a + “ is greatest” );
}
else if(b>a && b>c){
System.out.println( b + “ is greatest” );
else if(c>b && c>a){
System.out.println( c + “ is greatest” );
4) Printing pattern 1(for n)
**
***
****
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int nst=1;
for(int i=0;i<n;i++){
for(int cst=1;cst<=nst;cst++){
System.out.print(“*”);
}
System.out.println();
nst++;
5) Pattern 2(for n)
*****
****
***
**
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int nst=n;
int nsp=0;
for(int i=0;i<n;i++){
for(int csp=1;csp<=nsp;csp++){
System.out.print(“ ”); }
for(int cst=1;cst<=nst;cst++){
System.out.print(“*”);
System.out.println();
nst--;
nsp++;
6) Pattern 3(for n)
***
*****
***
import java.util.scanner;
public class testSpace{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int nst=1;
int nsp=n/2;
for(int i=0;i<n;i++){
for(int csp=1;csp<=nsp;csp++){
System.out.print(“ ”);
for(int cst=1;cst<=nst;cst++){
System.out.print(“*”);
System.out.println();
If(i<n/2){
nst+=2;
nsp--;
else{
nst-=2;
nsp++;