0% found this document useful (0 votes)
4 views

SEM2Java

Uploaded by

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

SEM2Java

Uploaded by

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

JAVA PRACTICAL PROGRAMS

VOWELS IN A STRING
Import java.util.scanner;
Class Vowelsinastring
{
Public static void main(String[]args)
{
String s;
Scanner sc = new Scanner(Systen.in);
System.out.print(“Enter a string:”);
S=sc.nextline();
System.out.println(“Vowels in a string”’+s+”’are:”);
Vowels(s);
}
Static void vowels(String str)
{
Char ch;
Int i = 0;
For(int j = 0;j<str.length();j++)
{
Ch = str.char At(j);
If(ch==’a’|| ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’|| ch==’A’|| ch==’E’|| ch==’I’|| ch==’O’||
ch==’U’||)
{
I = 1;
System.out.println(ch);
}
}
If(i==0)
System.out.println(“There are no vowels in a Entered String”);
}
}

OUTPUT:
Enter a string:
Abcdefghijklmnopqrstuvwxyz
Vowels ia a String
Abcdefghijklmnopqrstuvwxyz
are:
a
e
i
o
u

PLUS SYMBOL
Import java.util.scanner;
Public classplus
{
Public static void main (String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter N:”);
Int n =sc.nextint();
System.out.print(“Enter Symbol:”);
Char c = sc.next().charAt(0);
For(int i=1;j<=n*2-1;i++)
{
If(i!=n)
For(int j = 1;j<=n;j++)
{
If(j==n)
System.out.print(c);
System.out.print(“”);
For(int j=1;j<=n*2-1;j++)
{
System.out,print(c);
}
System.out,println();
}
}
}

OUTPUT:
5
Enter Symbol:+
+
Display Date and Time

import java.util.*;
class GetCurrentDateAndTime
{
public static void main(String args[])
{
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
System.out.println("Current date is "+day+"/"+(month+1)+"/"+year);
System.out.println("Current time is "+hour+" : "+minute+" : "+second);
}
}

OUTPUT:
Current date is 14|3|2024
Current time is 9:30:45

Sort an Array Elements in Ascending Order in Java

import java.util.Scanner;
public class SortArray
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i=0; i<n; i++)
{
a[i]=s.nextInt();
}
for (int i =0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.print("Elements in Ascending Order:");
for (int i=0; i<n-1; i++)
{
System.out.print(a[i]+ ", ");
}
System.out.print(a[n-1]);
}
}

OUTPUT:

Enter no of elements you want in array:5

Enter all the elements:

Elements in Ascending order:1,3,5,6,8


Java Program to Rotate Elements of the List

import java.io.*;
import java.util.*;
class GFG {

public static void main(String[] args)


{

List<Integer> my_list = new ArrayList<>();


my_list.add(10);
my_list.add(20);
my_list.add(30);
my_list.add(40);
my_list.add(50);
my_list.add(60);
my_list.add(70);

System.out.println(
"List Before Rotation : "
+ Arrays.toString(my_list.toArray()));

for (int i = 0; i < 4; i++) {

int temp = my_list.get(6);

for (int j = 6; j > 0; j--) {


my_list.set(j, my_list.get(j - 1));
}
my_list.set(0, temp);
}

System.out.println(
"List After Rotation : "
+ Arrays.toString(my_list.toArray()));
}
}
OUTPUT:
List Before Rotation:[10,20,30,40,50,60,70]
List After Rotation:[30,40,50,60,70,10,20]

DYNAMIC ARRAY IN JAVA


Import java.util.scanner;
Class Example1
{
Public static void main(String[]args)
{
Int I, key = 9;
Int a[]={1,4,7,9,10};
For(i=0;i<a.length;i++)
{
If(a[i]==key)
{
System.out.println(key+”is present at location”+(i+1));
Break;
}
}

OUTPUT:
9 is present at location 4

REVERSE OF A STRING
Import java.util.scanner;
Class Reverseofastring
{
Public static void main(String[]args)
{
Reverse of a string rev = new Reverse of a string();
Scanner sc = new Scanner(System.in)
System.out.print(“Enter a String:”);
String str = sc.nextline();
System.out.println(“Reverse of a string is :”+ rev.revrese (str));
}
Static string reverse(String s)
{
String rev = “ ”;
For(int j = s.length();j>0;--j)
{
Rev = rev + (s.char At(j-1);
}
Return rev;
}
}

OUTPUT:
Enter a String : String
Reverse of a String : gnirtS

DEMONSTRATE SUSPEND ( ) METHOD


import java.io.*;

class GFG extends Thread {


public void run()
{
for (int i = 1; i < 5; i++) {
try {
sleep(5);
System.out.println( "Currently running - "+ Thread.currentThread().getName());
}
catch (InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[])
{

GFG t1 = new GFG();


GFG t2 = new GFG();
GFG t3 = new GFG();

t1.start();
t2.start();
t2.suspend();
t3.start();
}
}

OUTPUT:
Currently running-Thread-0
1
Currently running-Thread-1
1
2
3
4

EXCEPTION HANDLING
public class TryCatchExample6
{
public static void main(String[] args)
{
int i=50;
int j=0;
int data;
try
{
data=i/j;
}
catch(Exception e)
{
System.out.println(i/(j+2));
}
}
}

OUTPUT:

25

MULTI CATCH BLOCK BY EXCEPTION


public class MultipleCatchBlock1 {

public static void main(String[] args) {

try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{ System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
} catch(Exception e)
{ System.out.println("Parent Exception occurs");
} System.out.println("rest of the code");
}
}

OUTPUT:

Arithmetic Exception occurs


rest of the code

JAVA THROWS EXAMPLE


import java.io.IOException;
class Testthrows1
{
void m()throws IOException
{
throw new IOException("device error");
}
void n()throws IOException
{
m();
}
void p()
{
Try
{
n();
}
catch(Exception e)
{
System.out.println("exception handled");
}
}
public static void main(String args[])
{
Testthrows1 obj=new Testthrows1();
obj.p();
System.out.println("normal flow...");
}
}

Output:

exception handled
normal flow...

JAVA THROW EXAMPLE


public class TestThrow1
{
static void validate(int age)
{
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[])
{
validate(13);
System.out.println("rest of the code...");
}
}

OUTPUT:

Exception in thread main java.lang.ArithmeticException:not valid

JAVA RUNNABLE INTERFACE

class Multi3 implements Runnable{


public void run(){
System.out.println("thread is running...");
}

public static void main(String args[]){


Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}

OUTPUT:
thread is running...

SYNCHRONIZATION METHOD IN JAVA


class Table

synchronized void printTable(int n)//synchronized method


{

for(int i=1;i<=5;i++)

System.out.println(n*i);

Try

Thread.sleep(400);

catch(Exception e)

System.out.println(e);

class MyThread1 extends Thread

Table t;

MyThread1(Table t)

this.t=t;

Public void run()

{
t.printTable(5);

class MyThread2 extends Thread

Table t;

MyThread2(Table t)

this.t=t;

public void run()

t.printTable(100);

public class TestSynchronization2

public static void main(String args[])

Table obj = new Table();//only one object

MyThread1 t1=new MyThread1(obj);

MyThread2 t2=new MyThread2(obj);

t1.start();

t2.start();
}

OUTPUT:

5
10
15
20
25
100
200
300
400
500

HELLO WORLD IN APPLET


import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet


{
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}

OUTPUT:

Hello World

DISPLAYING DATE IN APPLET


import java.applet.*;
import java.awt.*;
import java.util.Date;
public class GFG extends Applet
{
public void paint(Graphics g)
{
Date dt = new Date();
super.showStatus("Today is" + dt);
}
}

OUTPUT:

March 14,2024

DISPLAY RECTANGLE IN APPLET


import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
int height, width;
public void init()
{
height = getSize().height;
width = getSize().width;
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
}

OUTPUT:

SAMPLE BANNER IN APPLET


import java.awt.*;
import java.applet.*;

public class SampleBanner extends Applet implements Runnable {


String str = "This is a simple Banner ";
Thread t ;
boolean b;

public void init() {


setBackground(Color.gray);
setForeground(Color.yellow);
}
public void start() {
t = new Thread(this);
b = false;
t.start();
}
public void run () {
char ch;
for( ; ; ) {
try {
repaint();
Thread.sleep(250);
ch = str.charAt(0);
str = str.substring(1, str.length());
str = str + ch;
}
catch(InterruptedException e) {}
}
}
public void paint(Graphics g) {
g.drawRect(1,1,300,150);
g.setColor(Color.yellow);
g.fillRect(1,1,300,150);
g.setColor(Color.red);
g.drawString(str, 1, 150);
}
}

OUTPUT:

Sample Banner

You might also like