In: Computer Science
JAVA:
You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface, and the OverflowException class should handle any errors that may come from the addNum method.
Demo.java:
public class Demo implements SampleInterface {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated constructor stub
}
@Override
public void addNum(int value) throws OverflowException {
// TODO Auto-generated method stub
}
@Override
public void removeNum(int value) {
// TODO Auto-generated method stub
}
@Override
public int sumOfNumbers() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public boolean isFull() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
}
SampleInterface.java :
public interface SampleInterface {
/**
* Method adds a value to the list if the list doesn't have the value already
* If your list is full then the
* is full this method throws a OverflowException.
*
* @throws OverflowException
*/
public void addNum(int value) throws OverflowException;
/**
* This method will remove a value in your list if it's present
* If the value does not exist in this list then the list should remain unchanged
*
*/
public void removeNum(int value);
/**
* Method should add all numbers currently stored in the list and return it
* @return the sum of all the numbers stored in this object.
*/
public int sumOfNumbers();
/**
* removes all numbers from this object.
*/
public void clear();
/**
* Checks if the current list is full or not.
* @return true if the list in this object is full. false otherwise.
*/
public boolean isFull();
/**
* Checks if the current list is empty or not.
* @return true if the list in this object is empty. false otherwise.
*/
public boolean isEmpty();
}
OverflowException.java :
public class OverflowException extends Exception {
/**
*
*/
public OverflowException() {
// TODO Auto-generated constructor stub
}
/**
* @param arg0
*/
public OverflowException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
*/
public OverflowException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
* @param arg1
*/
public OverflowException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
* @param arg1
* @param arg2
* @param arg3
*/
public OverflowException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
}
Short Summary:
Source Code:
Demo.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Demo implements SampleInterface {
static List<Integer> list = new
ArrayList<Integer> (Arrays.asList(new Integer[5]));
int index;
/**
* @param args
* @throws OverflowException
*/
public static void main(String[] args) throws OverflowException {
//Test the methods
Demo obj=new Demo();
System.out.println("******Test
addNum method******");
obj.addNum(5);
obj.addNum(6);
obj.addNum(7);
obj.addNum(8);
obj.addNum(9);
obj.addNum(6);
//Uncomment the below line to test
Overflow exception
//
obj.addNum(10);
System.out.println(list);
System.out.println("******Test
removeNum method - Remove 5 from list******");
obj.removeNum(5);
System.out.println(list);
System.out.println("******Test
isEmpty method******");
System.out.println(obj.isEmpty());
System.out.println(list);
System.out.println("******Test
isFull method******");
System.out.println(obj.isFull());
System.out.println("******Test
sumOfNumbers method******");
System.out.println(obj.sumOfNumbers());
System.out.println("******Test
clear method******");
obj.clear();
System.out.println(list);
}
/**
* Method adds a value to the list if the list doesn't have the value already
* If your list is full then the
* is full this method throws a OverflowException.
*
* @throws OverflowException
*/
@Override
public void addNum(int value) throws OverflowException {
try
{
if(!list.contains(value))
{
//list.add(value);
list.set(index, value);
index++;
}
}
catch(Exception ex)
{
throw new
OverflowException(ex.getLocalizedMessage());
}
}
/**
* This method will remove a value in your list if it's present
* If the value does not exist in this list then the list should remain unchanged
*
*/
@Override
public void removeNum(int value) {
for(int
i=0;i<list.size();i++)
{
if(list.get(i)==value)
{
list.remove(i);
index--;
}
}
}
/**
* Method should add all numbers currently stored in the list and return it
* @return the sum of all the numbers stored in this object.
*/
@Override
public int sumOfNumbers() {
int sum=0;
for(int
i=0;i<list.size();i++)
{
sum=sum+list.get(i);
}
return sum;
}
/**
* removes all numbers from this object.
*/
@Override
public void clear() {
list.clear();
}
/**
* Checks if the current list is full or not.
* @return true if the list in this object is full. false otherwise.
*/
@Override
public boolean isFull() {
try
{
list.set(index,1);
}
catch(Exception ex)
{
return
true;
}
return false;
}
/**
* Checks if the current list is empty or not.
* @return true if the list in this object is empty. false otherwise.
*/
@Override
public boolean isEmpty() {
if(list.isEmpty())
return true;
return false;
}
}
Code Screenshot:
Output: (Uncomment line no 32 to test Overflow
Exception)
**************Please do upvote to appreciate our time. Thank you!******************