Question

In: Computer Science

JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface,...

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

}

}

Solutions

Expert Solution

Short Summary:

  • Implemented the Demo class as per the requirement
  • Attached source code and output

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!******************


Related Solutions

JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
Given the following UML class diagram, implement the class as described by the model. Use your...
Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method. +---------------------------------------------------+ | Polygon | +---------------------------------------------------+ | - side_count : int = 3 | | - side_length : double = 1.0 | +---------------------------------------------------+ | + Polygon() | | + Polygon(side_count : int) | | + Polygon(side_count : int, side_length : double) | | + getSides() : int | | + setSides(side_count : int) | |...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
Following is hw14. Save the demo filename as lastname_hw14.java and the class name as lastname_people.java. Following...
Following is hw14. Save the demo filename as lastname_hw14.java and the class name as lastname_people.java. Following are three arrays each containing 50 values. Store all three arrays in the main method. Write an object to transfer the three arrays. int []age[] = {70, 68, 52, 69, 40, 59, 61, 34, 45, 50, 43, 22, 35, 50, 67, 33, 36, 22, 63, 65, 56, 31, 55, 28, 30, 24, 55, 35, 39, 59, 68, 50, 33, 45, 26, 54, 44, 56,...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
c++ The purpose of this project is to test your ability to use files, class design,...
c++ The purpose of this project is to test your ability to use files, class design, operator overloading, and Strings or strings effectively in program design Create a program which will read a phrase from the user and create a framed version of it for printing. For example, the phrase "hello world"would result in: ********* * hello * * world * ********* Whereas the phrase "the sky is falling"might be: *********** * the * * sky * * is *...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT