Month: November 2017
Android List View example
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <ListView android:id="@+id/cities" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@color/colorAccent" android:dividerHeight="1dp"> </ListView> </LinearLayout>
strings.xml
<resources> <string name="app_name">Cities-ListView</string> </resources>
MainActivity.java
package com.example.abhay.listviewdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ListView lv; String[] arrcities = {"London","Paris","New York","Frankfurt", "Warsaw","Tokyo","Rome","Toronto"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrcities); lv = (ListView) findViewById(R.id.cities); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int selectedCityPosition = position; String selectedCity = (String) lv.getItemAtPosition(position); selectedCityPosition ++; Toast.makeText(getApplicationContext(), "You have selected city at :"+selectedCityPosition+" is : " +selectedCity , Toast.LENGTH_LONG) .show(); } }); } }
Output
Java Array List example
ArrayListEx.java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Employee
{
int id;
String name;
String type;
Float salary;
public Employee(int pId, String pName, String pType,float pSal)
{
id=pId;
name=pName;
type=pType;
salary=pSal;
}
}
class Employeedb
{
Scanner obj=new Scanner(System.in);
List<Employee> employees;
public Employeedb()
{
employees=new ArrayList<Employee>();
Employee e1=new Employee(100,”John”,”Permanent”,45000);
Employee e2=new Employee(101,”Mark”,”Contract”,35000);
Employee e3=new Employee(102,”Peter”,”Contract”,50000);
employees.add(e1);
employees.add(e2);
employees.add(e3);
}
public void showAll()
{
System.out.println();
System.out.println(“Employee Listing”);
for(int i=0; i<employees.size(); i++)
{
System.out.println();
System.out.print(employees.get(i).id + ” ” +employees.get(i).name+” ” +employees.get(i).type+” “+employees.get(i).salary );
}
System.out.println();
}
public void deleteEmp()
{
int Flag=0;
String strDelete;
System.out.println();
System.out.println(“\nEmployee record deletion “);
System.out.println();
System.out.print(“Please enter Employee Name : “);
strDelete=obj.next();
for(int i=0; i<employees.size(); i++)
{
if(employees.get(i).name.equals(strDelete))
{
employees.remove(employees.get(i));
Flag=1;
}
}
if(Flag==0)
{
System.out.println(“Employee does not exists”);
}
else
{
System.out.println(“Employee “+strDelete+” ‘s record successfully deleted”);
showAll();
}
}
public void newEmployee()
{
int id;
String name;
String type;
float sal;
System.out.println(“\nPlease new enter employee information\n” );
System.out.print(“Please enter id : “);
id=obj.nextInt();
System.out.print(“Please enter name : “);
name=obj.next();
System.out.print(“Please enter type : “);
type=obj.next();
System.out.print(“Please enter salary : “);
sal=obj.nextFloat();
Employee e=new Employee(id, name, type,sal);
employees.add(e);
showAll();
}
public void updateEmployee()
{
int Flag=0;
String strName;
float fSal;
System.out.println();
System.out.println(“\nEmployee data updating”);
System.out.println();
System.out.print(“Please enter employee name : “);
strName=obj.next();
for(int i=0; i<employees.size(); i++)
{
if(employees.get(i).name.equalsIgnoreCase(strName))
{
System.out.println();
System.out.print(“Please enter new salary amount : “);
fSal=obj.nextFloat();
employees.get(i).salary=fSal;
employees.set(i,employees.get(i));
Flag=1;
}
}
if(Flag==0)
{
System.out.println(“Employee does not exist”);
}
else
{
System.out.println(“\nEmployee salary successfully updated”);
showAll();
}
}
}
public class ArrayListEx
{
public static void main(String args[])
{
Employeedb empDB=new Employeedb();
empDB.showAll();
empDB.deleteEmp();
empDB.newEmployee();
empDB.updateEmployee();
}
}
Output
Java vector example
vdemo.java
import java.util.Vector;
import java.util.Collections;
import java.util.List;
class vectorDemo
{
Vector<String> days ;
public vectorDemo()
{
days = new Vector<String>();
days.add(“Monday”);
days.add(“Tuesday”);
days.add(“Thursday”);
days.add(“Friday”);
days.add(“Satday”);
days.add(“Sunday”);
}
public void display()
{
for(int i=0;i<days.size();i++)
{
System.out.println(days.get(i));
}
}
public void swap()
{
Collections.swap(days,0,6);
System.out.println(“\n\nElement after swap”);
display();
}
public void sort()
{
System.out.println(“\n\nElement after sort”);
Collections.sort(days);
}
public void remove(String day)
{
days.remove(day);
System.out.println(“\n\nElement after removing ” + day);
display();
}
public void insert(int pos, String day)
{
System.out.println(“\n\n”+ “after inserting ” + day);
days.insertElementAt(day,pos);
display();
}
public void subList()
{
List<String> weekend = days.subList(5,7);
System.out.println(“\n\nElement after sublist”);
System.out.println(weekend);
}
public void search(String day)
{
if(days.contains(day))
{
System.out.println(“\n”+ day + ” found at position “+ days.indexOf(day));
}
}
}
public class vdemo
{
public static void main(String args[])
{
vectorDemo obj = new vectorDemo();
obj.display();
obj.insert(2,”Wednesday”);
obj.swap();
obj.subList();
obj.sort();
obj.remove(“Tuesday”);
obj.search(“Monday”);
}
}
Output
HTML Heading tag with style
colortext.html
<html> <head> <title>Colorful text</title> </head> <body> <center> <h1 style="color:coral">Good Morning</h1> <h2 style="color:darkSeaGreen">Good Afternoon</h2> <h3 style="color:chocolate">Good Evening</h3> <h4 style="color:deeppink">How are you?</h4> <h5 style="color:teal">Welcome</h4> <h6 style="color:skyblue">Good Night</h6> <center> </body> </html>
Output
HTML Text formatting in web page
format.html
<html>
<head>
<title>Text formatting </title>
</head>
<body>
<center>
<h3>Text formatting example</h3>
<b>This is bold text.</b><br>
<i>This is italic text.</i><br>
<u>This is underline text </u>
<hr>
<strong>Attention</strong><br>
<em>Silence</em><br>
<mark>Highlighted text</mark>
</center>
</body>
</html>
Output
Java perfect number
perfect.java
import java.util.Scanner;
class numberDemo
{
int iNumber;
Scanner obj = new Scanner(System.in);
public void isPerfect()
{
int iSum=0;
System.out.print(“Please enter number: “);
iNumber = obj.nextInt();
for(int i=1;i<iNumber;i++)
{
if(iNumber % i == 0)
{
iSum = iSum + i;
}
}
if(iSum == iNumber)
{
System.out.println(iNumber + ” is perfect number”);
}
else
{
System.out.println(iNumber + ” is not perfect number”);
}
}
}
public class perfect
{
public static void main(String args[])
{
numberDemo obj = new numberDemo();
obj.isPerfect();
}
}
Output
Java Fibonacci series
fibdemo.java
import java.util.Scanner;
class numberDemo
{
int iNumber;
Scanner obj = new Scanner(System.in);
public void fib()
{
int iTerms;
int fNumber=0;
int sNumber=1;
int fibNumber;
System.out.print(“Please enter terms for Fibonacci series: “);
iTerms = obj.nextInt();
System.out.print(fNumber + “,”+ sNumber);
for(int i=0;i<iTerms – 2;i++)
{
fibNumber=fNumber + sNumber;
System.out.print(“,” + fibNumber);
fNumber = sNumber;
sNumber=fibNumber;
}
}
}
public class fibdemo
{
public static void main(String args[])
{
numberDemo obj = new numberDemo();
obj.fib();
}
Output
}
You must be logged in to post a comment.