Question

Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal...

Please use Java only.

  1. Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. -- Not needed
  2. Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. --needed

ZeroException Task: -- Not needed

Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise. The advantage of doing so is that when exceptions are thrown, we can use the type of the exception we've created to identify the problem from among a set of other possible problems.

Here we have invented the notion of a "zero exception," which we would use to indicate that something has a zero value when it shouldn't. So in our main task below, we have a class which can return values but is not allowed to return a zero (for whatever arbitrary reason). Thus, whenever it is supposed to return a value, and the value happens to be zero, it will throw a ZeroExceptioninstead. Do not confuse this with a divide by zero error. This class will be fairly simple: the only constructor we will expect to be able to call is a default constructor (truly, we hardly need to put anything in the class for it to work - an exception's job is only to report that an error happened, so it really only needs to exist to do its work).

ArrayManipulator Task - I only need this one:

Now we write a simple class which stores an array and allows us to set or get elements of the array. The getters include methods which manipulate values before returning them (i.e. multiply the number by a constant or divide the number by a constant). We will add one more constraint: the getters must never return a zero value. Instead, whenever we have a zero value, we would throw a zero exception instead.

The class should implement the following public methods:

  • public ArrayManipulator(int size) Initialize an internal array of integers using the given size.
  • public void set(int i, int v) A setter which sets the value at index i of the array to v.
  • public int get(int i) A getter which gets the value at index i of the array. However, if the value would be zero, it should throw a ZeroException instead.
  • public int getMult(int i, int m) A getter which gets the value at index i of the array and returns the product of that value and m. It should not change the value in the array, just return the result. However, if the result would be zero, it should throw a ZeroException instead.
  • public int getDiv(int i, int d) A getter which gets the value at index i of the array and returns that value divided by d. It should not change the value in the array, just return the result. However, if the result would be zero, it should throw a ZeroException instead.
  • public void print(int mode, int i, int v) This method will call either get(), getMult(), or getDiv(), depending on the mode parameter which is passed in, and print the result. If mode==0 then it calls get(), if mode==1 then it calls getMult(), and if mode==2 then it calls getDiv(). For any other value of mode, it will do nothing. It will make the call using i as the index and (if necessary) v as the value. If the result of the call is a division by zero, then print "divide by zero" instead. If the result is an out-of-bounds array access, then "out of bounds" should be printed. Finally, if the result of the operation is an illegal zero, then "zero results" should be printed.

Please make sure that it passes the following: https://repl.it/@micky123/z


.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

He in Co.cliwen, e0 heh way aroo. Sopes C cablic Aay Maulpevaloy Cint.the aytp o) 了 .3 ak Care je terv Calcb CAwat-le.dex outor eocudExcepten ex)

Add a comment
Know the answer?
Add Answer to:
Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that somethi...

    IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. ZeroException Task: Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise. The advantage of doing so is that when exceptions...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the...

    Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...

  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT