java

26
INFORMATICS PRACTICES PRACTICAL SUBMITTED BY : C SAI SATHVICK ROLL NO : 46

Upload: saisathvick

Post on 22-Nov-2014

187 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Java

INFORMATICS

PRACTICES

PRACTICAL

SUBMITTED BY :

C SAI SATHVICK

ROLL NO :

46

Page 2: Java

JAVA PROGRAMMING

Q1.Design a java program to print the table of the entered number?

Ans.

Preview-

Source Code-

Code for Table

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int x= Integer.parseInt(AT.getText());

int y,z=0;

for(y=1;y<=10;++y) {

Page 3: Java

z=y*x;

RT.append(z+"\n"); } }

Code for Clear private void clearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: AT.setText(" "); RT.setText(" "); }

Q2.Design a java program to print the Fibonacci series of the entered number?

Ans.

Preview-

Source Code-

Code for Fibonacci

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int s1=0,s2=1,sum,n;

n=Integer.parseInt(AT.getText());

Page 4: Java

if(n==0)

RT.append(0+"\n");

else

if(n==1)

RT.append(0+"\n"+1+"\n");

else {

RT.append(0+"\n"+1+"\n");

RT.append("");

for(int i=2; i<=n; i++) {

sum=s1+s2;

RT.append(sum+"\n");

RT.append(" ");

s1=s2;

s2=sum; } } }

Code for Clear

private void clearActionPerformed (java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

AT.setText("");

RT.setText("");

}

Page 5: Java

Q3.Design a java program to display the scored goal details and match result?

Ans.

Preview-

Source Code-

Code for Scored By A

private void GOAActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int ga= Integer.parseInt(tAGoals.getText());

ga=ga+1;

tAGoals.setText(""+ga); }

Code for Scored By B private void GOBActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:int gb= Integer.parseInt(tBGoals.getText()); gb=gb+1; tBGoals.setText(""+gb); }

Page 6: Java

Code for Declare Match

private void DCActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int ga=Integer.parseInt(tAGoals.getText());

int gb=Integer.parseInt(tBGoals.getText());

String res=(ga>gb?"Team A wins":(ga<gb)?"Team B wins":"Draw");

result.setText(res); }

Code for Clear

private void CLEARActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

result.setText(" ");

tBGoals.setText("0");

tAGoals.setText("0"); }

Q4. Design a java program to add the Digits in input area?

Ans.

Preview-

Page 7: Java

Code of main method-

int addDigits(int n){int s=0;

int dig;

while(n>0){

dig=n%10;

s=s+dig;

n=n/10;

}return s;

}

Code for Compt……….. Button-

int num=Integer.parseInt(input.getText());

int sum=addDigits(num);

out.setText("Sum of its digits is "+sum);

Page 8: Java

Q5. Design a java program to change background colour of different input controls ?

Ans.

Preview-

Code for the jList (Event –ListSelection-valueChanged)-

private void ColValueChanged(javax.swing.event.ListSelectionEvent evt) {

// TODO add your handling code here:

int i;

Page 9: Java

Color x=Color.WHITE;

i=Col.getSelectedIndex();

switch(i){

case 0: x=Color.RED;

break;

case 1: x=Color.BLUE;

break;

case 2: x=Color.GREEN;

break;

case 3: x=Color.MAGENTA;

break;

case 4: x=Color.CYAN;

break;

case 5: x=Color.YELLOW;

break;

case 6: x=Color.GRAY;

break;

}

if(LBL1.isSelected())

Lb.setBackground(x);

else

Lb.setBackground(Color.WHITE);

if(LBL2.isSelected())

Btn.setBackground(x);

else

Page 10: Java

Btn.setBackground(Color.WHITE);

if(LBL3.isSelected())

TF.setBackground(x);

else

TF.setBackground(Color.WHITE);

Q6. Design a java program to select the character from the list as given in textfield?

Ans.

Preview-

Code of Select in List Button-

private void oKActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

List.setSelectedValue(ENTER.getText(), true);

Page 11: Java

Q7.Design a java program to print the numbers in between the given input?

Ans.

Code of Count Button-

int a=Integer.parseInt(IN1.getText());

int b=Integer.parseInt(IN2.getText());

if(a>b){

for(;b<=a;b++)

{OUT.append(b+" ");}

Page 12: Java

}

else{

for(;a<=b;a++)

{ OUT.append(a+" ");}

}

Q8. Design a java program to check a string is palindrome or not?

Ans.

Code of Perform Palindrome Test Button-

String str=In.getText();

showPalindrome(str);

Main method Code-

public void showPalindrome(String s){

Page 13: Java

StringBuffer out=new StringBuffer(s);

if(isPalindrome(s))

s=s+": is a palindrome!";

else if(isPalindrome2(s))

s=s+ ": is a palindrome if you ignore case";

else

s=s+": is not a palinidrome! ";

Out.setText(s);

}

public boolean isPalindrome(String s)

{StringBuffer reversed=(new StringBuffer(s)).reverse();

return s.equalsIgnoreCase(reversed.toString());

}

public boolean isPalindrome2(String s)

{StringBuffer reversed=(new StringBuffer(s)).reverse();

return s.equalsIgnoreCase(reversed.toString());

}

Page 14: Java

Q9. Design a java program to reverse a string?

Ans.

Code of Reverse Button-

String a=IN.getText();

String b="";

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

b=b+a.charAt(i);

}OUT.setText(b);

Page 15: Java

Q10. Design a java program to find the occurrence of a character?

Ans.

Code of Count Button-

String a=In1.getText();

char b=In2.getText().charAt(0);

int c=0;

for(int d=0;d<a.length();d++){

if(a.charAt(d)==b)

c++;

Page 16: Java

} Out.setText (""+c);

Q11. Design a java program to find the position of a vowel in a string?

Ans.

Code of OK Button-

// TODO add your handling code here:

String a=IN.getText();

for(int d=0;d<=a.length();d++){

char c=a.charAt(d);

switch(c){

case 'a':

Page 17: Java

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

d=d+1;

OUT.append(" "+d+"\n");

break;

default: } }

Q12. Design a java program to display a menu of Ice-cream parlour ?

Ans.

Page 18: Java

Code of Calculate Button-

if(St.isSelected()==true)

{ STP.setText("35");}

else

{STP.setText("0");

STQ.setText("0");}

if(Ch.isSelected()==true)

{CHP.setText("50");}

else

{CHP.setText("0");

CHQ.setText("0");}

if(Va.isSelected()==true)

{VAP.setText("30");}

else

{VAP.setText("0");

Page 19: Java

VAQ.setText("0");}

int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;

a1=Integer.parseInt(STP.getText());

a2=Integer.parseInt(CHP.getText());

a3=Integer.parseInt(VAP.getText());

a4=Integer.parseInt(STQ.getText());

a5=Integer.parseInt(CHQ.getText());

a6=Integer.parseInt(VAQ.getText());

a7=a1*a4;

a8=a2*a5;

a9=a3*a6;

STT.setText(""+a7);

CHT.setText(""+a8);

VAT.setText(""+a9);

a10=a7+a8+a9;

OUT.setText(a10+"");

Code of Clear Button-

St.setSelected(false);

Ch.setSelected(false);

Va.setSelected(false);

STT.setText("");

VAT.setText("");

CHT.setText("");

OUT.setText("");

STQ.setText("");

Page 20: Java

VAQ.setText("");

CHQ.setText("");

VAP.setText("");

STP.setText("");

CHP.setText("");

Code of Exit Button-

System.exit (0);

Q13. Design a class program to display a detail of a book ?

Ans.

Class Method-

public class book {

String name,author;

int edition;

float price;

public book(){

this.name="";

this.author="";

edition=2000;

price=100.0f;

Page 21: Java

}

public book(String a,String b,int c,float d){

this.name=a;

this.author=b;

edition=c;

price=d;

}

void display(){

System.out.println("Name of the book is "+name);

System.out.println("Author of book is "+author);

System.out.println("Edition:-"+edition);

System.out.println("Price:-"+price);

}

Main Method-

book B1=new book ("The Canterville Ghost”, “Oscar Wilde",2012,55);

B1.display ();

Q14. Design program to display a student record table ?

Ans.

Page 22: Java

Class Method-

void process(){

int a=0;

String name="",hname="";

double perc=0.0,hperc=0.0;

do{name=JOptionPane.showInputDialog("Enter Student's name:");

perc=Double.parseDouble(JOptionPane.showInputDialog("Enter Percentage Marks:"));

resTa.append(name+"\t"+perc+"\n");

if(hperc<perc){

hperc=perc;

hname=name;

}

a=JOptionPane.showConfirmDialog(null,"More Student info??");

}

while(a==JOptionPane.YES_OPTION);

if(a==JOptionPane.NO_OPTION){

Page 23: Java

JOptionPane.showMessageDialog(null,"Highest scorer is "+hname+" with "+hperc+"marks. ");

}

}

Main Method-

JOPdialogs j1=new JOPdialogs();

j1.setVisible(true);

j1.process();

j1.setVisible(false);

Q15. Design a class program to print detail of a student?

Ans.

Class Method-

public class Student{

int rollNumber = 0;

String name = "nil";

public Student(int rno,String sname){

rollNumber=rno;

Page 24: Java

name=sname;

}

void display(){

System.out.println("RollNumber ="+rollNumber);

System.out.println("Name ="+name);

}

Main Method-

StudentB1=new Student(23,"GOVIND");

B1.display();

}

}