Restrictions: Unless directed otherwise, you cannot use any Java class libraries
ID: 3742516 • Letter: R
Question
Restrictions: Unless directed otherwise, you cannot use any Java class libraries in this assignment. In particular any methods from the Arrays class. If needed cannot use the ArrayList class nor can you use you may create and use one or more instances of an array and access the length instance variable for each array you may use the Java Random class Of course you may use the System.print, println, and printf methods. - - In this project you will be doing the following Develop a java interface named Bag that can store a certain number of whole numbers in it. Abag in java is a kind of collection that does not do much more than contain its items. It does not order the items in any particular order and nor does it prevent duplicates Provide the following methods in the interface. getCurrentsize( that returns a count of numbers in the bag isEmpty)-that checks if the bag is empty, returns true when empty add (int num) - adds the number num to the bag * remove (int num) removes the first occurrence of the number num from the bag emoveremoves a randomly selected entry from the bag clear) removes all the numbers from the bag getFrequencyof (int num) -returns a count the number of times the number num exists in the bag *contains (int num) - Tests whether the bag contains the number num. Returns true when the pum is contained in the bag *tostring( )- returns a String of the contents of the bag equals (object o)returns a true if the parameter o exactly matches the contents of the bag i.e. same numbers in the same order) Design a java class called Scores that implements the Bag interface and provides implementation for all the methods inherited from the Baginterface. Do the following in the Scores class:Explanation / Answer
//Bag.java
/**
* @author
*
* Interface to store whole number
*/
public interface Bag {
public int getCurrentSize();
public boolean isEmpty();
public void add(int num);
public void remove(int num);
public void remove();
public void clear();
public int getFrequencyOf(int num);
public boolean contains(int num);
public String toString();
public boolean equals(Object o);
}
//Scores.java
import java.util.Random;
/**
* @author
*
* Implement Bag interface
*
*/
public class Scores implements Bag {
private int[] list;
private int count;
public Scores() {
super();
list = new int[50];
this.count = 0;
}
public Scores(int count) {
super();
list = new int[count];
this.count = 0;
}
@Override
public int getCurrentSize() {
// TODO Auto-generated method stub
return this.count;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return (count == 0);
}
@Override
public void add(int num) {
// TODO Auto-generated method stub
if(this.count < list.length) {
list[count++] = num;
} else {
int [] temp = new int[2*list.length];
for(int i=0;i<list.length;i++) {
temp[i] = list[i];
}
list = temp;
temp = null;
list[count++] = num;
}
}
@Override
public void remove(int num) {
// TODO Auto-generated method stub
if(contains(num)) {
for(int i=0;i<list.length;i++) {
if(list[i] == num) {
for(int j=i+1;j<=count;j++) {
list[j-1] = list[j];
}
break;
}
}
}
}
@Override
public void remove() {
// TODO Auto-generated method stub
if(!isEmpty()) {
Random r = new Random();
int num = r.nextInt(this.count);
for(int i=num+1;i<=count;i++) {
list[i-1] = list[i];
}
}
}
@Override
public void clear() {
// TODO Auto-generated method stub
count=0;
}
@Override
public int getFrequencyOf(int num) {
// TODO Auto-generated method stub
int frequency=0;
for(int i=0;i<list.length;i++) {
if(list[i] == num)
frequency++;
}
return frequency;
}
@Override
public boolean contains(int num) {
// TODO Auto-generated method stub
for(int i=0;i<list.length;i++) {
if(list[i] == num)
return true;
}
return false;
}
@Override
public String toString() {
String str="";
for(int i=0;i<list.length;i++) {
str += list[i]+" ";
}
return str;
}
public int get(int i) throws ArrayIndexOutOfBoundsException{
if(i >=0 && i <= this.count)
return list[i];
throw new ArrayIndexOutOfBoundsException();
}
}
//Client.java
import java.util.Random;
/**
* @author
*
*
*/
public class Client {
public static void main(String[] args) {
Scores s = new Scores(100);
for (int i = 0; i < 100; i++) {
Random r = new Random();
int num = r.nextInt(100 - (-100)) - 100;
s.add(num);
}
System.out.println(s.toString());
s.add(86);
System.out.println("Current size: " + s.getCurrentSize());
s.remove();
int element = s.get(75);
System.out.println("Element at 75th index: " + element);
System.out.println("Frequency of " + element + " is : " + s.getFrequencyOf(element));
System.out.println("Frequency of 86 is : " + s.getFrequencyOf(86));
if (s.contains(86)) {
System.out.println("86 present in list");
} else {
System.out.println("86 not present in list");
}
}
}
//output:
-81 91 -32 -88 -88 80 73 36 -47 -78 70 33 51 -5 49 -71 -37 2 -33 26 27 -48 42 32 -49 -95 -69 47 25 -31 -1 66 27 12 53 -24 -83 1 -69 55 28 -46 95 -9 -90 -22 -29 63 51 -24 -31 82 37 -97 -94 -57 34 30 16 -8 -16 76 79 24 -18 91 25 -57 29 57 48 -100 26 59 84 87 7 3 -93 25 89 20 -75 -38 36 19 -95 2 86 -17 11 46 87 -69 -33 -10 70 -49 -42 -46
Current size: 101
Element at 75th index: 7
Frequency of 7 is : 1
Frequency of 86 is : 2
86 present in list
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.