Wednesday, 26 July 2017

Class 10 - Methods

Its not ROCKET  SCIENCE.....

Its just COMPUTER SCIENCE .....



METHODS
When our program is too complicated, the best approach is to divide it into smaller sections, so that it is easy to write the code and also easy to debug.
If we want to use  a set of statements in more than one place, instead of writing the same code  again and again,we can write it once and call it wherever we need it.
For instance
*******************************************************
WELCOME TO MY BLOG
*******************************************************
Class X Notes.

*******************************************************
class example1
{
    public static void main( )
       {
          System.out.println("******************************");
          System.out.println("WELCOME TO MY BLOG");
          System.out.println("******************************");
          System.out.println("Class X Notes");
              System.out.println("****************************");
      }
}
In the above program the highlighted lines are repeated 3 times. We can reduce the code by writing once and calling it 3 times.
class example1
{
    public static void main( )
       {           
                    star( );
                    System.out.println("WELCOME TO MY BLOG");
                    star( );
                    star( );
                 }
public static void star ( )
     {
                Sytem.out.println("****************************");
      }
}
Method Header
access_specifier  modifier  return data_type method name( )e.g :
 public static void main() 
 

NOTE
  • The other name for method is FUNCTION
  • A method can be easily identified .A method name is always followed by the opening and closing parentheses ( ).
  • A method name must begin with a letter or an underscore, it also can contain letters, numbers and underscore.
  • Access specifier and modifier are optional.
  • main( )  method can be written anywhere in the program.
Function header or prototype
The first line of the method with the name of the method,data type of the return value, number and the type of arguments is called the header or prototype.
Function Signature
It is the number and types of arguments.
1. Example
Write a program to add two numbers .
class addition{
public static void main( )
  {
     int a=10,b=45; --------------------------------------------1
     int s= add(a,b); -------------------------------------2,6     System.out .print("Sum = "+s);-----------------------7
}
 int add(int a,int b)----------------------------------------3
  {      int c;
       c=a+b;-----------------------------------------------4
       return  c; --------------------------------------------5  
  }
}
In the above program , the numbers are the order in which the program will be executed. In the step 2, add( ) is the method name,this is the function call.the control will go to the function in step 3.The method statements will be executed.And the answer will be sent to the step which called the function, that is step 2.The answer is stored in variable s.The data type of the answer is int that is why the return data type is int in function header. Step 2 is also called calling a funtion or invoking a function.
Tha above program can be written like this:

class addition
{
public static void main( )
  {
     int a=10,b=45; --------------------------------------------1
      add(a,b); -------------------------------------2    
}
 void add(int a,int b)----------------------------------------3
  {      int c;
       c=a+b;-----------------------------------------------4
       System.out.print(c); -----------------------------------5  
  }
}
There is a change in step 2, the answer is not stored in any variable .The answer is displayed in the method add itself, thats why there is no return statement and also the datatype is void in function header.  a and b in step 2 are called actual parameters ,a and b in step 3 is called formal parameters.
2. Example 
 Write a program to find the factorial of a number .
class factorial
{
public static void main( )
  {
     int n=5; --------------------------------------------1
     int x= fact(n); -------------------------------------2,8
     System.out .print("Factorial = "+x);------------9
}
 int fact(int n)----------------------------------------3
{
     int i,f=1; ------------------------------------------4
    for(i=1;i<=n;i++)---------------------------------5
     f=f*i; ----------------------------------------------6
  return f; --------------------------------------------7
}
}
After the  step 2 the control will go to the function fact.It will complete all the statements there and send the answer back to main function. The answer will be an integer value ,thats why the return data type is int.
3. Example
Write a program to display the sum of the series

13+23+33+….n3
using the function cube()
class sum

{
public static void main(int n)---------1
{
int i,s=0;
for (i=1;i<=n;i++)
s=s+cube(i);
System.out.print("Sum="+s);
}
public static int cube(int a)
  {
    int c=a*a*a;
    return c;
   }
}
In step 1 you should accept the value of ' n ' from the user. In the above question we should find the cube of each number.We write the code for this once, but call it again and again.

Let me introduce the scope of variables here......

The scope of the variable is nothing but where all you can use a particular variable in a program.
  • Local variable - Variable which is declared inside any function.You can use the variable only in that function not throughout the program .
  • Global variable - variable declared ouside all functions. i.e. just after class.This can be used anywhere in the program.
Example 4
Define a class salary describesd as below:
Data members : Name,Address,Subject,Salary,Income tax
Member methods :
  1.   accept the details of the teacher
  2. display the details
  3. compute the income tax as 5% of the annual salary above Rs.1,75,000.
Write a main method to create an object .
import java.util.*;
class salary
{
String name,address,subject;-----------1
double sal,it;--------------------------2
void get(String n,String add,String sub,double s1)------3
{
  name=n;
  address=add;
  subject=sub;
  sal=s1;
}
void compute()
{   
   int a=sal*12 ;
  if(a > 175000)
          it=0.05* a;
  else
          it=0;
}
void display( )
{
  System.out.println("Name="+name);
  System.out.println("Address="+address);
  System.out.println("Subject="+subject);
  System.out.println("Salary="+sal);
  System.out.println("tax="+it);
}
public static void main()
{
   salary ob=new salary();---------------------------4
   Scanner s=new Scanner(System.in);
    System.out.println("Enter the name");
    name=s.next();
    System.out.println("Enter the address");
    address=s.next();
    System.out.println("Enter the subject");
    subject=s.next();
    System.out.println("Enter the  Monthly salary");
   salary=s.nextDouble();
   ob.get(name,address,subject);
   ob.compute();
   ob.display( );
}
}

Steps 1 and 2 are the global variables.You can use them anywhere in the program. In step 3, the values received from the main method . and in   get ()  the values are stored in global variable for the same reason mentioned above.compute() and display() are according to the question.In step 4, we create an object. salary is the calss name,donot use anything else, ob is the object name,you can give any name. new is the keyword, it allocates memory for the object.
Remember to write the global variable on the left hand side and local variable on the right hand side in void get().Donot mix it up
Methods can be divided into
  • Pure method or accessors - methods donot change the state of an object.
  • Impure mentod or mutators - methods the change  the state of the object.
Method Overlaoding or Function OverloadingThere can be more than one method with the same name.But the problem is how the control will go to the method it is supposed to go.Thats why there should be  some difference in the methods.So method overloading will have more than one method with same name but differ in the number or types of arguments or both.   
Write a program using a function called area( ) to do the following :
  1. Area of a circle
  2. Area of a square
  3. Area of a rectangle

import java.util.*;
class menu
{
public void area(int r)
{
System.out.println("Area of circle="+(3.14*r*r));
}
public void area(int l,int b)
{
System.out.println("Area of rectangle="+(l*b));
}
public void area(float side)
{
System.out.println("Area of square="+(side*side));
}
public  void main( )
{
   Scanner s =new Scanner(System.in);
   System.out.println("Enter radius");
   int r=s.nextInt();
   System.out.println("Enter length and breadth");
    int l=s.nextInt();
    int b=s.nextInt();
   System.out.println("Enter side:");
   float side=s.nextFloat();
area(r);-------------------1
area(l,b);-----------------2
area(side);---------------3
}
}

In step 1 and step 3 have same number of arguments but different data type values and all the 3 functions have same name and differ in something or the other so it represents function overloading.
I give a similar program, try it.If you want your program to be checked, mail it to me.
Write a program using a class to calculate the volume,surface area and the diagonal of cuboid with the following specifications:
class name  : Cuboid
Data members : int l,int b,int h
Member funcions :
void input() : Accept length,breadth and height of cuboid
void calculate() : find volume,surface area and diagonal
void dispaly(): Print the result
Volume of cuboid : lxbxh
Surface Area : 2(lb+bh+lh)
Diagonal :  square root of   l2+b2+h2

Java provides two ways in which a value can be called.
  • Pass or call by value : Its simple,just pass the value in calling function and receive the value in receiving function like in the above examples.
  • Pass or call by reference : Reference is the memory address. The memory address of the variable is passed , so if there is a change in the receiving function, then it will refelect in the calling function also.e.g passing arrays.

Sunday, 23 July 2017

Class 7 - Revision

Select the correct answer:
  1. HTML stands for
  • Hyper Text Markup Language
  • Hyper Text Makeup Language

2. Container element has both
  • opening and middle tag
  • opening and closing tag
3. <br> is
  • a line break tag
  • an empty tag
  • line break and an empty tag
4. Firefox is
  • an editor
  • a web browser
  • search engine
5. How would you give the background colour
  • <body backcolor=red>
  • <body bgcolour=red>
  • <body bgcolor=red>
  • <body backcolour=red>
  • <body bg color=red>
6. To change the colour of the letters and numbers you type
  • <text=green>
  • <body textcolour=green>
  • <body text=green>
  • <textcolour=green>
7. <p> tag
  • moves the cursor to the next line
  • leaves a blank line
  • leaves a paragraph
8. _____ are not displayed in the web browser.
  • comments
  • title
  • body
9. To align the text in the middle of the page
  • <align=center>
  • <align=centre>
  • <p align=centre>
  • <center>
10. Heading tags ranges from ___ to _____
  • 1 to 7
  • 1 to 6
  • 1 to 3
11. To make the text look darker
  • <dark>
  • <b>
  • <bold>
12. To draw a line across the text
  • <u>
  • <line>
  • <strike>
  • <strikethrough>
13. The largest heading tag is
  • <head>
  • <h1>
  • <h6>

14. The text between <title> and </title> will be displayed
  • in the title bar
  • in the heading bar
  • in the web page
  • none of the above
15. An attribute is
  • an additional information about the tag
  • main tag
  • an example

1. Write a program in HTML to display the following :

My first program                        ---------------------------- 1
I am in class 7                               -----------------------------2

Bishop Cotton Boys' School        -----------------------------3
 
<html>
<head>
<title> Example 1</title>
</head>
<body >

<b> My first program</b> <br>
 I am in  <I> class 7 </I> <p>
<u> Bishop Cotton Boys' School</p>
</body>
</html>


The  line1 is darker than the rest of the program. This is because of the bold tag. to go to the next line use the <br> tag.
In line 2 , only class 7 is slanting , so use italics tag for that.
There is a blank line between  line 2 and line 3, so paragraph tag should be used. Even if you close <p> before <u> tag also will give same effect.
In line 3,a line is drawn below the whole sentence using <u> tag.

2. What would be the output for the following program:
<html>
<head>
<title> Example 2</title>
</head>
<body bgcolor=olive >                              ------------------1
<font color=white size=6 face= Mistral> ------------------2
Computers<br>Class 7 
<p>
Cottonnian shield...................

</p>
<!- I am going to draw a line - >               -----------------3
<hr size=5  >                                             -----------------4
</font>
</body>
</html>


Line 1 : the background colour. The spelling is bgcolor.
Line 2 : font color makes the text and numbers to turn to that particular colour. face changes the default font name to the name given.
Line 3 : This is the comment line , which will not be displayed in the web browser.
Line 4: The horizontal line with size 5




























      The above output has 5 lines. The first one is a thin line, because there is no size tag.
      <html>
      <head>
      <title> Example 3</title>
      </head>
      <body>
      <hr > <p>
      <hr size=3 color=red> <p>
      <hr size=8 color=green></p>
      <hr size=3 align=right color=blue><p>
      <hr size=3 noshade ></p>
      </body>
      </html>

      Class 7 - Introduction to HTML

      HTML
      HTML (Hyper Text MarkUp Language) is used to create web pages. The codes are called tags. The tags are given within < and >.
      There are two types of tags ,
      i.                   Container Tag : It has both opening and closing tags.
                               e.g : <html>  </html>
      ii.                 Empty Tag     : It has only a opening tag.
                              e.g : <br>
      The basic structure of a HTML document is
      <html>
      <head>
      <title>
      </title>
      </head>
      <body>
      ………
      ……..
      </body>
      </html>
       Whatever is typed between <title> and </title>  will be displayed on the title bar not in the web page.

      <html>
      <head><title> My Page BCBS</title>
      </head>

      <body>
      Your text goes here.
      </body>
      </html>

      Example 1 :
      <html>
      <head><title> My Page BCBS</title>
      </head>
      <body>
      Bishop Cotton Boys' School
      </body>
      </html>


      The output will be :




      The data entered between <body> and </body> only  will be displayed in the web page.
      Line break tag <br>:
      It moves the cursor to the next line.
      Paragraph Tag <p> :
      It leaves a blank line.
      Example 1 :
      <html>
      <head><title> My Page BCBS</title>
      </head>
      <body>
      BCBS <p>

      Thomas House <br>
      Pope House <br>
      Elphick House <br>
      Pakenham Walsh  House <br>
      Pettigrew House
      </body>
      </html>


      The output will be :

       


      Class 7 - Formatting Tags

      Formatting tags
      The formatting tags are used to give a stylish appearance to the text in the web page.
      Bold
      The text appears darker.
      <b> ..... </b>
      Italics
      The text appears different(slanting).
      <I>....</I>
      Underline
      There is a line below the text.
      <U>.....</U>
      Strikethrough
      A line is drawn across the text.
      <Strike>....</Strike>
      Superscript
      Text appears above the text.
      <Sup>....</Sup>
      Subscript
      Text appears below the text
      <Sub>.....</Sub>
      Center
      Text appears in the middle of the screen.
      <Center>....</Center>

      <html>
      <head>
      <title>
      Friends
      </title>
      </head>
      <body bgcolor=olive text =white>
      <center><h1>FRIENDS</h1></center>

      Written with a pen, sealed with a kiss,<br>
       If you are my <b>friend </b>, please answer me this:<br>
       Are we friends, or are we not?<br>
       You told me once, but I <I> forgot</I>.<br>
       So tell me now, and tell me <u>true</u>,<br>
       So I can say I'm here for you.<p>
       Of all the friends I've ever met,<br>
       You're the one I won't forget.<br>
       And if I die before you do,<br>
       I'll go to <b><I> Heaven </I></b>and wait for you,<br>
       I'll give the <font color=red><h2>angels</h2></font> back their wings<br>
       And risk the loss of everything.<br>
       There isn't a thing I wouldn't do,<br>
       To have a friend just like you!

      <p align =right>The true author is unknown

      </body>
      </html>
       

      OUTPUT
      

      Class 7 -External Hardware Devices


      Class 7 - HTML Quiz

      Monday, 10 July 2017

      Class 7 : HTML

      HTML (Hyper Text MarkUp Language) is used to create web pages. The codes are called tags. The tags are given within < and >.
      There are two types of tags ,
      i.                   Container Tag : It has both opening and closing tags.
      ii.                 Empty Tag     : It has only an opening tag.
      The basic structure of a HTML document is
      <html>
      <head>
      <title>
      </title>
      </head>
      <body>
      ………
      ……..
      </body>
      </html>
       Whatever is typed between <title> and </title>  will be displayed on the title bar not in the web page.
      <html>


      <head><title> My Page BCBS</title>
      </head>

      <body>
      Your text goes here.
      </body>
      </html>

      The data entered between <body> and </body> only  will be displayed in the web page.

      Line Break Tag:
      The cursor moves to the next line.
      Paragraph Tag :
      It gives a blank line.
      Heading Tag : Heading tags are used to display the text slightly bigger and bolder. There are six levels. Level 1 is the largest.
      The output will  be:
            
          Formatting Tags :

      1.     Bold :  <b>  …</b>
      2.     Italics  : <I> …. </I>
      3.     Underline  : <U> ….</U>
      4.     Superscript : <SUP>….. </SUP>
      5.     Subscript : <SUB> ….</SUB>
      6.     Center Tag: The spelling is important. It positions the text in the center of the screen. <center>....</center>
      7. Strikethrough : <strike> ..... </strike> : It draws a line through
      the middle of the text.
      The output will be: