ICSE Java


For any type of consultation, query or doubt. You may contact to the following:
(+91) 9804 436 193
debabrataguha20@gmail.com 

and join the group 
https://www.facebook.com/groups/331231161093613/


1.      After completing my course you would be able to complete your academic syllabus. But while learning, do not think about the academic syllabus. You just need to trust me. This is made for the people having zero experience in coding world.

2.      It would be quick, if you read all the lines without thinking anything else or about any book suggested by your board or university. After 1 month ( yes!! just 1 month) you would be able to read your board/university suggested books & you would be able to understand everything and can do programming by your own.

3.      I have written few lines in the chapters rather putting all the theories in one shot. So after reading the chapter quickly, start solving the exercise. You have to solve the exercise in Java compiler. If you need any help for the installation of Java in your smart phone or in your computer, just send me a mail to my mail id.




You have only 5 things to learn in this chapters. After that, you will be able to solve all the problems where we do not need for loop or if statement.

1.1 First Thing to learn:

Let’s start with some problems in the real life world. Like if I ask you what is your height and suppose you give me the answer like 6 feet. Now If I ask you that I need the length in cm, because I have to fill an army examination form where it
 is mandatory to give your height in cm . So how can you give me the length in cm?

Obvious answer is to search it in google. There are multiple small apps which can convert a length in foot to centimeter equivalent. And the answer would be 182.88 cm. You just need to write “foot to cm converter” in google search engine.



Now write 6 in 1st box and in another box the centimeter equivalent will automatically come.


 So my question is now , how does google transform the length from foot to cm. Or what about those small apps which can convert temperature from Centigrade to Fahrenheit and lots more. So We will start learning how simply we can write this logic by our own.

In Java, memorize 1st 2 lines. For next few chapters we would copy these lines without knowing the meaning. Later we would learn the meaning of following lines:

class example
{
            public static void main (String args[])
{

}
}

Remember ,

·        Java is case sensitive. The word “String” would start with a capital S.

·        Instead of chapter1_program1, you can give any name. You can put your name as well. But the name should not contain any space.

·        Lines inside public static void main() are terminated by  a semicolon ( ; )


Now If I want to add 2 numbers and print the sum, I will write the following code in java:

class chapter1_program1
{
            public static void main (String args[])
            {
                        int a,b,c;
                        a=10;
                        b=5;

                        c=a+b;

                        System.out.println(c);
}

}



Here we are working with 3 variables or boxes. 1 variable or box can hold any value inside. For example, box “a” is holding 10 , variable “b” is holding 5 and initially the variable “c” is not having any value. We would calculate the value of “c” at the runtime.

 ðŸ–µ      🖵      🖵
   a=10              b=5                c


while running the program, java is putting the sum ( means 15 ) into the variable “c”. 

Another thing, we are mentioning “ int a,b,c “,  just because of initially we will handle integer values only like 1,2,3,4,0,-1,-2 etc , not any decimal values like 1.5, 1.3389 etc.


Now, System.out.println() line basically prints something in the screen when you execute/run the program.


If we write , System.out.println(“hi”) it would print “hi” in the screen.

If we write , System.out.println(“Debabrata”) it would print “Debabrata” in the screen.

If we write , System.out.println(“Debabrata Guha”) it would print “Debabrata Guha” in the screen.

If we write , System.out.println(“c”) it would print “c”

If we write , System.out.println(c) without quotation , it would print the value of c, that is “15” in above program.

If we write , System.out.println(“hi”+c) it would print “hi15”

So If we write , System.out.println(“answer is : ”+c) it would print “answer is : 15”


Now moving into our 2nd program,

If I ask you to take 2 numbers as input and show the addition and subtraction of them.



You can write the following program:

class chapter1_program2

{
            public static void main (String args[])
{
                        int a,b,c,d;
                        a=10;
                        b=5;

                        c=a+b;

                        System.out.println(c);

                        d=a-b;

                        System.out.println(d);
}

}

You can also write it in an alternative way like below:


class chapter1_program2_alternative 
{
            public static void main (String args[])
{
                        int a,b,c;
                        a=10;
                        b=5;

                        c=a+b;

                        System.out.println(c);

                        c=a-b;

                        System.out.println(c);
}
}


Here in the alternative way we are calculating the sum and storing it into “c” and printing it , again we are calculating the difference and storing it inside “c” and printing it back.

Now, suppose we have a temperature in centigrade and we want the equivalent temperature in Fahrenheit. In this case we have to use the old physics formula as below:


c/5=(f-32)/9

or, 9c/5=f-32

or, f=(9c/5)+32



class chapter1_program3 
{
            public static void main (String args[])
{

                        int c,f;
                        c=100;

                        f=(9*c/5)+32;

                        System.out.println(c+” Centigrade = “+f+” Fahrenheit”); 
}
}



So the output would be “100 centigrade= 212 Fahrenheit

And please be noted, in computer for multiplication you have to use “*” and for division you have to use “/”.







1.2   Second Thing to learn:

Now if we wish to make it more real time like the google app we saw earlier “Foot to cm converter”. We need to allow the user to enter the temperature in centigrade as per their choice and our program will return the temperature in Fahrenheit. Like in google we entered 6 by our choice and in another box it showed 182.88 cm.


So our programming would be

class chapter1_program4
{
            public static void main (String args[])
{
            Scanner sc=new Scanner(System.in);
                        int c,f;

                        System.out.println(“Enter temperature in centigrade”);

                        c=sc.nextInt();

                        f=(9*c/5)+32;

                        System.out.println(c+” Centigrade = “+f+” Fahrenheit”) 
}
}


Here you will find 3 new lines as underlined above. No need to understand the meaning of those new lines as of now. For the time being, memorize these lines. Only remember, highlighted System.out.println lines is being used to prompt a message to user saying “Enter the value” , or can prompt “Please give a value” etc etc. and sc.nextInt() helps to take a number input from keyboard and store it inside the variable c. If you enter “abcd” from keyboard, it will throw an error. nextInt() only takes numbers from keyboard.


Suppose now we will take 2 number input from users and will print the sum and product of them. Then the program will be like following:



class chapter1_program5
{
            public static void main (String args[])
{
            Scanner sc=new Scanner(System.in);
                        int a,b,c;

                        System.out.println(“Enter 1st number”);
                        a=sc.nextInt();

                        System.out.println(“Enter 2nd number”);
                        b=sc.nextInt();

c=a+b;
                        System.out.println(“the sum is “+c);

c=a*b;
System.out.println(“the product is “+c);
}

}



Here we have written sc.nextInt() 2 times to take 2 input from keyboard by user.

Similarly if we want to take an input in decimal values , we would use sc.nextDouble(). For any word or sentence input we will use sc.nextLine().

Sc.nextInt() can take input of numbers like 0, 1 , 2 , -3 , -9 , 999 etc

Sc.nextDouble() can take input of numbers like 0.35 , 1.27  , 2.333333  , -3.12 , -9.0 , 999.9999999 etc

Sc.nextLine() can take input of words/sentences like : “Hello”, “hi” , “hi5”, “hello100”, “100”, “







1.3   Third Thing to learn:

Now we can create any small app and give it to our younger brother or sister. We can create following apps by the programming logic we learnt.


1.      Centigrade to Fahrenheit conversion app.

2.      Fahrenheit to Centigrade conversion app.

3.      Meter to KM, CM conversion and vice versa. We had a chapter in mathematics in our childhood days.

4.      Mensuration mathematics like entering 2 sides of rectangle and print area and perimeter.

5.      Area , perimeter calculation for a triangle, circle and square.

6.      In algebra, you will be given the value of a & b and you need to calculate (a+b)2



These apps will be useful in mathematics or physics homework. They can quickly complete their homework without doing any calculation and complete their homework in rapid speed like in 5-10 mins instead of spending the full evening. They just need to execute the program written by you and input the values. For giving this idea, I think mathematics teacher will not be happy at all.


So now for the app, where the user will give radius of a circle and will expect the area and perimeter of the same, he/she will get the result always in integer. Because till now, we are dealing with only integers. But in real scenario, area and perimeter of circle almost always comes in decimal format, as we are multiplying the radius with PI (that is 3.14).


So here we will declare the variable with “double” datatype instead of “int”. Everything else will be same. Please follow the below program.


class chapter1_program6
{
            public static void main (String args[])
{
            Scanner sc=new Scanner(System.in);
                        double a,b,c;
                        System.out.println(“Enter the radius”);
                        a=sc.nextDouble();

b=2 * 3.14 * a;
c=3.14 * r * r;

System.out.println(“the perimeter is “+b); 
                        System.out.println(“the area is “+c); 
}
}



Now take another example:

int a=9;

int b=2;

int c=a/b;

System.out.println(c);


Above program will print the result as 4. But if we declare the variable as double , it would print the correct answer as 4.5 . Integer variable never shows the decimal part, it discards the point values.







1.4   Fourth Thing to learn:

Suppose , it is needed to do determine x5 or y4 in any program. The way you will follow now is to write x*x*x*x*x and y*y*y*y.

But what will happen in following 2 cases:

1.      if I need to determine x34 , will you write x 34 times?

2.      If I need to determine xy and x ,y are unknown and will be given by user in run time. In this case you would not be able to write the code even.


So there is a short-cut to this. Simply write,

double z=Math.pow(x,y);


or directly you can write

double z=Math.pow(5,3);


remember,

1.      M is capital in Math

2.      x and y are integer, but the result of xy will be in double.



Now you must have a confusion why the result is in double datatype. Because in case of 53 , the answer 125 would be surely an integer and it can be easily stored in int z. then why double z?


Ok now answer me, what will happen if user want to see 5½ and 5¾ it means root over  and 50.75. Math.pow(5,0.5) and Math.pow(5,0.75) will always come in decimal data type. So to accommodate both integer and decimal type the result is always in double datatype.


We have an alternative for , we can use both Math.pow(5,0.5) & Math.sqrt(5).



So now assume there is program where user needs to enter 2 number as x & y, in return he/she will get

class chapter1_program7
{
            public static void main (String args[])
{
            Scanner sc=new Scanner(System.in);
                        int x,y;

                        double z;

                        System.out.println(“Enter 1st number”);
                        x=sc.nextInt();

System.out.println(“Enter 2nd number”);
                        y=sc.nextInt();

                        z=Math.sqrt(    Math.pow(x,2)    +   Math.pow(y,2)    );
System.out.println(“the result is “+z); 
}

}




1.5   FIFth Thing to learn:

To calculate the remainder after mathematical division in java, we use ‘%’. Suppose user will give 2 number as input you need to calculate result, remainder after division, we need to write the program in following way:

class chapter1_program8
{
            public static void main (String args[])
{
            Scanner sc=new Scanner(System.in);

                        int a,b,c,d;

                        System.out.println(“Enter 1st number”);
                        a=sc.nextInt();

System.out.println(“Enter 2nd number”);
                        b=sc.nextInt();

                        c=a/b ;
                        d=a%b ;

System.out.println(“the result is “+c); 
                        System.out.println(“the remainder is “+d); 
}
}



So if user enters 2 numbers as 38 & 5. The output will be as follows:

the result is 7
                        the remainder is 3

Remember,
                              Here result came as 7 because integer never can be 7.6


So we are done with the 5 things, now we can build all the small apps where we do not need the help of IF statement or FOR loop. We will learn IF & FOR in immediately next chapters.

So now we can start solving the exercise. So before starting the exercise, we need to maintain 2 small best practice like below:

1.      Try to maintain alignment as much as possible. Alignment means the gap we give before each line.



2.      Try to give a meaningful names to variables.



Instead of giving a,b,c,d  , try to give names like num1 , num2, div, rem respectively. Later when you will be dealing with a hundred line program, it will be tough to handle and remember those multiple variables. Better to call them by their meaningful names.



That’s all for this chapter. Now we can jump into the exercise. You can find the exercise in the album of the FACEBOOK GROUP. Group link is mentioned above. In case of any problem, you can contact to the mail id provided above.

Comments

Popular posts from this blog

Java concepts Theory