java assignment | assignment | chutiya banavo, Assignments of Java Programming

java programming|learn always|project|university

Typology: Assignments

2019/2020

Uploaded on 10/24/2020

someonepatel
someonepatel 🇨🇦

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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()
{
pf3
pf4
pf5
pf8

Partial preview of the text

Download java assignment | assignment | chutiya banavo and more Assignments Java Programming in PDF only on Docsity!

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(); } }