




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
java programming|learn always|project|university
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Declare a class called buffer having an int array of length n to store only positive no.s Define required methods and main to meet following : -> read location and value pairs until 0 0 given -> write into the array at location l value v if, -> Location l is valid ,else raise and handle an exception -> value v is positive ,else raise an handle an exception (different type) -> Location l is empty ,else raise and handle an exception (different type- user defined) import java.util.*; class myException extends Exception { String msg="default"; int code; myException() {
msg="default msg set in constructor"; code=0; } myException(String d_msg,int d_code) { msg=d_msg; code=d_code; } public String toString() { return msg; } } class buffer {
int data=d_data; try { if(index>=n || index<0) throw new ArrayIndexOutOfBoundsException(); try { if(data<0) throw new Exception(); if(a[index]==-1) a[index]=data; else throw new myException("Location is not empty ",index); } catch(myException me) { System.out.println(me); } catch(Exception e)
System.out.println(e+" Not a positive no"); } } catch(ArrayIndexOutOfBoundsException ibe) { System.out.println(ibe); } } void display() { for(int i=0;i<n;i++) { if(a[i]!=-1)
System.out.print("index: "); int index=sc.nextInt(); System.out.print("data: "); int data=sc.nextInt(); if(data==index && data==0) break; b.input(index,data); } catch(InputMismatchException ime1) { System.out.println(ime1); break; } }
b.display(); } }