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

Java Pratical

Practical Program for bca 2nd sem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Pratical

Practical Program for bca 2nd sem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Program 4. Write a program to create a user defined exception say Pay Out of Bounds. import java.util; class OutOfBoundException extends Exception { OutOfBoundException(String msg) { System.out. printin("Out of Bounds Exception:"+msg); y class userdefinedex {public static void main(String args[]) throws OutOfBoundException {System.out.printin("enter age of the person"); ‘Scanner sc = new Scanner(System.in); int age = se.nextint(); iffage<18 || age>100) { throw new OutOfBoundException("Person is not eligible for voting"); } else {System.out.printin("Person is eligible for voting"); i Program 5. Write a java program to add two integers and two float numbers. When no arguments are supplied, give a default value to calculate the sum. Use function overloading. class sum fint a,b; sum() } void add() {int o=a+b; ‘System.out.printin("Sum is "+0); } void add(int a,int b) {int o=a+b; ‘System.out.printin('sum is "+c); } void add(double a, double b) {double c=a+b; ‘System.out.printin("sum is "+c); yt class calculate {public static void main(String argsf]) {sum S1 = new sum(); S1.add\); S1.add(20,40); S1.add(6.3,7.8); tt Program 6: Write a program to perform mathematical operations. Create a class called AddSub with methods to add and subtract. Create another class called MulDiv that Extends from AddSub class to use the member data of the super class. Mull should Have methods to multiply and divide A main function should access the methods and Perform the mathematical operations. Class addsub {Int a,b; Addsub() {A=50; b=10; } Void addi) {Int c=a+! ‘System.out.printin(“addition of "+a* “and” +b+ “is” +c); } Void sub() {Int c=a-b; System.out.printin(“subtract of” +a+ “and” +b+ “is” +c); t Class muldiv extends addsub {Void mutti() {Int c=a*b; ‘System.out.printin(“multiplication of “ +a* “and” +b+ “is” +c); } System.out.printin(“division of”+a+"and “+b+"is"+c); » ct thetic {Public static void main(String args) {Muldivmd = new mutdiv(; ‘System.out.printin(*ARITHMETIC OPERATIONS"); md.ada; md.sub 0; md.mutti md.divds » Program 7. Write a program with class variable that is available for all instances of a class. Use static variable declaration. Observe the changes that occur in the object’s member variable values. Class student {Static String college=” bcu College”; Int ro String name; ‘Student(int mo,String name) { This.mo=rno; This.name=name; } Void display() {System.out.printin{mno+” “+name+”“+college); y Public class StaticDemo {Public static void main(String args{]) {System.out.printin(“objects sharing the static varible-College name\n" Student s1= new student(111,"bcu”); Student s2= new student(112,"college”}; S1.display(); S2.display(); System.out.printin("\nStatoc value changed by one of the object \n”); S1.college=” bcu College (Autonomous); S1.display(); S2.display(); w Program 8. Write a java program to create a student class with following attributes: : Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three marks must be calculated only when the student passes in all three subjects. The pass mark for each subject is 50. If a candidate fails in any one of the subjects his total mark must be declared as zero. Using this condition write a constructor for this class. Write separate functions for accepting and displaying student details. In the main method create an array of three student objects and Enroltment, display the details. Import java.util; Class student {Scanner sc= new Scanner(System.in); String rolino; String name; Int m1,m2,m3,total; Student() {Readdata(); } Public void readdata() {System.out.printin(“enter student details”); ‘System.out.printin(“enter rolinumber”) Rollno=se.next(); System.out.printin(“enter Name”); ‘System.out.printin(“enter marks in 3 subjects”); M1=se.nextint(); M2=se.nextint(}; M3=sc.nextint(); It(m1>=50 && m2>= 50 && m3>= 50) Totat=mi+m2+m3; Else Total=0; d Public void display() {System.out.printin(rollno+"\te"snames"Wt\e"stotal); » Public class Studentinto {Public static void main(String args) { ‘Student s[]= new student{3}; For(intin0; +) {Slilenew student() d ‘System.out-printin("t Student Details\n” ‘System.out.printin("Roll NO \tNamel\t Total”); Forint 0:13:64) {Stil=50 && m2>= 50 && m3>= 50) Total=m1+m2+m3; Else Total=0; Public void display() {System.out.printin(rolino+ "Wt" +name+"Wt"+total); » Public class Studentinfo {Public static void main(String args{]) { ‘Student s{}= new student{3}; For{int ie0;ic3;i++) {SliJenew student(); d ‘System.out.printin(*\t Student Det An") ‘System.out.printin(*Roll NO \tNamett Total”); Forfint ie0;i<3;i++) {Slil.display(); » Program 10. Define a class called fi year with above attributes and define a suitable constructor. Also write a method called best Student () which process a first-year object and return the student with the highest total mark. In the main method define a first-year object and find the best student of this class Import java.util; Class Firstyear {String classname; String Classteacher; Int studentcount; Int marks{]=new int{50 String stdnames[]=new String[50]; Scanner sc = new Scanner(System. i Public Firstyear() {Getdata(); } Public void getdata() {System.out.printin(“enter class name”); Classname = sc.nextLine(); ‘System.out.printin(“enter the class teacher name”); Classname = sc.nextLine(); Fortint jiestudentcount;ie+) {Marks{ijesc.nextint); » Public void bestStudent() {Int best=0, For(inti=0;ibest) {Bost =marks{i; Ket; » ‘System.out.printin(*the best count is.“+stdnames{k}}; » Public class Student {Public static void main(String args{)) (Firstyearfy= new Firstyear(); Fybeststudent); » ‘System.out. printin(“Please enter the total number of students in the class"); Studentcount = Integer.parselnt(sc.nextLine()); ‘System.out.printin(*Please enter the names of all students of the class: For(int i=0;i

You might also like