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

External Program Outputs

The document contains Java code snippets and documentation for multiple external programs: 1) The first program displays all even numbers between 1 to 20 using a for loop and if statement. 2) The second program creates a Student class with name, roll number, and branch data members and displays the values. 3) The third program checks if a user-input string is a palindrome. 4) The remaining programs cover additional concepts like exceptions, packages, threads, file operations, and more.

Uploaded by

Atharv Khadatare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

External Program Outputs

The document contains Java code snippets and documentation for multiple external programs: 1) The first program displays all even numbers between 1 to 20 using a for loop and if statement. 2) The second program creates a Student class with name, roll number, and branch data members and displays the values. 3) The third program checks if a user-input string is a palindrome. 4) The remaining programs cover additional concepts like exceptions, packages, threads, file operations, and more.

Uploaded by

Atharv Khadatare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

EXTERNAL PROGRAMS

/** Develop a program to display all the even numbers between 1 to 20 using for loop and if statement
*/

classexteven

public static void main(String arg[])

System.out.print("\n Even Numbers Between 1 to 20 are :- ");

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

if(i%2==0)

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

/** Develop a Program to create a class Student with data members student_name, roll_no and branch.

Initialize and display values of data members */

class Student
{

String student_name="Srushti",branch="Syco";

introll_no=60;

void display()

System.out.print("\n Name = "+student_name+"\n Branch = "+branch+"\n Roll Number = "+roll_no);

public static void main(String arg[])

Student s=new Student();

s.display();

//extstud.java

*************************************************************************************

/** Develop a program to check entered String is palindrome or not */

importjava.util.*;

classextpalinstring

{
public static void main(String arg[])

String original, reverse = "";

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string to check if it is a palindrome :- ");

original = sc.nextLine();

int length = original.length();

for ( int i = length - 1; i >= 0; i-- )

reverse = reverse + original.charAt(i);

if (original.equals(reverse))

System.out.println("<<<<<<<<<<<<< Entered string/number is a palindrome >>>>>>>>>>>>>>>");

else

System.out.println("<<<<<<<<<<<<< Entered string/number isn't a palindrome


>>>>>>>>>>>>>>");

//extpalinstring.java

************************************************************************************
/** Define an exception called 'No match Exception'that is thrown when a string is not equal to
"MSBTE". Write program */

importjava.util.*;

import java.io.*;

classNoMatchException extends Exception

String str;

NoMatchException(String a)

super(a);

public String toString()

return " Invalid String (No Matched Found With MSBTE String) "+str;

classextnomatchexc

public static void main(String arg[])

String str;

Scanner sc=new Scanner(System.in);

System.out.print("\n Enter a String :- ");

str=sc.next();

if(!str.equals("MSBTE"))

{
try

throw new NoMatchException(str);

catch(Exception e)

System.out.print(e);

else

System.out.print("\n Match Found with MSBTE String ");

*************************************************************************************

/** Write a program that will count no. of characters in a file. */

-------------------------------------------------

Srushti.txt file
hello!!!!!

Welcome to Sveri College

we are from syco class

--------------------------------------------------

import java.io.*;

public class extcountcharfile

public static void main(String arg[])

BufferedReader reader = null;

intcharCount = 0;

try

reader = new BufferedReader(new FileReader("srushti.txt"));

String currentLine = reader.readLine();

while (currentLine != null)

String[] words = currentLine.split(" ");

for (String word : words)

charCount = charCount + word.length();

currentLine = reader.readLine();

}
System.out.println("Number Of Chars In A File : "+charCount);

catch (IOException e)

e.printStackTrace();

finally

try

reader.close();

catch (IOException e)

e.printStackTrace();

*************************************************************************************
/** Define a Package Named "myPackage"to include a class named "DisplayMsg" with one method to
display some message. Develop a program to import this package in a java application and call the
method difined in the package */

Package myPackage;

public class displayMsg

public void display()

System.out.print("\n…. Be Unique ….");

//Accessing the Package

Import myPackage.displayMsg;

public class extpack

public static void main(String arg[])

displayMsg dm=new displayMsg();

dm.display();

}
***********************************************************************************

/** Write a program to input name and age of person and throws user defined exception, if entered age
is negative. */

Import java.util.*;

import java.io.*;

classNegativeAgeException extends Exception

int age;

NegativeAgeException(int a)

age=a;

public String toString()

return "You Entered Invalid Age :- "+age;

classextageexc

public static void main(String arg[])

int age;

Scanner sc=new Scanner(System.in);

System.out.print("\n Enter Name :- ");

String nm=sc.next();
System.out.print("\n Enter Your Age :- ");

age=sc.nextInt();

if(age<0)

try

throw new NegativeAgeException (age);

catch(NegativeAgeException ne)

System.out.print(ne);

else

System.out.print("\n Name = "+nm);

System.out.print("\n Age = "+age); }

}
*************************************************************************************

/** Write a program to create two threads so one thread will print ascending numbers whereas second
thread will print descending numbers between sequence 1 to 15 numbers */

class ascending extends Thread

public void run()

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

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

class descending extends Thread

public void run()

for(int i=15;i>1;i--)

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

classextthread

{
public static void main(String arg[])

ascending a=new ascending();

a.start();

descending d=new descending();

d.start();

**After adding Synchronized Keyword Output Looks like :-**

/** Develop a program to create an applet to display the message "Welcome to the worlds of Applet
"with green color background and red color foreground.*/

Import java.applet.*;

Import java.awt.*;

public class extapp extends Applet

public void init()

setBackground(Color.green);
setForeground(Color.red);

public void paint(Graphics ge)

ge.drawString("Welcome to the Worlds of Applet ",50,100);

**********************************************************************************

/**Develop a program to copy the contents of the file "abc.txt"into a new file "xyz.txt"*/

------------------------------------------------------------------------------------------------

Hello !!!!

Welcome to ShriVitthal Education and Research Institute of Engineering (Polytechnic)Pandharpur,

******** We are from SYCO class **********

------------------------------------------------------------------------------------------------
import java.io.*;

class extcopyfile

public static void main(String arg[])throws IOException

FileInputStream fin=new FileInputStream("abc.txt");

FileOutputStream fout=new FileOutputStream("xyz.txt");

int b;

do

b=fin.read();

fout.write(b);

}while(b!=-1);

//Copied File Successfully


/** Write a program to compute sum of digits of number */

import java.util.*;

class extsumdigit

public static void main(String arg[])

int num, digit, sum=0;

Scanner sc=new Scanner(System.in);

System.out.print("\n Enter the number = ");

num=sc.nextInt();

while(num>0)

digit=num%10;

sum=sum+digit;

num=num/10;

System.out.print("\n Sum of Digits: " +sum);

}
*************************************************************************************

/** Write a program to generate Fibonacci's Series.*/

import java.util.*;

class extfibonacci

public static void main(String arg[])

int n1=0,n2=1,n3,count=0,i;

Scanner sc=new Scanner(System.in);

System.out.print("\n How much numbers you've required into the series ?(Enter total count) :- ");

count=sc.nextInt();

System.out.print(n1+"\t"+n2);

for(i=2;i<count;++i)

n3=n1+n2;

System.out.print("\t"+n3);

n1=n2;

n2=n3;

}
*************************************************************************************

/**Write a program to create a vector with seven elements as [10,20,50,20,40,10,20]. Remove element
at 3rd and 4th position. Insert new element at 3rd position. Display the original and current size of the
vector.*/

import java.util.*;

class extvector

public static void main(String arg[])

Vector v=new Vector(15);

v.addElement(new Integer(10));

v.addElement(new Integer(20));

v.addElement(new Integer(50));

v.addElement(new Integer(20));

v.addElement(new Integer(40));

v.addElement(new Integer(10));

v.addElement(new Integer(20));

System.out.print("\n Original Size of The Vector = "+v.size());

System.out.print("\n Vector Class :- "+v);

v.removeElementAt(3);
v.removeElementAt(4);

System.out.print("\n After Deletion Vector Class :- "+v);

v.insertElementAt(new Integer(30),3);

System.out.print("\n After Insertion :- "+v);

System.out.print("\n Current Size of The Vector = "+v.size());

**********************************************************************************

/**Write a program to accept a number as command line argument and print the number is even or
odd. */

import java.util.*;

class extcmdeven

public static void main(String arg[])

String s1=arg[0];

int n=Integer.parseInt(s1);

if(n%2==0)
{

System.out.print("\n .......... Entered Number is Even .........");

else

System.out.print("\n !!!! Entered Number is Odd !!!!");

**********************************************************************************

/** create an applet to display the string " Applet Graphics Programming " with font " Courier New" ,
size :25 points and BOLD + ITALIC. */

import java.awt.*;

import java.applet.*;

public class extappfontop extends Applet

public void paint(Graphics g)

Font f=new Font("Courier New",Font.BOLD+Font.ITALIC,25);

g.drawString("Applet Graphics Programming", 50,100);

}
}

/*

<applet code="extappfontop.class" width=300 height=300></applet>

*/

/** Accept any string containing all types of characters and symbols from user and count only number of
digits.*/

import java.io.*;

class extcountstrdigit

public static void main(String arg[])throws Exception

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\n Enter a String which Contains numbers,character and also


Symbols :- ");
String sv=br.readLine();

int l=sv.length();

char ch[]=new char[l];

ch=sv.toCharArray();

int c=0;

for(int i=0;i<l;i++)

if(Character.isDigit(ch[i]))

c++;

System.out.print("\n String = "+sv);

System.out.print("\n ------------------------------------------------");

System.out.print("\n Total Number of Digits from Entered String = "+c);

System.out.print("\n ------------------------------------------------");

**********************************************************************************

/** Write a program to check whether entered number is prime or not */

import java.util.*;
class extprime

public static void main(String arg[])

int num,i,count=0;

Scanner sc=new Scanner(System.in);

System.out.print("\n Enter a Number := ");

num=sc.nextInt();

for(i=2;i<=num/2;i=i+1)

if(num%i==0)

count++;

break;

if(num==0||num==1)

System.out.print("\n You entered neigther prime nor composite number ");

else if(count==0)

System.out.print("\nAWW!!!\n...Entered Number is Prime....");

else

System.out.print("\nOOPS !!\n....Entered number is not prime....");

}
*************************************************************************************

/** implement the following

Class : Person Interface : Manager

Name, address accept(),salary()

Class : Employee

ID, basic */

import java.util.*;

interface Manager

public void accept();

public void salary();

class Person

String name,address;
public void accept()

Scanner sc=new Scanner(System.in);

System.out.print("\n Enter Name :- ");

name=sc.next();

System.out.print("\n Enter Your Address :- ");

address=sc.next();

class Employee extends Person implements Manager

int Id,basic;

void getdata()

Scanner s=new Scanner(System.in);

System.out.print("\n Enter ID :- ");

Id=s.nextInt();

System.out.print("\n Enter Basic Salary :- ");

basic=s.nextInt();

public void salary()

System.out.print("\n Name = "+name);


System.out.print("\n ID ="+Id);

System.out.print("\n Address = "+address);

System.out.print("\n Basic Salary = "+basic);

class extinterface

public static void main(String arg[])

Employee emp=new Employee();

emp.accept();

emp.getdata();

emp.salary();

*************************************************************************************
/** Write an applet program that display the logo of Olympic. All the circle are of same radius, assume
the radius.*/

import java.applet.*;

import java.awt.*;

public class extolympic extends Applet

public void paint(Graphics g)

g.setColor(Color.blue);

g.drawOval(50,50,75,75);

g.setColor(Color.black);

g.drawOval(130,50,75,75);

g.setColor(Color.red);

g.drawOval(210,50,75,75);

g.setColor(Color.yellow);

g.drawOval(95,90,75,75);

g.setColor(Color.green);

g.drawOval(175,90,75,75);

/*

<applet code="extolympic.class" width=400 height=400>

</applet>

*/
*************************************************************************

/** Define a class employee with data members as emp_id and salary, Accept data for 5 objects and
print it.*/

import java.util.*;

class employee

int emp_id,salary;

void accept()

Scanner sc=new Scanner(System.in);

System.out.print("\n Enter Employee Id :- ");

emp_id=sc.nextInt();

System.out.print("\n Enter Salary :- ");

salary=sc.nextInt();

void print()

{
System.out.print("\n Id = "+emp_id);

System.out.print("\n Salary= "+salary);

class extemp

public static void main(String arg[])

employee e[]=new employee[5];

System.out.print("\n -------- FILL YOUR DETAILS --------");

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

e[i]=new employee();

e[i].accept();

System.out.print("\n-------- EMPLOYEE DETAILS --------");

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

e[i].print();

}
**********************************************************************************

/** Write a thread program for implementing the 'Runnable interface'.*/

class myThread implements Runnable

synchronized public void run()

for(int i=0;i<=20;i++)

System.out.print("\t"+i);
}

class extrunnableinter

public static void main(String arg[])

myThread m=new myThread();

Thread t=new Thread(m);

t.start();

**********************************************************************************

You might also like