Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Saturday, 10 September 2016

Android Tutorial : Toast Example

Android Toast is used to display small pop up information for short time. It contain message to display some information.In this example , we will show that how to display message using toast.

android.widget.Toast class is the subclass of the java.lang.Object class. We can also create our custom toast like we can add image and text in toast and display on the top of the activity or bottom or center can also.

There are some constant of the toast class :

  • LENGTH_SHORT : display toast for the short time.
  • LENGTH_LONG : display toast for the long time.


There are some methods of the toast class :


  • public static Toast makeText(Context context, CharSequence text, int duration) :
    • used for set message of the toast and duration of the toast to display on view.

  • public void show() :
    • used for display toast on activity.

  • public void setMargin (float horizontalMargin, float verticalMargin) :
    • used to set horizontal and vertical margin.




File Name : MainActivity.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.example.toastexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Toast.makeText(getApplicationContext(),"Welcome to Desire Code",Toast.LENGTH_SHORT).show();
 }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}
Output:




Toast

Monday, 5 September 2016

Write a program of an array of object of room class to get, calculate and display the information

In this program, Room class is used for get details from the user and calculate amount of room charge and display details. Here member variables and member function is like this,

Member variable : roomNo, roomType, roomChargePerDay, roomAllocatedDays, totalPayment
Member Function : getRoomData() , calculatePayment() , displayRoomData()

getRoomData() is used to get all details from the user. calculatePayment() is used calculate all the task which fill up in details. displayRoomData() is used to display all the details with calculations.

Step of Execute Program :

1) Open Command Prompt
2) Set Path
3) Compile File     //javac room.java
4) Run File           // java room1
5) Fill the Details



File Name : room.java



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.*;
class room
{
 private int roomNo;
 private int roomType;
 private double roomChargePerDay;
 private int roomAllocatedDays;
 private double totalPayment;
 room()
 {
  this.roomNo=0;
  this.roomChargePerDay=0;
  this.roomAllocatedDays=0;
  this.totalPayment=0;
 }
 public void getRoomData(int roomNo,int roomType,double roomChargePerDay,int roomAllocatedDays)
 {
  this.roomNo=roomNo;
  this.roomType=roomType;
  this.roomChargePerDay=roomChargePerDay;
  this.roomAllocatedDays=roomAllocatedDays;

   this.calculatePayment();
 }
 private void calculatePayment()
 {
  this.totalPayment=roomChargePerDay*roomAllocatedDays;
 }
 public void displayRoomData()
 {
  if (roomType==1)
  {
   System.out.println("----------------------------------------------------------------------\n");
   System.out.println("Room No: " + roomNo + "\n");
   System.out.println("Room Type : Regular");
   System.out.print("Room Charges:" + roomChargePerDay);
   System.out.println("\t\t\t Allocated Days : " + roomAllocatedDays+"\n");
   System.out.println("\t \t \t Total charges : " + totalPayment);
   System.out.println("--------------------------------------------------------------------------\n");

   }
  else if (roomType==2)
  {
   System.out.println("----------------------------------------------------------------------\n");
   System.out.println("Room No: " + roomNo + "\n");
   System.out.println("Room Type : Delux");
   System.out.print("Room Charges:" + roomChargePerDay);
   System.out.println("\t\t\t Allocated Days : " + roomAllocatedDays+"\n");
   System.out.println("\t \t \t Total charges : " + totalPayment);
   System.out.println("--------------------------------------------------------------------------\n");

   }
  else if(roomType==3)
  {
   System.out.println("----------------------------------------------------------------------\n");
   System.out.println("Room No: " + roomNo + "\n");
   System.out.println("Room Type : Super Delux");
   System.out.print("Room Charges:" + roomChargePerDay);
   System.out.println("\t\t\t Allocated Days : " + roomAllocatedDays+"\n");
   System.out.println("\t \t \t Total charges : " + totalPayment);
   System.out.println("--------------------------------------------------------------------------\n");

   }
  else
  {
   System.out.println("Wrong Input");
  }
 }

}
class room1
{
 public static void main(String[] args)
 {
       
  try
  {

    DataInputStream in = new DataInputStream(System.in);


    System.out.print("\nHow many rooms allocating :");
   int roomAllocate=Integer.parseInt(in.readLine()); 
 
   room r[]=new room[roomAllocate];

    for(int i=0;i<roomAllocate;i++)
   {
    r[i]=new room();
    System.out.println("\n\t\t::::::::::Room Information Form::::::::::\n");
  
    System.out.print("Enter Room No.: ");
    int roomNo=Integer.parseInt(in.readLine());

     System.out.println("Select Room Type:-\n 1. Regular\n 2. Delux\n 3. Super Delux");
    int roomType=Integer.parseInt(in.readLine());
 
    System.out.print("Enter Room Charges Per Day: ");
    double roomChargesPerDay=Double.parseDouble(in.readLine());

     System.out.print("Enter Room Allocated Days: ");
    int roomAllocatedDays=Integer.parseInt(in.readLine());
  
    r[i].getRoomData(roomNo,roomType,roomChargesPerDay,roomAllocatedDays);

    }

    for(int i=0;i<roomAllocate;i++)
    r[i].displayRoomData();
  }
  catch(IOException e)
  {
   System.out.println("Something Goes Wrong...");
   System.out.println(e.getMessage());
  }
 }
}






Output :

array object of room class

Write a program to find out the second largest value from the N values Stored in array

In this program, code is explain about to find out the second largest value from the array. First , user must set the limit of the array. Then enter value in array. Which value is second largest from the array it will display at the end. In this program, for loop is used to find second largest value. User can also any looping structure if they don't want use for loop.

Step for Execute Program :

1) Open Command prompt
2) Set Path
3) Compile File     // javac SecondLargeArray.java
4) Run File           //java SecondLargeArray
5) Fill the Details



File Name : SecondLargeArray.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.*;
class SecondLargeArray
{
 public static void main(String []s) throws Exception
 {
  int n ,second=0,temp;

   DataInputStream da=new DataInputStream(System.in);
  
  System.out.println("Enter length of array :");  
  n=Integer.parseInt(da.readLine());
  
  int []arr1=new int[n];
  System.out.println("Enter value of array :");  
  for(int i=0;i<n;i++)
  {
   arr1[i]=Integer.parseInt(da.readLine()); 
  }
  for(int i=0;i<n;i++)
  {
   for(int j=i+1;j<n;j++)
   {
    if(arr1[i]>arr1[j])
    {
     temp=arr1[i];
     arr1[i]=arr1[j];
     arr1[j]=temp; 
    }
   }
  }

   second=arr1[n-1];
  for(int i=n-1;i>=0;i--)
  {
   if(arr1[i]<second)
   {
    System.out.println("Second Largest value :"+arr1[i]);
    break;
   }
  }

  }
}





Output :

Second Largest Value from Array

Write a program to perform matrix addition operation in java

In this program, Take the row and column value from the user and perform addition of the matrix. User can add more than one row and one column. Here explain the code and display the output of the program in given below.

Step of Execute Program :

1) Open Command prompt
2) Set path
3) Compile File // javac matrix.java
4) Run File // java matrix
5) Fill the Details


File Name : matrix.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.*;
class matrix
{
 public static void main(String args[])throws Exception
 {
  int n,i,j,r,c;
  
  DataInputStream dis=new DataInputStream(System.in);

   System.out.println();
  System.out.println();
  System.out.print("enter The Number Of rows :-    ");
  r=Integer.parseInt(dis.readLine());

   System.out.print("enter The Number Of Column :-    ");
  c=Integer.parseInt(dis.readLine());

   int arr1[][]=new int[r][c];
  int arr2[][]=new int[r][c];
  int arr3[][]=new int[r][c];

   System.out.println();
  System.out.println();

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    System.out.print("Enter number for First Matrix [ "+(i)+" ] [

"+(j)+" ]    :-   ");
    arr1[i][j]=Integer.parseInt(dis.readLine());
   }
 
  }

   System.out.println();

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    System.out.print("Enter number for Second Matrix [ "+(i)+" ] [

"+(j)+" ]    :-   ");
    arr2[i][j]=Integer.parseInt(dis.readLine());
   }
 
  }

   System.out.println();

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    arr3[i][j]=arr1[i][j]+arr2[i][j];
   }
 
  }

   for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    System.out.println("Addition of Two Matrix is  [ "+(i)+" ] [ "+(j)+"

]    :-   "+arr3[i][j]);
   }
  }
  System.out.println();
 }
}



Output :

Matrix Addition in Java

Write a Program to change background color using Radio Button in android

In this Example, There are three radio button use in this program. On click of radio button background color of view will be changed as per name. For ex. on yellow radio button select background color will be change and yellow color set. Here, design file and code file is describe here and output of the program also display in below.

There are RadioGroup is used in the example. RadioGroup is used for radio button in group format. All radio buttons are used under RadioGroup.

File Name : MainActivity.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.radiobtn;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

  
 RadioGroup rg;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  rg=(RadioGroup)findViewById(R.id.radioGroup1);
 
 }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 
 public void onClick(View v)
 {
  View root=v.getRootView();
   switch(rg.getCheckedRadioButtonId())
   {
         case R.id.radio0:
      root.setBackgroundColor(Color.YELLOW);
      break;
      
         case R.id.radio1:
      root.setBackgroundColor(Color.MAGENTA);
      break;

          case R.id.radio2:
      root.setBackgroundColor(Color.CYAN);
      break;
   }
 }
}



File Name : activity_main.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
       
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="@string/yellow" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="@string/magenta" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="@string/cyan" />

    </RadioGroup>


</RelativeLayout>

Output :

Change Background Color in Android

Write a Program to calculate the marksheet of students


This example is about built a marksheet of student using java. In this example,
Member Variable : roll_no, sub1, sub2, sub3, sub4, sub5, total, avg
Member Function : getStudentData() , calculateTotal() , calculateAvg(), displayStudentData()

getStudentData() function is used to get all information for the student. calculateTotal() function is used to calculate total of all subjects of marks. calculateAvg() function is used to count average of all subjects. displayStudentData() function is used to display the student information.


File Name : studDetail.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.*;
class stud
{
 int roll_no,sub1,sub2,sub3,sub4,sub5,total,avg;
 String name;

  public void getStudentData() throws Exception
 {
  System.out.println("Enter Roll No. :-");
  DataInputStream in = new DataInputStream(System.in);
  roll_no = Integer.parseInt(in.readLine());

   System.out.println("Enter Name :-");
  name = in.readLine();

     
  System.out.println("Enter Subject 1 Mark :-");
  sub1 = Integer.parseInt(in.readLine());

   System.out.println("Enter Subject 2 Mark :-");
  sub2 = Integer.parseInt(in.readLine());

   System.out.println("Enter Subject 3 Mark :-");
  sub3 = Integer.parseInt(in.readLine());

   System.out.println("Enter Subject 4 Mark :-");
  sub4 = Integer.parseInt(in.readLine());

   System.out.println("Enter Subject 5 Mark :-");
  sub5 = Integer.parseInt(in.readLine());

   
 }

  public void calculateTotal() throws Exception
 {
  total = sub1+sub2+sub3+sub4+sub5;
  
 }

  
 public void calculateAvg() throws Exception
 {
  avg = total/5;
  
 }

  public void displayStudent() throws Exception
 {
  System.out.println("Roll No. :"+roll_no);
  System.out.println("Name :"+name);
  System.out.println("Subject 1 Mark :"+sub1);  
  System.out.println("Subject 2 Mark :"+sub2);
  System.out.println("Subject 3 Mark :"+sub3);
  System.out.println("Subject 4 Mark :"+sub4);
  System.out.println("Subject 5 Mark :"+sub5);
  System.out.println("Total :"+total);
  System.out.println("Average :"+avg);
 }
 
}

class studDetail
{
 public static void main(String args[]) throws Exception
 {
  stud s1 = new stud();
  s1.getStudentData();
  s1.calculateTotal();
  s1.calculateAvg();
  s1.displayStudent();
 }
}
//put details like this
Output :
Enter Roll No. :- 1
Enter Name :- XYZ
Enter Subject 1 Mark : 70
Enter Subject 2 Mark : 80
Enter Subject 3 Mark : 90
Enter Subject 4 Mark : 60
Enter Subject 5 Mark : 50


//display information like this format


Roll No. :1

Name :XYZ
Subject 1 Mark : 70
Subject 2 Mark : 80
Subject 3 Mark : 90
Subject 4 Mark : 60
Subject 5 Mark : 50
Total : 350
Average : 70


Friday, 2 September 2016

Write a Program to calculate the area of rectangle

In this tutorial , we are learning that how to calculate area of the rectangle in java.

The programmer asks users for the length and width values and then output will be calculated and display on console. DataInputStream is used for read primitive java data types from an underlying input stream.

Integer.parseInt() is used to convert data into Integer format.

File Name : RecArea.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
class area
{
 int length,breadth,area;

 public void getRectangleData() throws Exception
 {
  System.out.println("Enter Length :");
  DataInputStream in = new DataInputStream(System.in);
  length = Integer.parseInt(in.readLine());
  System.out.println("Enter Breadth :");
  breadth = Integer.parseInt(in.readLine());
  
 }

 public void calculateArea() throws Exception
 {
  area = length * breadth;
   
 }

 public void displayRectangleData() throws Exception
 {
  System.out.println("Length of the rectangle :"+length);
  System.out.println("Breadth of the rectangle :"+breadth);
  System.out.println("Area of the rectangle :"+area);
 }

}

class RecArea
{
 public static void main(String args[]) throws Exception
 {
  area a1 = new area();
  a1.getRectangleData(); 
  a1.calculateArea();
  a1.displayRectangleData();

 }
}

Like us on Facebook

Site Visitor

Powered by Blogger.