• Nenhum resultado encontrado

bcom3java

N/A
N/A
Protected

Academic year: 2021

Share "bcom3java"

Copied!
18
0
0

Texto

(1)

1. Arithmetic Operations

Problem: Write a program to find the Addition, subtraction, multiplication and division using arithmetic operators. Problem solution: Algorithm: 1. Begin 2. Input a, b 3. print ‘addition=’a+b 4. print ‘subtraction=’a-b 5. print ‘multiplication=’a*b 6. print ‘division=’a/b 7. End Program: import java.util.*; class arithmetic {

public static void main(String args[]) {

int a,b;

Scanner in=new Scanner(System.in); System.out.println(“Enter a,b values”); a=in.nextInt(); b=in.nextInt(); System.out.println(“Addition=”+(a+b)); System.out.println(“Subtaction=”+(a-b)); System.out.println(“Multiplication=”+(a*b)); System.out.println(“Division=”+(a/b)); System.out.println(“Modulo Division=”+(a%b)); } } Test data: Enter a,b values 7 5 Addition=12 Subtraction=2 Multiplication=35 Division=2 Modulo divison=2

(2)

2. Bitwise Operations

Problem: Write a program to find the Bitwise AND, OR and XOR operations using bitwise operators.

Problem solution Algorithm:

1. Begin 2. Input a, b

3. print ‘Bitwise AND=’a&b 4. print ‘Bitwise OR=’a|b 5. print ‘Bitwise XOR=’a^b 6. End

Program:

import java.util.*; class bitwise {

public static void main(String args[]) {

int a,b;

Scanner in=new Scanner(System.in); System.out.println(“Enter a,b values”); a=in.nextInt();

b=in.nextInt();

System.out.println(“Bitwise AND=”+(a&b)); System.out.println(“Bitwise OR =”+(a|b)); System.out.println(“Bitwise XOR =”+(a^b)); }

}

Test data Enter a,b values 13

25

Bitwise AND=9 Bitwise OR=29 Bitwise XOR=20

(3)

3. Traingular Stars Problem: Write a program to display stars in triangular form. Problem solution:

Algorithm: 1. Begin

2. print ‘enter how many rows’ 3. input r 4. for i=1 to r 5. print “ “ 6. for j=1 to i 7. print “*” 8. End for 9. End for 10. End Program: import java.util.*; class stars {

public static void main(String args[]) {

int r;

Scanner in=new Scanner(System.in); System.out.println(“Enter how may rows:”); r=in.nextInt();

for(int i=1; i<=r; i++) { System.out.println( ); for(int j=1;j<=i; j++) System.out.print(“*”); } } } Test data

Enter how many rows: 4 *

* * * * * * * * *

(4)

4. Display Color Names

Problem: Write a program to display color name on executing a specific color code. Problem solution

Algorithm: 1 Begin

2 Print “enter color code” 3 Input code 4 If code=’r’ then 5 Print ‘Red’ 6 If code=’b’ then 7 Print ‘Blue’ 8 If code=’g’ then 9 Print ‘Green’ 10 If code=’w’ then 11 Print ‘white’ 12 End if 13 End Program: import java.util.*; class colors {

public static void main(String args[]) {

int code;

Scanner in=new Scanner(System.in); System.out.println(“Enter color code:”); code=(char)System.in.read(); switch(code) { case ‘r’: System.out.println(“Red”); break; case ‘g’: System.out.println(“Green”); break; case ‘b’: System.out.println(“Blue”); break; case ‘w’: System.out.println(“White”); break;

default: System.out.println(“Invalid color”); }

} }

Test data

Enter color code: b Blue

(5)

5. Sorting

Problem: Write a program to sort array elements in ascending order. Problem solution Algorithm: 1. Begin 2. tot=0 3. for i=1 to 5 4. input a[i] 5. End for 6. for i=1 to 5 7. for j=i+1 to 5 8. if a[i]<a[j] then 9. temp=a[i] 10. a[i]=a[j] 11. a[j]=temp 12. end if 13. end for 14. end for

15.print “array elements” 16. for i=1 to 5 17. print a[i] 18. End for 19. End Program: import java.util.*; class sort {

public static void main(String args[]) {

int a[]=new int[5];

Scanner in=new Scanner(System.in);

System.out.println(“Enter Array elements:”); for(int i=0;i<5;i++) a[i]=in.nextInt(); for(int i=0;i<5;i++) { for(int j=i+1;j<5;j++) { if(a[i]<a[j]) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

System.out.println(“Array elements after sort:”); for(int i=0;i<5;i++)

System.out.println(a[i]); }

}

Test data

Enter array elements: 4

2 1 5 3

(6)

6. Net salary Calculation

Problem: Write a program to accept employee basic salary and find net salary. Problem solution:

Algorithm: 1. Begin

2. print “enter employee basic salary” 3. input sal 4. if sal>=7500 then 5. hra=sal*0.12 6. da=sal*0.15 7. ptax=sal*0.05 8. else 9. hra=sal*0.10 10. da=sal*0.12 11. ptax=sal*0.03 12. end if 13. gross=basic+hra+da 14.net=gross-ptax

15. print “gross salary=”gross 16. print “net salary=”net 17. end

Program:

import java.util.*; class netsal {

public static void main(String args[]) throws Exception {

float sal,hra,da,ptax,gs,net;

Scanner in=new Scanner(System.in); System.out.println(“Enter Basic salary:”); sal=in.nextFloat(); if (sal>=7500) { hra=sal*0.12f; da=sal*0.15f; ptax=sal*0.05f; } else { hra=sal*0.10f; da=sal*0.12f; ptax=sal*0.03f; } gs=sal+hra+da; net=gs-ptax; System.out.println(“Gross salary=”+gs); System.out.println(“Net salary=”+net); } } Test data

Enter Basic salary: 6500 Gross salary:

(7)

7. Matrix Addition

Problem: Write a program to find the two-dimensional matrix addition. Problem solution

Algorithm: 1. Begin

2. print “enter first matrix elements” 3. for i=1 to 3

4. for j=1 to 3 5. input a[i][j]

6. print “enter second matrix elements” 7. for i=1 to 3 8. for j=1 to 3 9. input b[i][j] 10. for i=1 to 3 11. for j=1 to 3 12. c[i][j]=a[i][j]+b[i][j]

13. print “result matrix elements 14. for i=1 to 3 15. for j=1 to 3 16. print c[i][j] 17. End Program: import java.util.*; class matadd {

public static void main(String args[]) {

int i,j;

int a[][]=new int[3][3]; int b[][]=new int[3][3]; int c[][]=new int[3][3];

Scanner in=new Scanner(System.in);

System.out.println(“Enter first matrix elements:”); for(i=0;i<3;i++)

{

for(j=0;j<3;j++) a[i][j]=in.nextInt(); }

System.out.println(“Enter second matrix elements:”); for(i=0;i<3;i++) { for(j=0;j<3;j++) b[i][j]=in.nextInt(); } for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; } System.out.println(“result matrix”); for(i=0;i<3;i++) { for(j=0;j<3;j++) System.out.print(“ “+c[i][j]); System.out.println(); } } Test data

(8)

8. Matrix Multiplication

Problem: Write a program to find the two-dimensional matrix addition. Problem solution

Algorithm: 1. Begin

2. print “enter first matrix elements” 3. for i=1 to 3

4. for j=1 to 3 5. input a[i][j]

6. print “enter second matrix elements” 7. for i=1 to 3 8. for j=1 to 3 9. input b[i][j] 10. for i=1 to 3 11. for j=1 to 3 12. c[i][j]=c[i][j]+a[i][k]*b[k][j] 13. print “result matrix elements 14. for i=1 to 3 15. for j=1 to 3 16. print c[i][j] 17. End Program: import java.util.*; class matmul {

public static void main(String args[]) { int i,j;

int a[][]=new int[3][3]; int b[][]=new int[3][3]; int c[][]=new int[3][3];

Scanner in=new Scanner(System.in);

System.out.println(“Enter first matrix elements:”); for(i=0;i<3;i++)

{

for(j=0;j<3;j++) a[i][j]=in.nextInt(); }

System.out.println(“Enter second matrix elements:”); for(i=0;i<3;i++) { for(j=0;j<3;j++) b[i][j]=in.nextInt(); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } System.out.println(“result matrix”); for(i=0;i<3;i++) { for(j=0;j<3;j++) System.out.print(“ “+c[i][j]); System.out.println(); } } Test data

(9)

9: Matrix Transpose Problem: Write a program to transpose a two-dimensional matrix. Problem solution:

Algorithm: 1. Begin

2. print “enter matrix elements” 3. for i=1 to 3 4. for j=1 to 3 5. input a[i][j] 6. for i=1 to 3 7. for j=1 to 3 8. b[i][j]=a[j][i]

9. print “Given matrix elements 10. for i=1 to 3

11. for j=1 to 3 12. print a[i][j]

13. print “Tranpose matrix elements 14. for i=1 to 3 15. for j=1 to 3 16. print b[i][j] 17. End Program: import java.util.*; class transpose {

public static void main(String args[]) {

int i,j;

int a[][]=new int[3][3]; int b[][]=new int[3][3];

Scanner in=new Scanner(System.in);

System.out.println(“Enter matrix elements:”); for(i=0;i<3;i++) { for(j=0;j<3;j++) a[i][j]=in.nextInt(); } for(i=0;i<3;i++) { for(j=0;j<3;j++) b[i][j]=a[j][i]; }

System.out.println(“Given Matrix elements”); for(i=0;i<3;i++) { for(j=0;j<3;j++) System.out.print(“ “+a[i][j]); System.out.println(); }

System.out.println(“Transpose Matrix elements”); for(i=0;i<3;i++) { for(j=0;j<3;j++) System.out.print(“ “+b[i][j]); System.out.println(); } } } Test data

(10)

10. Strings sorting Problem: Write a program to sort a list of strings.

Problem solution: Algorithm:

1. Begin

2. print “how many names to sort” 3. input n 4. for i=1 to n 5. input name[i] 6. end for 7. for i=1 to n 8. for j=1 to n 9. if name[j]<name[i] then 10. temp=name[j]; 11. a[j]=a[i] 12. a[i]=a[j]; 13. end if

14. print “names after sort” 15. for i=1 to n 16. input name[i] 17. end for 18. end PROGRAM import java.util.*; class stringsort

{ public static void main(String args[]) {

String name[]=new String[10]; String temp;

int i,j,n;

Scanner in=new Scanner(System.in);

System.out.print("\n How Many names to sort:"); n=in.nextInt(); for(i=0;i<n;i++) { System.out.println("Enter a name:"); name[i]=in.next(); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(name[j].compareTo(name[i])<0) { temp=name[j]; name[j]=name[i]; name[i]=temp; } } }

System.out.println("\n sorted Names are \n"); for(i=0;i<n;i++)

System.out.println(name[i]); }

}

Test data

How many names to sort: 5 Chennai

Delhi Ahmadabad Culcutta Bombay

(11)

11: String Methods

Problem: Write a program to insert a substring between a string by using string methods. Problem solution:

Algorithm: 1. Begin

2. string s1=”jagans college” 3. m=s1.length() 4. for i=1 to m 5. print s1.CharAat(i) 6. end for 7. int pos=s1.indexof(“c”) 8. s1.insert(pos,”degree”) 9. print s1 10. s1.append(“makes professionsl”,s1) 11. print s1 12. end program: class strmethods {

public static void main(String args[])throws Exception {

StringBuffer S1=new StringBuffer("sv college"); int i;

System.out.println("the original string is:"+S1); int m=S1.length();

System.out.println("length="+m); for(i=0;i<=m;i++)

{

System.out.println("the character at index "+i+" is "+S1.charAt(i)); }

int pos=S1.indexOf("c"); S1.insert(pos,"Degree");

System.out.println("the new string is:"+S1); S1.append("makes proffessionals");

System.out.println("the modified string is"+S1); }

}

Test data

the original string is: sv college the character at index 0 is s the character at index 1 is v the character at index 2 is the character at index 3 is c the character at index 4 is o the character at index 5 is l the character at index 0 is l the character at index 7 is e the character at index 8 is g the character at index 9 is e

the new string is: sv degree college makes proffessional the modified string is: sv degree college makes proffessional

(12)

12. class and objects

Problem: Write a program to create a class student to accept and display student details. Problem solution:

Algorithm Class student

Data: sno,sname,age

Methods: getdata(), putdata() Procedure getdata() 1. begin 2. input sno 3. input sname 4. input age 5. end procedure putdata() 1. begin

2. print “student number”,sno 3. print “Name:”,sname 4. print “Age:”,age 5. end

procedure main() 1. Begin

2. Declare s as student type 3. call s.getdata() 4. call s.putdata() 5. end Program import java.util.*; class student {

Scanner in=new Scanner(System.in); int sno,age;

String sname,course; void getdata()

{

System.out.println("enter student no,name,age and course"); sno=in.nextInt(); sname=in.next(); age=in.nextInt(); course=in.next(); } void putdata() {

System.out.println("Student number : "+sno); System.out.println("Student name: "+sname); System.out.println("Student age: "+age); System.out.println("Course is: "+course); }

}

class studetails {

public static void main(String args[]) {

student s=new student(); s.getdata();

s.putdata(); }

(13)

13. Class Constructor Problem: write a program on implementing constructors. Problem solution: Algorithm class room data: length,breadth constructor: room(x,y) 1. length=x 2. breadth=y room(x) 1. length=breadth=x procedure main() 1. begin 2. declare r1 program import java.util.*; class Room { int l,b; Room(int x,int y) { l=x; b=y; } Room(int x) { l=b=x; } int area() { return(l*b); } }; class rectarea {

public static void main(String args[])throws Exception {

Room r1=new Room(4,6); // rectangle object Room r2=new Room(5); // square object

System.out.println(“rectangle room area=”+r1.area()); System.out.println(“square room area=”+r2.area()); }

}

(14)

14. Multiple inheritance Problem: write a program to implement multiple inheritance Problem solution

Algorithm: 1. CLASS student Data: int hno Methods: gethno( ) Puthno( ) getmarks( ) putmarks( ) 2. Interface sports

Final constant: sprotswt=5.5 Abstract method: putwt( )

3. Class Result extends student and implements sports Data: total Method: display( ) 1. total=p1+p2+sportswt 2. puthno( ) 3. putmarks( ) 4. putwt( ) 5. print total 4. procedure main( ) 1. Begin

2. Declare student1 as Result type 3. student1.gethno( ) 4. student1.getmarks( ) 5. student1.display( ) 6. end Program import java.util.*; class student { int hno; void gethno(int a) { hno=a; } void puthno() {

System.out.println("hall ticket no="+hno); } } interface sports { float sportswt=5.5f; void putsportswt(); }

(15)

class test extends student {

float p1,p2;

void getmarks(float x,float y) { p1=x; p2=y; } void putmarks() { System.out.println("part1 marks="+p1); System.out.println("part2 marks="+p2); } }

class results extends test implements sports {

float total;

public void putsportswt() { System.out.println("sportswt="+sportswt); } void display() { total=p1+p2+sportswt; puthno(); putmarks(); putsportswt(); System.out.println("total marks="+total); } } class multiple

{ public static void main(String args[])throws Exception { results student1=new results();

student1.gethno(12536); student1.getmarks(41.5f,27.75f); student1.display(); } } Test data:

Hall ticket no:12356 Part1 marks:41.5 Part2 marks:27.75 Sports wt=5.5 Total marks=

(16)

15: Exception Handling Problem: write a program to implement Exception handling Problem solution

Algorithm: 1. Begin

2. array a[]={2,4,6,8,10} 3. try until exception raise 4. for i=1 to 10

5. print a[i] 6. tot=tot+a[i] 7. end for

8. catch the Exception in e 9. print exception e 10. print tot 11. end program import java.util.*; class excep {

public static void main(String args[]) {

int tot=o;

int a[]={2,4,6,8,10}; try

{

System.out.println("Array elements are"); for(int i=0;i<10;i++) { System.out.println(a[i]); tot=tot+a[i]; } }catch(Exception e1)

{ System.out.println("the exception is:"+e1); } System.out.println("total="+tot); } } Test data Array elements: 2 4 6 8 10

The exception is:AraryIndexOutOfBoundsException total=30

(17)

16. Threads Problem: write a problem to implement threads Problem solution

1.class A extends Thread 1. for i=1 to 10

2. print “ from thread A”,i 3. Print “Exit from threadA” 2.class B extends Thread 1. for j=1 to 10

2. print “ from thread B”,j 3. Print “Exit from thread B” 3.class C extends Thread 1. for k=1 to 10

2. print “ from thread C”, 3. Print “Exit from threadC” 4.procedure main( ) 1. Begin 2. A ThreadA=new A(); 3. B ThreadB=new B(); 4. C ThreadC=new C(); 5. ThreadA.start(); 6. ThreadB.start(); 7. ThreadC.start(); 8. End Program import java.util.*; class A extends Thread

{ int i;

public void run() {

for(i=1;i<=10;i++)

System.out.println("from thread A:"+i); System.out.println("Exit from thread A"); }

}

class B extends Thread

{ int j;

public void run() {

for(j=1;j<=10;j++)

System.out.println("from thread B:"+j); System.out.println("Exit from thread B"); }

}

class C extends Thread

{ int k;

public void run() {

for(k=1;k<=10;k++)

System.out.println("from thread C:"+k); System.out.println("Exit from thread C"); }

}

class Threaddemo {

(18)

public static void main(String args[])throws Exception {

System.out.println("Start of main thread"); A ThreadA=new A(); B ThreadB=new B(); C ThreadC=new C(); ThreadA.start(); ThreadB.start(); ThreadC.start(); } } Test data D:\saif>java Threaddemo Start of main thread from thread A:1 from thread A:2 from thread A:3 from thread A:4 from thread B:1 from thread A:5 from thread C:1 from thread A:6 from thread B:2 from thread A:7 from thread C:2 from thread A:8 from thread B:3 from thread A:9 from thread A:10

from thread C:3 from thread C:4 from thread C:5 from thread C:6 from thread B:4 from thread C:7 Exit from thread A from thread C:8 from thread B:5 from thread C:9 from thread B:6 from thread C:10 from thread B:7 Exit from thread C from thread B:8 from thread B:9 from thread B:10 Exit from thread B

Referências

Documentos relacionados

Em outras palavras, os valores médios do IRA dos alunos bolsistas do PIBID são superiores aos valores médios dos não bolsistas, independentemente do conceito ENADE do curso

de tombamento (tinta, plaqueta ou código de barras) devem ser anotados na “ Planilha de Bens Sem Identificação ”, com todos os detalhes do bem (descrição completa,

Os vazios devem ser considerados os navios urbanos das cidades contemporâneas que, por encontrarem-se em terra, não devem existir por si só, fechados, se pondo

Porém, restava ainda por saber até que ponto o conjunto de restos humanos submetidos a análise eram cronologicamente homogéneos , isto é, se pertencentes a indivíduos

Falar de “símbolo”, neste caso na perspectiva da poesia Simbolista, e tentar relacioná- -lo com a História, significa desde logo equacionar uma poética que tem o ponto de partida

O tratamento da infertilidade nos últimos anos tem sido objecto de rápidos e notáveis progressos, decorrentes do melhor conhecimento dos mecanismos fisiológicos da reprodução, como

Escrita numa época difícil para o autor, em que este se debatia com a falta de aceitação da sua obra, Claraboia, foi, durante a vida de José Saramago, colocada

Ou seja, duas hipóteses são prováveis: a primeira que seria os professores não utilizarem diferentes instrumentos de avaliação, mas disseram usa-los por estarem