DEV Community

darKLoin
darKLoin

Posted on

day-03

DAY-03
Index
01.Break Lable
02.Continue Lable
03.Array
04.2D-Array
05.Class/Object
06.Extra problem
06.1: Write a JAVA program to find simple interest.
06.2: Definging a class called Student.Observe the members of the class i.e. the data and function.
06.3: Write the JAVA program that prompts the user for an integer and then prints out all prime numbers up to that integer.
06.4: Write a JAVA program that checks whether a given string is palindrome or not?
06.5: Write a JAVA program to fine the biggest & smallest number from the given array of 10 integers.
06.6: Write a JAVA program to sort given of 10 integers in ascending & descending order.
06.7: Write a JAVA program to delete an element from an array of 10 elements.
06.8: Write a JAVA program that prints transpose of given matrix.

---------------------------------START----------------------------
----------------------------------01------------------------------

  1. Lable Break ~Lable Break: lable break means, coming out of a labled block of code OR braking the mentioned loop or block of code. it also work as a go to statement lable: is a identifire which identifies the some block of code. Systax: } { ...//statement }break lable; example: 01.1:write a simple program to show the use and working of'break lable;'
class lable
{
    public static void main(String args[])
    {
        boolean hell = true;
        first:
            {
                second: 
                    {
                        third:
                            {
                                System.out.println("in third block of code");
                                if(hell)
                                    break second;
                                System.out.println("after bresk second,, in third block of code.");
                            }
                        System.out.println("in second block of code.");
                    }
                System.out.println("in first block of code.");
            }

    }
}
Enter fullscreen mode Exit fullscreen mode
example: 01.2: write a loop program to show the use of break lable.
Enter fullscreen mode Exit fullscreen mode
class lablebreak
{
    public static void main(String args[])
    {
        first: for(int i = 0; i<100;i++)
                {
                    second: for(int j =10 ; j<100 ; i++ )
                            {
                                third: for(int k =0 ; k <5; k++)
                                        {
                                            ............................................................................
                                        }
                            }
                }
    }
}
Enter fullscreen mode Exit fullscreen mode

~if lable break is not there in the block or the loop then it will give the error.
like

class errcheck
{
    public static void main(String args[])
    {
        fail: for(int i = 5 ; i<50 ;i++)
        System.out.println("hello this will excute with out loop.");

        second: for(int j =21; j<30; j++)
                {
                    if(j==25)
                        break fail;
                    System.out.println("after breaking fail.");
                }
    }
}
Enter fullscreen mode Exit fullscreen mode

~ error will look like this

Main.java:11: error: undefined label: fail
                        break fail;
                        ^
1 error
exit status 1
Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------
--------------------------------02--------------------------------

  1. Lable continue
    ~ Lable Continue is work to skip some further steps of the loop or block of code in labled block.

    example: 02.1: write a program to show the use of continue lable.

class contlable
{
    public static void main(String args[])
    {
    int i,j;
        first: for( i = 0 ; i<10 ; i++)
                {
                    second: for( j = 0 ; j<10 ; j++)
                            {
                                if(j>i)
                                {
                                    System.out.println(" ");
                                    continue first;
                                }
                                System.out.print(" "+i*j);
                            }
                }
        System.out.println("");
    }
}
Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------
---------------------------------03-------------------------------

  1. Array
    ~Array: it stores the data of same data type with consecutive order of memory.
    ~decleration:
    DataType ArrayName[] = new dataType[Size];
    int arr[] = new int[50];
    int arr[] = {1,2,4,5,6,8,4,7,4};

    example: 03.1: write a program to get the average double element of an array.

class arro
{
    public static void main(String args[])
    {
        double arr[] = {1.1,1.2,1.3,2.1,2.2,2.3,2.4};
        double result = 0, avg =0 ;
        for(int i = 0 ; i<arr.length; i++)
        {
            result = result+arr[i];
        }
        avg = result / arr.length;
        System.out.println("the average of given array is: "+avg);
    }
}

Enter fullscreen mode Exit fullscreen mode
example: 03.2: write a program to get the average of an array.(size and element are input from user)
Enter fullscreen mode Exit fullscreen mode
import java.util.Scanner;
class arr
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int result =0;
        double avg = 0;
        System.out.println("Enter the size of array");
        int n = sc.nextInt();
        int arr[] = new int[n];
        System.out.println("Enter the element for the array");
        for(int i= 0 ; i<n; i++)
        {   
            arr[i] = sc.nextInt();
            result = result + arr[i];
        }
        avg = result/arr.length;
        System.out.println("The average of array is: "+avg);
    }
}
Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------
---------------------------------04-------------------------------
04.2D-Array
~2D-Array: 2D-array are the same as the array but it stores the another dimension of the array
as shown in example:
example: 04.1: write a program to show the two dimensional array by taking input.

class 2darr
{
    public static void main(String args[])
    {
        int i,j,k=0;
        int arr[][] = new int[4][4];
        for(i = 0 ; i< 3 ; i++)
        {
            for(j =0 ; j<3 ; j++)
            {
                arr[i][j] = k ;
                k++;
            }
        }
        for(i =0 ; i<3 ; i++)
        {
            for(j =0 ; j <3 ; j++)
            {
                System.out.print(" "+arr[3][3]);
            }
                System.out.println("");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

~we can decleare a 2-D in different ways
by taking input from user
by decleareing the different variable for different or outer position as declerared
int two[][] = new int[3][];
two[1] =new int[1];
two[2] = new int[2];
two[3] = new int[3];

by declearing the inner variable with respect of outer variable
int two[3][4] = new int[3][];
two[0] =new int[4];
two[2] =new int[4];
two[3] =new int[4];
Enter fullscreen mode Exit fullscreen mode

---------------------------------END------------------------------
---------------------------------05-------------------------------
05.Class/Object
~there are two types of objects normally
1. tangible objects:- pen, desktop , bike, bottel.
2. intangible objects:- account , fee , air , marks.

~every objects have different states adn behaviours like
dog: states(name, color, bread,appetite) behaviour(bark,run,fetching)

class: it is the set of property behaviour which all entites in objects list.
include the all property(states, behaviour) in them for the similar objects.
it is also known as blueprint-n objects.

    the different propety(states,behaviour) is also known as instance variable(states) and instance function(behaviour)
    class is a logical entity and 
    objects is physical entity

for example 
    class laptop
    {
        float weight;
        int processer;
        float model;
        float color;
        float rate;
        //above all are the class member/instance variable/variable member

        void broucher()
        {
            float all = model+color+rate+weight;
            System.out.printnl(" "+all);
        } 
        //this is the class behacviour/instance function it uses the states of class
    }

    objects are created dynamicaly like lapotopIdentifire

    Creation of a class is a creation of a datatype and
    object creatation is creation of variable to use class datatype
    create a object as 
    laptop dell = new laptop();
    laptop hp = new laptop();
    laptop intel = new laptop();

    here 'laptop();' is work as a constructor

    object is also known as instance of a class

example: create a class and object
Enter fullscreen mode Exit fullscreen mode

class laptop
{
float weight;
float model;
float color;
float rate;
void broucher()
{
float all = model+color+rate+weight;
System.out.printnl(" "+all);
}
}

class laptopIdentifier
{
    public static void main(String args[])
    {
        laptop obj1 = new laptop();
        obj1.weight = 230.44f; 
        obj1.model = 239944X; 
        obj1.color = #112233; 
        obj1.rate = 66000.0; 
        obj1.broucher();
        laptop obj2 = new laptop();
        obj2.weight = 465.44f; 
        obj2.model = 654167Y; 
        obj2.color = #458932; 
        obj2.rate = 85200.0f; 
        obj2.broucher();
        laptop obj3 = new laptop();
        obj3.weight = 850.00f; 
        obj3.model = 5448131A; 
        obj3.color = #789522; 
        obj3.rate = 239000.0; 
        obj3.broucher();
    }
}
    ```


---------------------------------END------------------------------
---------------------------------06-------------------------------

06.Extra problem
    ~problem
        06.1: Write a JAVA program to find simple interest.
    ~problem    
        06.2: Definging a class called Student.Observe the members of the class i.e. the data and function.
    ~problem
        06.3: Write the JAVA program that prompts the user for an integer and then prints out all prime numbers up to that integer.
    ~problem
        06.4: Write a JAVA program that checks whether a given string is palindrome or not?
    ~problem    
        06.5: Write a JAVA program to fine the biggest & smallest number from the given array of 10 integers.
    ~problem
        06.6: Write a JAVA program to sort given of 10 integers in ascending & descending order.
    ~problem
        06.7: Write a JAVA program to delete an element from an array of 10 elements.
    ~problem
        06.8: Write a JAVA program that prints transpose of given matrix.           











Enter fullscreen mode Exit fullscreen mode

Top comments (0)