Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

My Java Programs

Uploaded by

niveditha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

My Java Programs

Uploaded by

niveditha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

public class EmployeeVoting

{
public static void main(String[] args)
{
int age=12;
if(age>=18)
{
System.out.println("Eligible to vote");
}
else
{
System.out.println("not Eligible to vote");
}
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------
VOTING ELIGIBILITY
import java.util.Scanner;
public class EmployeeVoting

{
public static void main(String args[])
{
int age;
Scanner c=new Scanner(System.in);
System.out.println("Enter the Voter's Age:");
age=c.nextInt();
if(age>=18)
{
System.out.println("YOU CAN VOTE");
}
else
{
System.out.println("YOU CANNOT VOTE");
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------
POSITIVE NUMBER
import java.util.Scanner;

public class NumberValidation


{
public static void main(String args[])
{
int number;
Scanner a=new Scanner(System.in);
System.out.println("Enter the Number");
number=a.nextInt();
if(number>=0)
{
System.out.println("THIS IS A POSITIVE NUMBER");
}
else
{
System.out.println("THIS IS A NEGATIVE NUMBER");
}
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------
Extend the previous program to check whether the given number is positive, zero or
negative. (Hint: use if-else conditions) [NESTED IF....ELSE]

import java.util.Scanner;

public class NumberValidation


{
public static void main(String args[])
{
int number;
Scanner a=new Scanner(System.in);
System.out.println("Enter the Number");
number=a.nextInt();
if(number==0)
{
System.out.println("THIS IS ZERO");
}
else
{
if(number>0)
{
System.out.println("THIS IS A POSITIVE NUMBER");
}
else
{
System.out.println("THIS IS A NEGATIVE NUMBER");
}
}
}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------
Write a program to find largest of two numbers.

ENTER NUMBER1
23

ENTER NUMBER2
45

IF NUMBER1>NUMBER2
PRINT NUMBER1 IS GREATER
ELSE
PRINT
NUMBER2 IS GREATER

import java.util.Scanner;
public class BiggerNumber
{
public static void main(String args[])
{
int NUMBER1, NUMBER2;
System.out.println("Enter Number1");
Scanner NUM1=new Scanner(System.in);
NUMBER1=NUM1.nextInt();
System.out.println("Enter Number2");
Scanner NUM2=new Scanner(System.in);
NUMBER2=NUM2.nextInt();
if(NUMBER1>NUMBER2)
{
System.out.println("Number1 is bigger");
}
else
{
System.out.println("Number2 is bigger");
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------
Write a program to check given number is even or odd. (Hint: use % operator)

ENTER NUMBER
23
IF(NUMBER%2==0)
EVEN NUMBER
ELSE
ODD NUMBER

import java.util.Scanner;
public class EvenOdd
{
public static void main(String args[])
{
int NUMBER;
System.out.println("Enter the number");
Scanner A=new Scanner(System.in);
NUMBER=A.nextInt();
if(NUMBER%2==0)
{
System.out.println("This is an EVEN number");
}
else
{
System.out.println("This is an ODD number");
}
}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------

2
4
6
8
10
12
14
16
18
20

1
3
5
7
9
11
13
15
17
19
public class PrintEvenOdd
{
public static void main(String[] args)
{

int limit = 50;


System.out.println("Printing Even numbers between 1 and " + limit);
for(int i=1; i <= limit; i++)
{
if( i % 2 == 0)
{

System.out.print(i + " ");


}
}
System.out.println();
System.out.println("Printing Odd numbers between 1 and " + limit);
for(int i=1; i <= limit; i++)
{
if( i % 2 != 0)
{

System.out.print(i + " ");


}
}
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
FIND THE LARGEST NUMBER IN AN ARRAY

import java.util.Scanner;
public class Largest_Number
{
public static void main(String[] args)
{
int n, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
max = a[0];
for(int i = 0; i < n; i++)
{
if(max < a[i])
{
max = a[i];
}
}
System.out.println("Maximum value:"+max);
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------
INHERITANCE- SINGLE

public class parent


{

int salary = 10000;


}

class child extends parent


{
int bonus = 5000;
public static void main(String[] args)
{
child property = new child();
System.out.println(property.salary);
System.out.println(property.bonus);
}
}
-----------------------------------------------------------------------------------
------------
INHERITANCE - MULTILEVEL

public class Multilevel


{
String name1 = "Subramani";
int property1 = 10000000;
}
class appa extends Multilevel
{
String name2 = "Ravi";
int property2 = 3000;
}

class nive extends appa


{
String name3 = "Niveditha";
int property3 = 10000;
public static void main(String[] args)
{
nive ob = new nive();
System.out.println(ob.name1);
System.out.println(ob.property1);
System.out.println(ob.name2);
System.out.println(ob.property2);
System.out.println(ob.name3);
System.out.println(ob.property3);
}
}
-----------------------------------------------------------------------------------
-----------------
XML PARSER
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class XMLReader


{
public static void main(String[] args)
{
SAXBuilder builder = new SAXBuilder();

Document xml = null;


try {
xml = builder.build(new File("sample2.xml"));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Element root = xml.getRootElement();


System.out.println(root.getName());
System.out.println(root.getChildren().size());

List books = root.getChildren();


Iterator itr = books.iterator();
while(itr.hasNext())
{
Element book = (Element) itr.next();
System.out.println("ISBN : " + book.getAttributeValue("ISBN"));

System.out.println("author : " + book.getChildText("author"));


System.out.println("title : " + book.getChildText("title"));
System.out.println("pages : " + book.getChildText("pages"));
System.out.println("language : " +
book.getChildText("language"));
}
}
}
-----------------------------------------------------------------------------------
-----------------------------

You might also like