Anda di halaman 1dari 16

PATTERN

PROGRAM:import java.io.*; publicclass pattern { publicstaticvoid main(String args[]) throwsIOException { int n=5,m=0,l,i,j,k; InputStreamReader pat=newInputStreamReader(System.in); System.out.println("enter string"); BufferedReaderbr=newBufferedReader(pat); String str=br.readLine(); l=str.length(); for(i=1;i<=n;i++) { for(k=n-i;k>=0;k--) { System.out.println(" "); } for(j=1;j<=i;j++) { if(m==l) m=0; System.out.print(str.charAt(m)+" "); m++; } System.out.print(" "); } } }

MAGIC SQUARE
PROGRAM:package math; import java.io.*; publicclassMagicSquare { publicstaticvoid main(String args[])throwsIOException{ System.out.println("Enter the size of the matrix\n"); BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); int size=Integer.parseInt(br.readLine()); int matrix[][] = newint[size][size]; int visited[][] = newint[size][size]; //For odd matrix size for(inti=0;i<size;i++){ for(int j=0;j<size;j++){ visited[i][j]=0; } } int row=0,col=size/2; intnrow=0,ncol=size/2; int count=1; for(inti=0; i<(size*size);i++){ if(visited[row][col]==0){ matrix[row][col]=count; visited[row][col]=1; } nrow=row-1; ncol=ncol+1; if(nrow<0) nrow=size-1; if(ncol==size) ncol=0; if(visited[nrow][ncol]==0){ row=nrow; col=ncol; } else{ row++; if(row==size) row=0; nrow=row; ncol=col; }

count++; //} } for(int l=0;l<(size);l++){ for(int k=0;k<size;k++) System.out.print("\t"+matrix[l][k]); System.out.println("\n"); } } }

STUDENT DATABASE
PROGRAM:importjava.util.Scanner; importjava.io.*; class student { intmaths,phy,chem, total=0; intdt, mn, yr; longreg; String name = newString(); String add = newString(); String dob = newString(); Scanner sc1 = newScanner(System.in); void marks() { System.out.print("Enter marks for maths: "); maths = sc1.nextInt(); System.out.print("Enter marks for physics: "); phy= sc1.nextInt(); System.out.print("Enter marks for chemistry: "); chem = sc1.nextInt(); total = maths+phy+ chem; } }

/*----------------------------------------------------- CLASS STUDE ------------------------------------------------------*/


importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStreamReader; importjava.util.Scanner; publicclassstude { publicstaticvoid main(String args[]) throwsCloneNotSupportedException,IOException { Scanner sc2 = newScanner(System.in); BufferedReaderobj = newBufferedReader(newInputStreamReader(System.in)); inti,j,opt,n; String srch; System.out.print("Enter the no. of records: "); n = sc2.nextInt(); student[] st = new student[n]; for(i=0;i<n;i++){ //taking input from user st[i] = new student(); System.out.print("\nEnter student's reg no.: "); st[i].reg = sc2.nextInt(); System.out.print("Enter student's name: "); st[i].name = obj.readLine(); System.out.print("Enter student's dob (ddmmyy):\n"); st[i].dob = obj.readLine(); System.out.print("Enter student's address: "); st[i].add = obj.readLine();

System.out.print("Enter the marks\n"); st[i].marks(); } while(true){ Scanner sc3 = newScanner(System.in); System.out.println("\nSearch Menu:\n1. By Reg No.\n2.By Name\n3.By Total Marks\n4. Exit"); System.out.print("Enter your option: "); opt = sc3.nextInt(); switch(opt){ case 1:{ int min; for(i=0;i<n;i++){ min = i; for(j=i+1;j<n;j++){ if(st[j].reg<st[i].reg){ min = j; } } student temp3 = new student(); temp3 = (student) st[i]; st[i] = (student) st[min]; st[min] = (student) temp3; } for(i=0;i<n;i++){ System.out.println("\nReg. No.: "+st[i].reg); System.out.println("Name: "+st[i].name); System.out.println("DOB: "+st[i].dob); System.out.println("Address: "+st[i].add); System.out.println("Marks: maths="+st[i].maths+" physics="+st[i].phy+" chemistry="+st[i].chem); System.out.println("Total: "+st[i].total); } break; } case 2:{ System.out.print("\nEnterserach string: "); srch = sc2.next(); for(i=0;i<n;i++){ if(st[i].name==srch) { continue; } } for(i=0;i<n;i++) { System.out.println("\nReg. No.: "+st[i].reg); System.out.println("Name:"+st[i].name); System.out.println("DOB: "+st[i].dob); System.out.println("Address:"+st[i].add); System.out.println("Marks: maths="+st[i].maths+" physics="+st[i].phy+" chemistry"+st[i].chem); System.out.println("Total:"+st[i].total); } break;

} case 3: marks(descending) {

//case to sort by total

int max; for(i=0;i<n;i++) { max = i; for(j=i+1;j<n;j++) { if(st[j].total>st[i].total) { max = j; } } student temp3 = st[i] = st[max] temp3 = new student(); (student) st[i]; (student) st[max]; = (student) temp3;

} for(i=0;i<n;i++) //printing search result { System.out.println("\nReg. No.: "+st[i].reg); System.out.println("Name: "+st[i].name); System.out.println("DOB: "+st[i].dob); System.out.println("Address: "+st[i].add); System.out.println("Marks: maths="+st[i].maths+" physics="+st[i].phy+" chemistry="+st[i].chem); System.out.println("Total: "+st[i].total); } break; } case 4: //case to exit out of the loop System.exit(0); default: //case to handle wrong input } } } }

INHERITANCE
PROGRAM:import java.io.*; class student { String name,mother,father; inta,c; student(String name,Stringmother,Stringfather,inta,int c) { this.name=name; this.mother=mother; this.father=father; this.a=a; this.c=c; } void display() { System.out.println("enter name:"+name); System.out.println("enter mother name:"+mother); System.out.println("enter father name:"+father); System.out.println("age:"+a); System.out.println("cgpa:"+c); } } classplacestdextends student { String com; intj; placestd(String name,Stringmother,Stringfather,inta,intc,Stringcom,int j) { super(name,mother,father,a,c); this.com=com; this.j=j; } void show() { System.out.println("enter name:"+name); System.out.println("enter mother name:"+mother); System.out.println("enter father name:"+father); System.out.println("age:"+a); System.out.println("cgpa:"+c); System.out.println("enter company name:"+com); System.out.println("joining:"+j); } } classintershipstdextendsplacestd { String company; intsthyphand,duration; intershipstd(String name,Stringmother,Stringfather,inta,intc,String com, intj,intsthyphand,int duration) { super(name,mother,father,a,c,com,j); this.sthyphand=sthyphand; this.duration=duration; } void play() {

super.display(); System.out.println("sthyhand:"+sthyphand); System.out.println("duration:"+duration); } } classstudentdevloperextends student { String project,tool,pur; intm; studentdevloper(String name,Stringmother,Stringfather,inta,intc,intm,Stringproject,Stringtool,Stri ngpur) { super(name,mother,father,a,c); this.m=m; this.project=project; this.tool=tool; this.pur=pur; } void play() { super.display(); int l=c+m; System.out.println("project:"+project); System.out.println("tool"+tool); System.out.println("purpose:"+pur); System.out.println("new cgpa:"+l); } } class demo { publicstaticvoid main(String args[])throwsIOException { InputStreamReaderir=newInputStreamReader(System.in); BufferedReaderbr=newBufferedReader(ir); System.out.println("enter name:"); String name=br.readLine(); System.out.println("enter mother name:"); String mother=br.readLine(); System.out.println("enter father name:"); String father=br.readLine(); System.out.println("age:"); int a=Integer.parseInt(br.readLine()); System.out.println("cgpa:"); int c=Integer.parseInt(br.readLine()); System.out.println("company:"); String com=br.readLine(); System.out.println("joining:"); int j=Integer.parseInt(br.readLine()); System.out.println("intership duration"); int duration=Integer.parseInt(br.readLine()); System.out.println("sthyphand"); intsthyphand=Integer.parseInt(br.readLine()); System.out.println("enter project name"); String project=br.readLine(); System.out.println("company marks"); int m=Integer.parseInt(br.readLine()); System.out.println("tool"); String tool=br.readLine();

System.out.println("purpose"); String pur=br.readLine(); System.out.println("enter choice 1.student, 2.placement, 3.intership,4.studentdevloper: "); int n=Integer.parseInt(br.readLine()); switch(n) { case 1: { student ram=new student(name,mother,father,a,c); ram.display(); break; } case 2: { placestdsita=newplacestd(name,mother,father,a,c,com,j); sita.show(); break; } case 3: { intershipstdhanu=newintershipstd(name,mother,father,a,c,com,j,sthyphand,dur ation); hanu.play(); break; } case 4: { studentdevloper lakh=newstudentdevloper(name,mother,father,a,c,m,project,tool,pur); lakh.play(); break; } default: System.out.println("wrong choice"); } } }

PACKAGE
PROGRAM:packagehomepack; importjava.util.Scanner; publicclass Income { protectedstaticfloatsalary,allowance,rent; publicvoid input(){ System.out.println("Enter income details : "); Scanner buffer = newScanner(System.in); System.out.print("\n Salary : "); salary = buffer.nextFloat(); System.out.print("\n Allowance : "); allowance = buffer.nextFloat(); System.out.print("\n Rent : "); rent = buffer.nextFloat(); } publicstaticfloattotal_inc(){ return (salary+allowance+rent); } }

/*.expenditure*/
importjava.util.*; publicclass Expenditure { protected staticfloatfood,clothing,educational; publicvoid input(){ System.out.println("Enter expenditure details : "); Scanner buffer = newScanner(System.in); System.out.print("\n Food : "); food = buffer.nextFloat(); System.out.print("\n Clothing : "); clothing = buffer.nextFloat(); System.out.print("\n Educational : "); educational = buffer.nextFloat(); } publicstaticfloattotal_exp(){ return (food+clothing+educational); } }

/*..budget.*/
importhomepack.*; publicclass Budget { publicstaticfloat savings(floatexp,floatinc) { return (inc - exp); } publicstaticvoid main(String args[]) { Expenditure expen = newExpenditure(); expen.input(); Income inc = newIncome(); inc.input(); floattot_exp = Expenditure.total_exp();

floattot_inc = Income.total_inc(); floattot_sav = savings(tot_exp,tot_inc); System.out.println("Total Savings = "+tot_sav); } }

LEXICOGRAPHIC ANALYSIS
PROGRAM:importjava.util.*; publicclasslexo { publicstaticvoid main(String args[]){ inti,j,n=0,num,temp1; Scanner input = newScanner(System.in); System.out.println("Enter the no. of characters:"); num = input.nextInt(); n = num; String[] chars = new String[num]; String temp; int[] freq = newint[num]; for(i=0;i<num;i++){ System.out.println("Enter the char:"); chars[i] = input.next(); System.out.println("Enter frequency for the char:"); freq[i] = input.nextInt(); } System.out.println("The given Input are:"); for(i=0;i<num;i++){ System.out.println("("+chars[i]+", "+freq[i]+")"); } for(i=0;i<num;i++){ for(j=i+1;j<num;j++){ if(chars[i].compareTo(chars[j])>0){ temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; temp1 = freq[i]; freq[i] = freq[j]; freq[j] = temp1; } } } System.out.println("The output is:"); for(j=0;j<n;j++){ num = freq[j]; for(i=0;i<num;i++){ System.out.print(chars[j]+" } } input.close(); } }

");

BASE CONVERTOR
PROGRAM:importjava.util.Scanner; publicclassbaseconverter { privatestatic Scanner sc; staticfloatformdigit(int a[],intn,intbasea) { float number=0; inti; for(i=0;i<n;i++) { number+=(1.0/basea)*a[i]; basea*=basea; } return number; } staticStringBuffer conversion(int a[],intbasea,intn,intbaseb,int pre) { inti; float x=formdigit(a,n,basea); System.out.println(x); StringBuffernd=newStringBuffer(); for(i=0;i<pre;i++) { x=x*baseb; if(x>=baseb-1) x=x%baseb; int y=(int)x; nd=nd.append(y); } returnnd; } publicstaticvoid main(String args[])throws Exception { intn,i; sc = new Scanner(System.in); System.out.println("How many digits :"); n=sc.nextInt(); int a[]=newint[n]; for(i=0;i<n;i++) a[i]=sc.nextInt(); intbasea,baseb,pre; System.out.println("Enter the base"); basea=sc.nextInt(); System.out.println("Enter base to be converted"); baseb=sc.nextInt(); System.out.println("Enter precision"); pre=sc.nextInt(); try { if(basea<2 || baseb<2) thrownewIllegalArgumentException(); for(i=0;i<n;i++) if(a[i]>=basea) thrownewIllegalArgumentException();

StringBuffer b; b=conversion(a,basea,n,baseb,pre); System.out.println(b+"\t"); } catch(IllegalArgumentException e) { System.out.println("Wrong bases"); } } }

DIGIT STRING
PROGRAM:importjava.util.Scanner; publicclassdigstr { static String conv(int d[],intn,char a[]) { inti; String ret=newString(); for(i=0;i<n;i++) { ret=ret+a[d[i]]; } return ret; } publicstaticvoid main(String args[]) { Scanner sc=newScanner(System.in); intn,base,i; System.out.println("Enter base"); base=sc.nextInt(); System.out.println("Enter no of digits"); n=sc.nextInt(); int d[]=newint[n]; char a[]=newchar[base]; System.out.println("Enter chars"); for(i=0;i<base;i++) { a[i]=sc.next().charAt(0); } System.out.println("Enter digits"); for(i=0;i<n;i++) { d[i]=sc.nextInt(); } try { for(i=0;i<n;i++) if(d[i]>=base) thrownewIllegalArgumentException(); String v=conv(d,n,a); System.out.println("String is "+v); } catch(Exception e) { System.out.println("Digit more than base"); } } }

Anda mungkin juga menyukai