Java Source Codes

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Source Codes as PDF for free.

More details

  • Words: 2,944
  • Pages: 28
Signatu re Not Verified

Vidit Kothari Digitally signed by Vidit Kothari DN: cn=Vidit Kothari, o=Photo Vision, ou=Home, c=IN Date: 2006.10.17 00:45:08 Z

// This program has been written to show the implementation of Constructor Overloading and Parameterized Constructor. import java.io.*; class Program01 { int a,b; public Program01() { a=20; b=43; } public Program01(int a,int b) { this.a=a; this.b=b; } public void findSum() { int sum=a+b; System.out.println("Sum of " + a + b + "=" + sum); } public void findDiff() { int diff=a-b; System.out.println("Difference between " + a + b + " = " + diff); } public void findProduct() { int product=a*b; System.out.println("Product of " + a + b + " = " + product); } public void findQuotient() { int quotient=a/b; System.out.println(a + " divided by " + b + " = " + quotient); } public static void main(String args[]) { Program01 x=new Program01(); Program01 y=new Program01(10,51); x.findSum(); x.findDiff(); x.findProduct(); x.findQuotient(); System.out.println(); y.findSum(); y.findDiff(); y.findProduct(); y.findQuotient(); } }

// // // // // //

Program to print series 1 12 123 1234 12345

import java.io.*; class Program02_1 { public static void makeSeries_a() { for(int i=1;i<6;i++) { for(int j=1;j
class Program02_1_1 { public static void main(String args[]) { for(int i=-3;i<=1;i++) { for(int j=1;j<=(i+4);j++) { System.out.print(j); } System.out.println(); } } } // Program to print series // 12345 // 1234 // 123 // 12 // 1 import java.io.*; class Program02_2 { public static void makeSeries_a() { for(int i=5;i rel="nofollow">=1;i--) { for(int j=i;j>=1;j--) { System.out.print("*"); } System.out.println(); } } public static void makeSeries_a(int a) { for(int i=a;i>=1;i--) { for(int j=i;j>=1;j--) { System.out.print("*"); } System.out.println(); } } public static void makeSeries_1() { for(int i=6-1;i>=1;i--) { for(int j=i;j>=1;j--) { System.out.print(j); } System.out.println();

} } public static void makeSeries_1(int a) { for(int i=a;i>=1;i--) { for(int j=i;j>=1;j--) { System.out.print(j); } System.out.println(); } } public static void main(String arg[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); makeSeries_a(); makeSeries_1(); System.out.println(); System.out.println("Enter a number till which a series will be be printed"); int a=Integer.parseInt(y.readLine()); makeSeries_1(a); makeSeries_a(a); } } // Program to print series // 1 // 12 // 123 // 1234 // 12345 import java.io.*; class Program02_3 { public static void makeSeries_a() //It will print * ** *** **** ***** { for(int i=1;i<=5;i++) { for(int j=1;j
System.out.println(); } } public static void makeSeries_m() //It will print***** **** *** ** * { for(int i=5;i>=1;i--) { for(int j=i;j>=1;j--) { System.out.print("*"); } System.out.println(); } } public static void makeSeries_m(int a) //It will print ..... ***** **** *** ** * { for(int i=a;i>=1;i--) { for(int j=i;j>=1;j--) { System.out.print("*"); } System.out.println(); } } public static void makeSeries_1() //It will print 1 12 123 1234 12345 { for(int i=1;i<=5;i++) { for(int j=1;j=1;i--) { for(int j=i;j>=1;j--) { System.out.print(j); }

System.out.println(); } } public static void makeSeries_1m(int a) //It will print ..... 54321 4321 321 21 1 { for(int i=a;i>=1;i--) { for(int j=i;j>=1;j--) { System.out.print(j); } System.out.println(); } } public static void main(String arg[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); makeSeries_1(); makeSeries_1m(); System.out.println(); makeSeries_a(); makeSeries_m(); System.out.println(); System.out.println(); System.out.println("Enter a number till which a series will be be printed"); int a=Integer.parseInt(y.readLine()); System.out.println(); makeSeries_1(a); makeSeries_1m(a); System.out.println(); makeSeries_a(a); makeSeries_m(a); } } import java.io.*; class Program03 { public void getMagicNum(int n) { int a=0; while(a==0) { a=a+n/10; } if(a==1) System.out.println("The number entered is a Magic Number"); else System.out.println("The number entered is not a Magic Number"); } public static void main(String args[])throws IOException { InputStreamReader z=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(z);

System.out.println("Enter a number"); int n=Integer.parseInt(y.readLine()); Program03 x=new Program03(); x.getMagicNum(n); } } class Program04_1 { public static void main(String args[]) { for(int i=5;i>0;i--) { for(int j=i;j<=5;j++) { System.out.print(j); } System.out.println(); } } } class Program04_2 { public static void main(String args[]) { for(int i=0;i<5;i++) { for(int j=(5-i);j<=5;j++) { System.out.print(j); } System.out.println(); } } } class Program04_3 { public static void main(String args[]) { for(int i=1;i<=5;i++) { for(int j=(5-(i-1));j<=5;j++) { System.out.print(j); } System.out.println(); } } }

// // // // // //

Program to print 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

class Program05 { public static void main(String args[]) { int x=1; for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) { System.out.print(x + "\t"); if(x==1) x=0; else x=1; } System.out.println(); }}} class Program06_1 { public static void main(String args[]) { int a=5; for(int i=1;i<=5;i++) { for(int j=1;j<=a;j++) { System.out.print(j); } System.out.println(); a--; } } } class Program06_2 { public static void main(String args[]) { for(int i=5;i>0;i--) { for(int j=1;j<=i;j++) { System.out.print(j); } System.out.println(); } } }

class Program06_3 { public static void main(String args[]) { for(int i=4;i>=0;i--) { for(int j=1;j<=(i+1);j++) { System.out.print(j); } System.out.println(); } } } class Program07_1 { public static void main(String args[]) { for(int i=-3;i<=1;i++) { for(int j=1;j>=i;j--) { System.out.print(" "); } for(int j=(i+4);j>=1;j--) { System.out.print(j); } System.out.println(); } } } class Program07_2 { public static void main(String args[]) { for(int g=1;g<=5;g++) { for(int k=1;k<=g;k++) { System.out.print(" "); } for(int j=g;j>=1;j--) { System.out.print(j); } System.out.println(); } } } class Program08_1 { public static void main(String args[])

{ for(int i=5;i>=1;i--) { for(int j=5;j>=i;j--) { System.out.print(j); } System.out.println(); } } } class Program08_2 { public static void main(String args[]) { for(int i=5;i>=1;i--) { for(int j=1;j<=i;j++) { System.out.print(" "); } for(int j=5;j>=i;j--) { System.out.print(j); } System.out.println(); } } } import java.io.*; class Program09_1 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter A Sentence (String)"); String s=y.readLine(); s=s+" "; int sl=s.length(); char c; for(int i=0;i<=sl-1;i++) { c=s.charAt(i); if(c==' ') { for(int j=0;j<=i;j++) { System.out.print(s.charAt(j)); } System.out.println(); } }

} } import java.io.*; class Program09_2 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter A Sentence (String)"); String s=y.readLine(); s=s+" "; int sl=s.length(); char c; for(int i=sl-1;i>=0;i--) { c=s.charAt(i); if(c==' ') { for(int j=i;j>=0;j--) { System.out.print(s.charAt(j)); } System.out.println(); } } } } import java.io.*; class Program09_3 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter A Sentence (String)"); String s=y.readLine(); s=" "+s; int sl=s.length(); char c; int k=0; for(int i=sl-1;i>=0;i--) { c=s.charAt(i); if(c==' ') { k=i; for(int j=i;j<=sl-1;j++) { System.out.print(s.charAt(j)); } System.out.println(); } } } }

class ASCII_1 { public static void main(String args[]) { for(int n=1;n<=100;n++) { char a=(char)n; System.out.print(a); } System.out.println(); } } class ASCII_2 { public static void main(String args[]) { char a=(char)33; System.out.print(a); } } import java.io.*; class ASCII_3 { public static void ASCII(int a) { char c=(char)a; System.out.println(); System.out.println("The ASCII character for "+a+" is "+c); } public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); int a=Integer.parseInt(y.readLine()); ASCII(a); } } import java.io.*; class Ch_1 { public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader (System.in)); System.out.println("Enter a sentence"); String sen=y.readLine(); int sl=sen.length(); int a=0; for(int i=0;i<sl;i++) { for(int k=0;k<=i;k++) { char b=sen.charAt(i); System.out.println(b); } } } }

import java.io.*; class Ch_2 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter a sentence"); String sen=y.readLine(); String v="AEIOUaeiou"; int nv=0,nc=0,nbs=0,nspc=0,nd=0; int vl=v.length(); int senl=sen.length(); for(int i=0;i<senl;i++) { char c=sen.charAt(i); if((c>='A' && c<='Z') || (c>='a' && c<='z')) { for(int k=0;k=1 && c<=9) nd++; else nspc++; } System.out.println(" Number of vowels = " + nv); System.out.println(" Number of consonants = " + nc); System.out.println(" Number of digits = " + nd); System.out.println(" Number of special characters = " + nspc); } } class Account { private int ac_num; private String ac_name; private float bal; public Account() { ac_num=0; ac_name=null; bal=0.0f; } public void getValue(int a, String c, float b) { ac_num=a; ac_name=c; bal=b; }

public void display() { System.out.println("Account number = " + ac_num); System.out.println("Account holder's name = " + ac_name); System.out.println("Account balance = " + bal); } public void deposit(float ba) { bal=bal+ba; System.out.println("New amount after depositing " + ba + " = " + bal); } public void withdrawl(float bala) { bal=bal+bala; System.out.println("New amount after withdrawing " + bala + " = " + bal); } public static void main (String args[]) { Account x=new Account(); x.getValue(5456375,"Vidit",9000991); x.display(); x.deposit(10099); x.withdrawl(0); x.display(); } } class BankAccount { int AcNum; String AcName; float AcBalance; public BankAccount() { AcNum=0; AcName=null; AcBalance=0.0f; } public BankAccount(int acnum, String acname, float acbalance) { AcNum=acnum; AcName=acname; AcBalance=acbalance; } } class Employee_001 { int code,basic,PF; float hra,da; String name; public Employee_001() { basic = 0; hra = 0.0f; da=0.0f; PF=0;

} public Employee_001(int b,int c, String n) { basic = b; code = c; name = n; hra = 0.1f*basic; da = 0.55f*basic; PF = 1000; } public static void main(String args[]) { Employee_001 x = new Employee_001(50000,12351,"Vidit"); double NetSalary=(x.basic+x.da+x.hra)-x.PF; System.out.println("NetSalary of " + x.name + " = " + NetSalary); } } class Employee_002 { int basic; double hra,da,PF; static Employee_002 x=new Employee_002(100000); public Employee_002(int bas) { basic = bas; hra = (25.0/100.0) * basic; da = (15.0/100.0) * basic; PF = 2.0*((12.0/100.0) * basic); } public void findGross() { double gr=(x.hra + x.da + x.basic)-(x.PF / 2.0); System.out.println("Gross Income = " + gr); } public static void main(String args[]) { x.findGross(); } } class SeriesFunctions { public static double findFormula(double a,double b) { double formula=((4.25*a) + (b*b)) / (a-b); return formula; } public static void main(String args[]) { double a=2.2d,b=3.5d; for(int i=1;i<=5;i++) { System.out.println(findFormula(a,b)); a=a+0.1; b=1+b; } } }

import java.io.*; class Input_WCnt { public static void main(String args[])throws IOException { BufferedReader y = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Type a sentence"); String a = y.readLine(); int c=0,b,d,f; b = a.length(); for( d=0; d<=(b-1); d++) { if (a.charAt(d) == (' ')) { c+=1; } } System.out.println(c+1); } } import java.io.*; class Input_WCnt_2 { public static void main(String args[])throws IOException { BufferedReader y = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Type a sentence"); String a = y.readLine(); int c=0,b,d,f; b = a.length(); char e; for( d=0; d<=(b-1); d++) { e = a.charAt(d); if (e == (' ')) { c+=1; } } System.out.println(c+1); } } import java.io.*; class Series_InputSentence { public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER A WORD"); String word=y.readLine(); int wl=word.length(); char c; for(int i=0;i<wl;i++) { c=word.charAt(i);

for(int x=1;x<=i;x++) { System.out.print(" "); } System.out.println(c); } } } import java.io.*; class Series_InputWord { public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader(System.in)); System.out.println(" ENTER A SENTENCE"); String word=y.readLine(); int wl=word.length(); char c; int j=0; char wr; for(int i=0;i<wl-1;i++) { c=word.charAt(i); if (c==' ') { for(j=0;j
Array

import java.io.*; class Search_1 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); int a[]={10,16,23,5,13,18,9,11,53,79}; System.out.println("Enter a number to be searched"); int n=Integer.parseInt(y.readLine()); int z=0; for(int i=0;i<10;i++) { if(n==a[i]) { System.out.println("the index of " + n + " = " + (i+1)); z++; } } if(z==0) System.out.println("Your number did not match with any number"); } } import java.io.*; class Search_2 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); int a[]={10,16,23,5,13,18,9,11,53,79}; System.out.println("Enter a number to be searched"); int n=Integer.parseInt(y.readLine()); for(int i=0;i<10;i++) { if(n==a[i]) { System.out.println("the index of " + n + " = " + (i+1)); break; } else if(i==9 && n!=a[i]) { System.out.println("Your number did not match with any number"); break; } } } } import java.io.*; class Search_3 { public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in);

BufferedReader y=new BufferedReader(x); System.out.println("Enter two numbers to be searched"); int a1=Integer.parseInt(y.readLine()); int a2=Integer.parseInt(y.readLine()); int a[]={10,16,23,5,13,18,9,11,53,79}; System.out.println(); System.out.println(); System.out.println("For " + a1); for(int i=0;i<10;i++) { if(a1==a[i]) { System.out.println("the index of " + a1 + " = " + (i+1)); break; } else if(i==9 && a1!=a[i]) { System.out.println("Your number did not match with any number"); break; } } System.out.println(); System.out.println(); System.out.println("For " + a2); for(int i=0;i<10;i++) { if(a2==a[i]) { System.out.println("the index of " + a2 + " = " + (i+1)); break; } else if(i==9 && a2!=a[i]) { System.out.println("Your number did not match with any number"); break; } } } } import java.io.*; class Search_4 { public void numSearch(int b)throws IOException { for(int i=1;i<=b;i++) { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter the number to be searched"); int n=Integer.parseInt(y.readLine()); int a[]={10,16,23,5,13,18,9,11,53,79}; for(int j=0;j<10;j++) {

if(n==a[j]) { System.out.println("the index of " + n + " = " + (j+1)); break; } else if(j==9 && n!=a[j]) { System.out.println("Your number did not match with any number"); break; } } System.out.println(); } } public static void main(String args[])throws IOException { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter how many numbers you have to search"); int ns=Integer.parseInt(y.readLine()); Search_4 o=new Search_4(); o.numSearch(ns); } } import java.io.*; class LinearSearch_1 { public static void main(String args[])throws IOException { BufferedReader x=new BufferedReader(new InputStreamReader(System.in)); String s[]={"UTTAR PRADESH","KERALA","WEST BENGAL","TAMILNADU","BIHAR", "KARNATAKA","MADHYA PRADESH","MAHARASHTRA","ANDHRA PRADESH","MIZORAM"}; String c[]={"LUCKNOW","TIRUVANANTHPURAM","KOLKATA","CHENNAI","PATNA", "BANGALORE","BHOPAL","MUMBAI","HYDERABAD","AIZAWL"}; System.out.println("Enter a name of a state whose capital you want to know"); String st=x.readLine(); for(int i=0;i<10;i++) { if(st.equalsIgnoreCase(s[i])) { System.out.println("The capital of " + s[i] + " is " + c[i]); break; } else if(i==9 && st.toUpperCase()!=s[i]) { System.out.println("No matches found"); break; } } } }

import java.io.*; class LinearSearch_2 { public void stateSearch(int n)throws IOException { BufferedReader x=new BufferedReader(new InputStreamReader(System.in)); String s[] = {"UTTAR PRADESH","KERALA","WEST BENGAL","TAMILNADU","BIHAR", "KARNATAKA","MADHYA PRADESH","MAHARASHTRA","ANDHRA PRADESH","MIZORAM"}; String c[] = {"LUCKNOW","TIRUVANANTHPURAM","KOLKATA","CHENNAI","PATNA", "BANGALORE","BHOPAL","MUMBAI","HYDERABAD","AIZAWL"}; for(int j=1;j<=n;j++) { System.out.println("Enter a name of a state whose capital you want to know"); String st=x.readLine(); for(int i=0;i<10;i++) { if(st.equalsIgnoreCase(s[i])) { System.out.println("The capital of " + s[i] + " is " + c[i]); break; } else if(i==9 && st.toUpperCase()!=s[i]) { System.out.println("No matches found"); break; } } } } public static void main(String args[])throws IOException { BufferedReader x=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Number of states to be searched"); int n=Integer.parseInt(x.readLine()); LinearSearch_2 ob=new LinearSearch_2(); ob.stateSearch(n); } } import java.io.*; class Accept_Sum { public static void main(String args[])throws IOException { int nums[]=new int[10]; InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); int so=0,se=0; for(int i=0;i<=9;i++) { System.out.println("Enter Ten (10) numbers"); nums[i]=Integer.parseInt(y.readLine()); if(nums[i]%2==0) se+=nums[i]; else so+=nums[i]; } System.out.println(" Total sum of the even numbers = " + se); System.out.println(); System.out.println(" Total sum of the odd numbers = " + so); } } import java.io.*; class Accept_Sum_new { public static void main(String args[])throws IOException { BufferedReader x=new BufferedReader(new InputStreamReader(System.in)); String n[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; System.out.println("Enter the number(0-9) which to be spelt"); int num=Integer.parseInt(x.readLine()); for(int i=0;i<10;i++) { if(num==i) { System.out.println(num + " in letters is " + n[i]); break; }

else if(i==9 && num!=i) { System.out.println("Not match found"); break; } } } } //This program is to show how to accept anything in array enabled variable import java.io.*; class Basic_Accept { public static void main(String args[])throws IOException { int r[]=new int[5]; String name[]=new String[5]; for(int i=0;i<=4;i++) { InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter Name"); name[i]=y.readLine(); System.out.println("Enter Roll No."); r[i]=Integer.parseInt(y.readLine()); System.out.println(); } System.out.println("Name\t\tRoll No."); for(int i=0;i<=4;i++) { System.out.println(name[i] + "\t\t" + r[i]); } } }

Functions

import java.io.*; class Func_pl_mi_pr_di { public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number "); double n1=Double.parseDouble(y.readLine()); System.out.println("Enter another number"); double n2=Double.parseDouble(y.readLine()); Func_Subs.subs(n1,n2); Func_Sum.sum(n1,n2); Func_Pro.pro(n1,n2); Func_Quo.quo(n1,n2); } }

import java.io.*; class Func_Subs { public static double subs(double a, double b) { double diff=a-b; System.out.println("The difference of " + a + " and "+ b +" = " + diff); return diff; } }

import java.io.*; class Func_Pro { public static double pro(double a, double b) { double pro=a*b; System.out.println("The difference of " + a + " and "+ b +" = " + pro); return pro; } }

import java.io.*; class Func_Quo { public static double quo(double a, double b) { double quo=a/b; System.out.println("The difference of " + a + " and "+ b +" = " + quo); return quo; } }

import java.io.*; class Func_Sum { public static double sum(double a, double b) { double sum=a+b; System.out.println("The sum of " + a + " and "+ b +" = " + sum); return sum; } } ---------------------------------------------------------------------------------------------------------

import java.io.*; class Func_Max_Char_Main { public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a sentence"); String s1=y.readLine(); System.out.println(); System.out.println("Enter another sentence"); String s2=y.readLine(); Func_Max_Char obj1=new Func_Max_Char(); obj1.string(s1,s2); } }

class Func_Max_Char { public static void string(String a, String b) { int aa=a.length(),bb=b.length(); if (aa>bb) { System.out.println(); System.out.println("The first sentence has more characters"); } else if(bb>aa) { System.out.println(); System.out.println("The second sentence has more characters"); } else { System.out.println(); System.out.println("Both the sentences have the same number of characters"); } } }

class SeriesFunctions { public static double findFormula(double a,double b) { double formula=((4.25*a) + (b*b)) / (a-b); return formula; } public static void main(String args[]) { double a=2.2d,b=3.5d; for(int i=1;i<=5;i++) { System.out.println(findFormula(a,b)); a=a+0.1; b=1+b; } } }

import java.io.*; class Func_Max_Num { public static void string(int a,int b) { if (a>b) { System.out.println(); System.out.println("The first number is greater"); } else if (b>a) { System.out.println(); System.out.println("The second number is greater"); } else { System.out.println(); System.out.println("Both numbers are equal"); } } public static void main(String args[])throws IOException { BufferedReader y=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); int s1=Integer.parseInt(y.readLine()); System.out.println(); System.out.println("Enter another number"); int s2=Integer.parseInt(y.readLine()); string(s1,s2); } }

Related Documents

Java Source Codes
November 2019 9
Source Codes
June 2020 1
Java Codes
November 2019 7
Java Source
November 2019 8
Codes
October 2019 37
Codes
October 2019 43