Thursday 12 January 2017

Class 9 - switch

Points to remember:
1.  switch,case and default  in small letters
2. After switch , the  constant must be either  integer or character constant.
3. If the input doesnot match any given choices , the default will be executed.
4.break statement is compulsory, otherwise all the cases will be executed.
5. After case and the constant , there is a colon(:)
6. After switch there is no semi colon.
7. No two cases can have the same value.

Section A
1. What is the function of default in switch statement?
                                                                                      [ICSE 2005]
2. Write down the syntax of switch case.                [ICSE 2007]
3. What is the purpose of break statement?
4. Re write the following using switch case
int n=0,ch;
if(ch==1)
{
n++;
System.out.println(n);
}
if(ch==2)
{
n+=2;
System.out.println(n);
}
if(ch==3)
{
n+=3;
System.out.println(n);
}

Section B
1. Write a menu driven program to find the area of an equlateral triangle(ET) , a right angled triangle (RT), a scalene triangle (ST)respectively.
2. Write a menu driven program to calculate the area of
circle =p*r*r
square = side * side
rectangle = length * breadth                                 [ICSE 2005]
Display the menu to output the area as per the user's choice. For an incorrect input, appropriate message should be displayed.

Class 9 - Scanner

Input Method:

 We are going to use Scanner for accepting values from the user . Its a simple one.

First include the package util.

import java.util.*;

Only small letters and * indicates all the files from the package and there is a semi colon in the end.

Then comes the main function

public static void main( )
{
Scanner S=new Scanner(System.in);

Remember S in red colour are capital letters. Here S is a variable can be in small or capital letter or any other variable name.

There are different statements for accepting different data type values.



int a= S.nextInt( );

float f= S.nextFloat( );

double x=S.nextDouble( );

But  accepting character and string are different.

char ch=(char)System.in.read( );

String st=S.next( );

String st1=S.nextLine( );

next( ) accepts a word till a space and keeps the cursor in the same line.

nextLine( ) accepts words and moves the cursor to the next line after accepting the value.

Let us accept two numbers.

import java.util.*; ------------------------------------ 1

class ex1
{
public static void main( )
{
  Scanner s=new Scanner(System.in); --------------- 2
int a, b,sum; --------------------------------------------- 3
System.out.println("Enter the first number"); ------ 4
a=s.nextInt( ); ------------------------------------------- 5
System.out.println("Enter the second number");----- 4
b=s.nextInt( );-------------------------------------------- 5
sum=a+b; -------------------------------------------------6
System.out.print("Total = " +sum);------------------- 7
}
}

 1  is the first line in the program.

 2  Write any variable instead of s , if you want.

 3  is variable declaration.

 4 is just a prompt message. A remainder to the user to enter a number.

5 Since I am accepting integer values, it should be nextInt. Remember no space  after next and I in Int is capital.

6 is the calculation

7 is the output.


 For other programs step 3 and step 5 will be different according to the data type you use.