Java programming
The purpose of this problem is to practice using a generic Urn class.
NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should be contents (as in the contents of the Urn), etc..
Create a generic class, called Urn, with a type parameter that simulates drawing an item at random out of a Urn. For example the Urn might contain Strings representing names written on a slip of paper, or the Urn might contain integers representing a random drawing for a lottery. Include the following methods in your generic class, along with any other methods you’d like:
Your Urn class with need an array of size 10 to hold the objects in the Urn, and a count variable to maintain a count of how many objects are actually in the Urn.
In the driver file that tests your class create 3 Urns, one to test the various methods in the Urn class, one with the names of 8 of your friends, the third with numbers between 1 and 5 inclusive representing the number of hours you will spend playing computer games tonight with 2 of your friends.
For the test Urn,
For the remaining 2 urns, use the add( ) method to populate the 2 Urns, and the drawItem( ) method for each Urn to determine i) which 2 friends you will invite to play computer games with and ii) how many hours of gaming you and your friends will do.
Note:
public int nextInt(int bound) in java.util.Random returns a random int value between 0 (included) and the specified value, bound, (excluded).
Program
import java.util.*;
//EmptyUrnException class
class EmptyUrnException extends Exception
{
//constructor
public EmptyUrnException()
{
super("Urn underflow");
}
}
//FullUrnException class
class FullUrnException extends Exception
{
//constructor
public FullUrnException()
{
super("Urn overflow");
}
}
//Urn class
class Urn<T>
{
//data members
private T array[];
private int count;
//constructor
public Urn()
{
array = (T[]) new Object[10];
count = 0;
}
//method adds an item
public void add(T item) throws FullUrnException
{
if(count==10)
throw new
FullUrnException();
array[count] = item;
count++;
}
//method returns true if the Urn is empty, otherwise
returns false
public boolean isEmpty()
{
if(count==0)
return
true;
return false;
}
//method selects an object randomly from the Urn and
returns it
public T drawItem() throws EmptyUrnException
{
if(isEmpty())
throw new
EmptyUrnException();
//create an instance of Random
class
Random rand = new Random();
//generate a random index
int j = rand.nextInt(count);
//get the item
T item = array[j];
//delete the item
for(int i=j; i<count-1;
i++)
{
array[i] =
array[i+1];
}
count--;
//return the item
return item;
}
//returns a String containing the Urn’s contents
public String toString()
{
String s = "[";
for(int i=0; i<count-1;
i++)
{
s = s + array[i]
+ ", ";
}
if(!isEmpty())
s = s +
array[count-1];
s = s + "]";
return s;
}
}
//Driver class
class Driver
{
//main method
public static void main (String[] args)
{
//create an instance of Random
class
Random rand = new Random();
/******* 1st Urn object
**********/
//create an Urn object
Urn<Integer> urn = new
Urn<Integer>();
try{
//create a for
loop to attempt to fill 11 items
for(int i=0;
i<11; i++)
{
urn.add(rand.nextInt(20));
}
}catch(Exception e)
{
System.out.println (e.getMessage());
}
try{
//create a for
loop to attempt to draw 11 items
for(int i=0;
i<11; i++)
{
int m = urn.drawItem();
System.out.println ("Drawn item is " + m);
}
}catch(Exception e)
{
System.out.println (e.getMessage());
}
//display the contents of the empty
urn
System.out.println (urn);
/******* 2nd Urn object
**********/
//create an Urn object
Urn<String> urn2 = new
Urn<String>();
try{
/* add 8 friend
names */
urn2.add("friendname1");
urn2.add("friendname2");
urn2.add("friendname3");
urn2.add("friendname4");
urn2.add("friendname5");
urn2.add("friendname6");
urn2.add("friendname7");
urn2.add("friendname8");
}catch(Exception e)
{
System.out.println (e.getMessage());
}
try{
//draw 2 friends
name
String fname1 =
urn2.drawItem();
String fname2 =
urn2.drawItem();
System.out.println ("Two friends are " + fname1 + " and " +
fname2);
}catch(Exception e)
{
System.out.println (e.getMessage());
}
/******* 3rd Urn object
**********/
//create an Urn object
Urn<Integer> urn3 = new
Urn<Integer>();
try{
//add numbers
between 1 and 5 inclusive
for(int i=1;
i<=5; i++)
{
urn3.add(i);
}
}catch(Exception e)
{
System.out.println (e.getMessage());
}
try{
//draw 1
item
int m =
urn3.drawItem();
System.out.println (m + " hours you will spend playing computer
games tonight with 2 of your friends.");
}catch(Exception e)
{
System.out.println (e.getMessage());
}
}
}
Output:
Urn overflow
Drawn item is 10
Drawn item is 2
Drawn item is 1
Drawn item is 6
Drawn item is 2
Drawn item is 3
Drawn item is 17
Drawn item is 14
Drawn item is 8
Drawn item is 9
Urn underflow
[]
Two friends are friendname8 and friendname5
3 hours you will spend playing computer games tonight with 2 of
your friends.
Java programming The purpose of this problem is to practice using a generic Urn class. NOTE:...
The purpose of this problem is to practice using a generic Jar class. write in drjava Create a generic class, called Jar, with a type parameter that simulates drawing an item at random out of a Jar. For example the Jar might contain Strings representing names written on a slip of paper, or the Jar might contain integers representing a random drawing for a lottery. Include the following methods in your generic class, along with any other methods you’d like:...
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...
In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element); /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...
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 programming: please help with the following assignment: Thank you. Create a class called GiftExchange that simulates drawing a gift at random out of a box. The class is a generic class with a parameter of type T that represents a gift and where T can be a type of any class. The class must include the following An ArrayList instance variable that holds all the gifts,The ArrayList is referred to as theBox. A default constructors that creates the box....
Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...
Write code in Java programming language. The ShoppingCart class will be composed with an array of Item objects. You do not need to implement the Item class for this question, only use it. Item has a getPrice() method that returns a float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart class should have: Constructors: A no-argument and a single argument that takes an array of Items. Fields: • Items: an array of Item objects...
Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction - This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...
java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...
Java Programming help!!! Anthony W. Smith, 2017 Purpose Purpose of this lab is for you to develop a program where many objects are created from a class. Primitives and objects are passed as parameters to class methods. Problem specification Your new wallet class implements a wallet that contains banknotes. A banknote is represented as an int for simplicity, 1 for a SI bill, 5 for a $5 bill, and so on. You are required to use a simple array ofint...