Wednesday 25 July 2018

class 9 Java introduction

Creating a set of instructions to make the computer to do something is no Rocket Science. Just a bit of logic, bit of syntax, bit of patience, bit of interest and bit of concentration in the class are all you need to learn the language.

Programming ,  most of the times involve 4 steps:
  1. Declaration
  2. Assign / Input
  3. Calculation (if necessary)
  4. Print
Example 1 :

Write a program to print your name and school name.

class prog1
{
  public static void main( )
  {
      System.out.print("Bishop");
      System.out.print("Cottons");
   }
}
   
   This is the basic structure of the program. You might have noticed that the program did not use all the above 4 steps. 

  We don't have to use all the steps in all the programs. 
The output is going to be :
BishopCottons

System.out.print( ) displays anything written inside the brackets.

In case if I don't want the answer in single line , I have to use

System.out.println( ).

In case if I want the answer in a single line but separated by a space or a comma then,

System.out.print("Bishop" + "  " + "Cottons" ).

The answer will be Bishop  Cottons

In the print statement, the + symbol is not an addition symbol rather it combines two items, that is Bishop and space and also space and Cottons. So there are 3 items Bishop, space , Cottons.

So if I make a small change in my program the answers will be in two different lines.
    
class prog1
{
  public static void main( )
  {
      System.out.println("Kavitha");
      System.out.print("Cottons");
   }
}


 Example 2 :

Write a program to assign two values and print its sum.

class prog2
{
  public static void main( )
  {
      int a, b,c;  -------------------------------------- Step 1
      a=5;         ---------------------------------------Step 2
      b=30;      ----------------------------------------Step 2
      c=a+b;    --------------------------------------- Step 3
      System.out.print(c);  ------------------------- Step 4
     
   }
}

The output will be 35

Step 1 : Declaration.

           Mention the data types of the variables involved.
Step 2: Assign

           Put some values in your variables.
Step 3 : Calculate

           Add the values and store it in the new variable. Remember variable = constant not the other way round.

Step  4 : Print

               Display the answer. you can also write
         System.out.print("Sum= " + c);

The answer will be Sum = 35. This is the good way of displaying the answer with a message.

Steps 1 and 2 can be combined to form a single statement.

  int a = 5, b=30, c;

Input Methods

   We are going to learn 2 input methods .

  • Blue J
  • Scanner


 Blue J Method

class prog3
{
  public static void main( int a, int b ) ----- Accepting values                                                                              from the user
  {
      int c;
      c=a+b;
      System.out.print("Sum = " + c);
   }
}

Even though both a and b are of same data type ,  it should be mentioned twice and should be separated by comma(,).

public static void main( int a, b ) ------ Wrong


 See you in the next post.

No comments:

Post a Comment