Question

In: Computer Science

I need this in java on textpad. There are two files, both have instructions in them...

I need this in java on textpad. There are two files, both have instructions in them on what to add in the code. They are posted below.

public class MyArrayForDouble {
double[] nums;
int numElements;
public MyArrayForDouble() { // Constructor. automatically called when creating an instance
numElements = 0;
nums = new double[5];
}
public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance
numElements = 0;
nums = new double[capacity];
}
public MyArrayForDouble(double[] nums1) {
nums = new double[nums1.length];
for(int i=0;i<nums1.length;i++)
nums[i] = nums1[i];
numElements = nums1.length;
}
void printArray(){ // cost, times
System.out.printf("printArray(%d,%d): ",numElements,nums.length);
for(int i=0; i<numElements;i++)
System.out.print(nums[i]+" ");
System.out.println();
}
int linearSearch(double val) {
for(int i=0;i<numElements;i++)
if(nums[i] == val)
return i;
return -1;
}
int binarySearch(double val) {
int start = 0;
int end = nums.length - 1;
int mid;
while(start <= end) {
mid = (start + end)/2;
if(val == nums[mid])
return mid;
else if(val < nums[mid])
end = mid-1;
else // val > nums[mid]
start = mid + 1;
}
return -1;
}
private void enlarge() {
// double up the size of nums;
double[] new_nums = new double[nums.length*2];
for(int i=0;i<numElements;i++)
new_nums[i] = nums[i];
nums = new_nums;
}
void add(double val) {
if(isFull()) // if(numElements == nums.length)
enlarge();
nums[numElements] = val;
numElements++;
}
public void addOrder(int idx, double[] valArray) {
// add all elements of valArray from the specified position of nums.
// need to keep order
// eg) [10,20,30] --> addOrder(1,{1,2}) makes {10,1,2,20,30}
ensureCapacity(numElements + valArray.length);
// Here we can safely assume 'nums' has at least 'numElements + valArray.length' spaces
for(int i=numElements-1; i>=idx ; i--)
nums[i+valArray.length] = nums[i];
for(int i=0;i<valArray.length;i++)
nums[i+idx] = valArray[i];
numElements += valArray.length;
}
private void ensureCapacity(int count) {
if(count <= nums.length)
return;
// need more space
double[] new_nums = new double[count];
for(int i=0;i<numElements;i++)
new_nums[i] = nums[i];
nums = new_nums;
}
int remove(double val){
// search for an element that is equal to val. and remove it from 'nums'
int idx = linearSearch(val);
if(idx < 0)
return 0;
// fill the location with the last element
nums[idx] = nums[numElements-1];
numElements--;
return 1;
}
// void removeAll(int val) { // worst-case: latter half elements are equal to val
// // remove(10) in nums = {1, 2, 3, 2, 4, ..., 10, 10, 10, 10}
// // O(N) * N = O(N*N)
// // search for all the elements equal to val and remove them.
// while(remove(val) > 0);
// }
void removeAll(double val) { // O(N)
int j = 0;
for(int i=0;i<numElements;i++)
if(nums[i] != val)
nums[j++] = nums[i];
numElements = j;
}
double findMin() {
// return the minimum value among elements in nums;
double minV = nums[0];
for(int i=1;i<numElements;i++)
if( nums[i] < minV )
minV = nums[i];
return minV;
}
void sort() { // There is a bug in this code. Find it.
// sort 'nums' in ascending order. eg) 10, 20 , 30
for(int i=0;i<numElements-1;i++) {
int minIdx = i;
for(int j=i+1;j<numElements;j++)
if( nums[j] < nums[minIdx])
minIdx = j;
// swap between nums[i] and nums[minIdx]
double temp = nums[i];
nums[i] = nums[minIdx];
nums[minIdx] = temp;
}
}
double[] toArray() {
// return a copy of the array only with valid elements
//return nums; // this is not a right to return a copy.
double[] new_nums = new double[numElements];
for(int i=0;i<numElements;i++)
new_nums[i] = nums[i];
return new_nums;
}
public MyArrayForDouble clone(){
// return a copy of this instance/object
MyArrayForDouble nums1 = new MyArrayForDouble(this.toArray());
return nums1;
}
public void clear() {
numElements = 0;
}
// In the above class ‘MyArray’, define a new method ‘getElements(int start, int end)’
// that returns a new array. The new array should have elements
// of ‘nums’ from index ‘start’ to ‘end’ inclusively.
// For example, suppose nums = {10,20,30,40,50}. Then getElements(2,4)
// returns a new array {30,40,50}. Assume that index ‘start’ and ‘end’ are valid
// (you don’t need to check their validity).
public double[] getElements(int start, int end) {
double[] new_nums = new double[end-start+1];
for(int i= start; i <= end ; i++)
new_nums[i-start] = nums[i];
return new_nums;
}
boolean isEmpty() { return numElements == 0; }
boolean isFull() { return numElements==nums.length; }
}

----------------------------------------------------------------------
public class MyArrayDemo {
static void printArray(int[] nums){
for(int i=0; i<nums.length;i++)
System.out.print(nums[i]+" ");
System.out.println();
}
public static void main(String[] args) {
// MyArrayForDouble mynums1 = new MyArrayForDouble();
// mynums1.add(10.5); mynums1.add(1.9); mynums1.add(-0.2); mynums1.add(10.5); mynums1.printArray();
// System.out.println(mynums1.linearSearch(1.9));
// mynums1.remove(1.9); mynums1.printArray();
// mynums1.removeAll(10.5); mynums1.printArray();
// Add your code to test MyArrayForString and MyArrayForChar
// --> Required.
}
}

Create MyArrayForString.java to handle 'String' type elements and
create MyArrayForChar.java to handle 'char' type elements.

Test them in MyArrayDemo.java.

Solutions

Expert Solution

MyArrayForString .java

public class MyArrayForString {
String[] strings;
int stringElements;
public MyArrayForString() { // Constructor. automatically called when creating an instance
stringElements = 0;
strings = new String[5];
}
public MyArrayForString(int capacity) { // Constructor. automatically called when creating an instance
stringElements = 0;
strings = new String[capacity];
}
public MyArrayForString(String[] strings1) {
strings = new String[strings1.length];
for(int i=0;i<strings1.length;i++)
strings[i] = strings1[i];
stringElements = strings1.length;
}
void printArray(){ // cost, times
System.out.printf("printArray(%d,%d): ",stringElements,strings.length);
for(int i=0; i<stringElements;i++)
System.out.print(strings[i]+" ");
System.out.println();
}
int linearSearch(String val) {
for(int i=0;i<stringElements;i++)
if(strings[i] == val)
return i;
return -1;
}
int binarySearch(String val) {
int start = 0;
int end = strings.length - 1;
int mid;
while(start <= end) {
mid = (start + end)/2;
if(val == strings[mid])
return mid;
else if(val.compareTo(strings[mid])>0)
end = mid-1;
else // val > strings[mid]
start = mid + 1;
}
return -1;
}
private void enlarge() {
// String up the size of strings;
String[] new_strings = new String[strings.length*2];
for(int i=0;i<stringElements;i++)
new_strings[i] = strings[i];
strings = new_strings;
}
void add(String val) {
if(isFull()) // if(stringElements == strings.length)
enlarge();
strings[stringElements] = val;
stringElements++;
}
public void addOrder(int idx, String[] valArray) {
// add all elements of valArray from the specified position of strings.
// need to keep order
// eg) [10,20,30] --> addOrder(1,{1,2}) makes {10,1,2,20,30}
ensureCapacity(stringElements + valArray.length);
// Here we can safely assume 'strings' has at least 'stringElements + valArray.length' spaces
for(int i=stringElements-1; i>=idx ; i--)
strings[i+valArray.length] = strings[i];
for(int i=0;i<valArray.length;i++)
strings[i+idx] = valArray[i];
stringElements += valArray.length;
}
private void ensureCapacity(int count) {
if(count <= strings.length)
return;
// need more space
String[] new_strings = new String[count];
for(int i=0;i<stringElements;i++)
new_strings[i] = strings[i];
strings = new_strings;
}
int remove(String val){
// search for an element that is equal to val. and remove it from 'strings'
int idx = linearSearch(val);
if(idx < 0)
return 0;
// fill the location with the last element
strings[idx] = strings[stringElements-1];
stringElements--;
return 1;
}
// void removeAll(int val) { // worst-case: latter half elements are equal to val
// // remove(10) in strings = {1, 2, 3, 2, 4, ..., 10, 10, 10, 10}
// // O(N) * N = O(N*N)
// // search for all the elements equal to val and remove them.
// while(remove(val) > 0);
// }
void removeAll(String val) { // O(N)
int j = 0;
for(int i=0;i<stringElements;i++)
if(strings[i] != val)
strings[j++] = strings[i];
stringElements = j;
}
String findMin() {
// return the minimum value among elements in strings;
String minV = strings[0];
for(int i=1;i<stringElements;i++)
if( strings[i].compareTo(minV) > 0)
minV = strings[i];
return minV;
}
void sort() { // There is a bug in this code. Find it.
// sort 'strings' in ascending order. eg) 10, 20 , 30
for(int i=0;i<stringElements-1;i++) {
int minIdx = i;
for(int j=i+1;j<stringElements;j++)
if( strings[j].compareTo(strings[minIdx]) > 0)
minIdx = j;
// swap between strings[i] and strings[minIdx]
String temp = strings[i];
strings[i] = strings[minIdx];
strings[minIdx] = temp;
}
}
String[] toArray() {
// return a copy of the array only with valid elements
//return strings; // this is not a right to return a copy.
String[] new_strings = new String[stringElements];
for(int i=0;i<stringElements;i++)
new_strings[i] = strings[i];
return new_strings;
}
public MyArrayForString clone(){
// return a copy of this instance/object
MyArrayForString strings1 = new MyArrayForString(this.toArray());
return strings1;
}
public void clear() {
stringElements = 0;
}
// In the above class ‘MyArray’, define a new method ‘getElements(int start, int end)’
// that returns a new array. The new array should have elements
// of ‘strings’ from index ‘start’ to ‘end’ inclusively.
// For example, suppose strings = {10,20,30,40,50}. Then getElements(2,4)
// returns a new array {30,40,50}. Assume that index ‘start’ and ‘end’ are valid
// (you don’t need to check their validity).
public String[] getElements(int start, int end) {
String[] new_strings = new String[end-start+1];
for(int i= start; i <= end ; i++)
new_strings[i-start] = strings[i];
return new_strings;
}
boolean isEmpty() { return stringElements == 0; }
boolean isFull() { return stringElements==strings.length; }
}

MyArrayDemo.java

public class MyArrayDemo {
static void printArray(String[] strings){
for(int i=0; i<strings.length;i++)
System.out.print(strings[i]+" ");
System.out.println();
}
public static void main(String[] args) {
MyArrayForString mystrings1 = new MyArrayForString();
mystrings1.add("Kevin");
mystrings1.add("Bob");
mystrings1.add("John");
mystrings1.add("David");
mystrings1.printArray();
System.out.println(mystrings1.linearSearch("John"));
System.out.println(mystrings1.linearSearch("Mic"));
mystrings1.remove("Bob");
mystrings1.printArray();
String s[] = {"David","Peter", "David"};
mystrings1.addOrder(1,s);
mystrings1.printArray();
mystrings1.removeAll("David");
mystrings1.printArray();
}
}

output screenshot:

MyArrayForChar.java

public class MyArrayForChar {
char[] chars;
int charElements;
public MyArrayForChar() { // Constructor. automatically called when creating an instance
charElements = 0;
chars = new char[5];
}
public MyArrayForChar(int capacity) { // Constructor. automatically called when creating an instance
charElements = 0;
chars = new char[capacity];
}
public MyArrayForChar(char[] chars1) {
chars = new char[chars1.length];
for(int i=0;i<chars1.length;i++)
chars[i] = chars1[i];
charElements = chars1.length;
}
void printArray(){ // cost, times
System.out.printf("printArray(%d,%d): ",charElements,chars.length);
for(int i=0; i<charElements;i++)
System.out.print(chars[i]+" ");
System.out.println();
}
int linearSearch(char val) {
for(int i=0;i<charElements;i++)
if(chars[i] == val)
return i;
return -1;
}
int binarySearch(char val) {
int start = 0;
int end = chars.length - 1;
int mid;
while(start <= end) {
mid = (start + end)/2;
if(val == chars[mid])
return mid;
else if(val < chars[mid])
end = mid-1;
else // val > chars[mid]
start = mid + 1;
}
return -1;
}
private void enlarge() {
// char up the size of chars;
char[] new_chars = new char[chars.length*2];
for(int i=0;i<charElements;i++)
new_chars[i] = chars[i];
chars = new_chars;
}
void add(char val) {
if(isFull()) // if(charElements == chars.length)
enlarge();
chars[charElements] = val;
charElements++;
}
public void addOrder(int idx, char[] valArray) {
// add all elements of valArray from the specified position of chars.
// need to keep order
// eg) [10,20,30] --> addOrder(1,{1,2}) makes {10,1,2,20,30}
ensureCapacity(charElements + valArray.length);
// Here we can safely assume 'chars' has at least 'charElements + valArray.length' spaces
for(int i=charElements-1; i>=idx ; i--)
chars[i+valArray.length] = chars[i];
for(int i=0;i<valArray.length;i++)
chars[i+idx] = valArray[i];
charElements += valArray.length;
}
private void ensureCapacity(int count) {
if(count <= chars.length)
return;
// need more space
char[] new_chars = new char[count];
for(int i=0;i<charElements;i++)
new_chars[i] = chars[i];
chars = new_chars;
}
int remove(char val){
// search for an element that is equal to val. and remove it from 'chars'
int idx = linearSearch(val);
if(idx < 0)
return 0;
// fill the location with the last element
chars[idx] = chars[charElements-1];
charElements--;
return 1;
}
// void removeAll(int val) { // worst-case: latter half elements are equal to val
// // remove(10) in chars = {1, 2, 3, 2, 4, ..., 10, 10, 10, 10}
// // O(N) * N = O(N*N)
// // search for all the elements equal to val and remove them.
// while(remove(val) > 0);
// }
void removeAll(char val) { // O(N)
int j = 0;
for(int i=0;i<charElements;i++)
if(chars[i] != val)
chars[j++] = chars[i];
charElements = j;
}
char findMin() {
// return the minimum value among elements in chars;
char minV = chars[0];
for(int i=1;i<charElements;i++)
if( chars[i] < minV)
minV = chars[i];
return minV;
}
void sort() { // There is a bug in this code. Find it.
// sort 'chars' in ascending order. eg) 10, 20 , 30
for(int i=0;i<charElements-1;i++) {
int minIdx = i;
for(int j=i+1;j<charElements;j++)
if( chars[j] < chars[minIdx])
minIdx = j;
// swap between chars[i] and chars[minIdx]
char temp = chars[i];
chars[i] = chars[minIdx];
chars[minIdx] = temp;
}
}
char[] toArray() {
// return a copy of the array only with valid elements
//return chars; // this is not a right to return a copy.
char[] new_chars = new char[charElements];
for(int i=0;i<charElements;i++)
new_chars[i] = chars[i];
return new_chars;
}
public MyArrayForChar clone(){
// return a copy of this instance/object
MyArrayForChar chars1 = new MyArrayForChar(this.toArray());
return chars1;
}
public void clear() {
charElements = 0;
}
// In the above class ‘MyArray’, define a new method ‘getElements(int start, int end)’
// that returns a new array. The new array should have elements
// of ‘chars’ from index ‘start’ to ‘end’ inclusively.
// For example, suppose chars = {10,20,30,40,50}. Then getElements(2,4)
// returns a new array {30,40,50}. Assume that index ‘start’ and ‘end’ are valid
// (you don’t need to check their validity).
public char[] getElements(int start, int end) {
char[] new_chars = new char[end-start+1];
for(int i= start; i <= end ; i++)
new_chars[i-start] = chars[i];
return new_chars;
}
boolean isEmpty() { return charElements == 0; }
boolean isFull() { return charElements==chars.length; }
}

MyArrayDemo.java

public class MyArrayDemo {
static void printArray(char[] chars){
for(int i=0; i<chars.length;i++)
System.out.print(chars[i]+" ");
System.out.println();
}
public static void main(String[] args) {
MyArrayForChar mychars1 = new MyArrayForChar();
mychars1.add('B');
mychars1.add('A');
mychars1.add('R');
mychars1.add('C');
mychars1.printArray();
System.out.println(mychars1.linearSearch('A'));
System.out.println(mychars1.linearSearch('D'));
mychars1.remove('C');
mychars1.printArray();
char s[] = {'C', 'A', 'D'};
mychars1.addOrder(1,s);
mychars1.printArray();
mychars1.removeAll('A');
mychars1.printArray();
}
}

output screenshot:


Related Solutions

Files I need to be edited: I wrote these files and I put them in a...
Files I need to be edited: I wrote these files and I put them in a folder labeled Project 7. I am using this as a study tool for a personal project of mine. //main.cpp #include <iostream> #include "staticarray.h" using namespace std; int main( ) {    StaticArray a;    cout << "Printing empty array -- next line should be blank\n";    a.print();    /*    // Loop to append 100 through 110 to a and check return value    // Should print "Couldn't append 110"...
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • Create an array called “boyNames” and store all names from the BoyNames.txt file • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file • Create a text menu that allowing users to: 1. Enter a name and the application must display a message indicating if the name is among the most popular...
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
This code has to be in java (I code in eclipse). Also these instructions have to...
This code has to be in java (I code in eclipse). Also these instructions have to be followed exactly because if not my output won't match the expected out ( this will be uploaded on zybooks). Write a program that asks the user for their age in days. The program will compute the person's age in years (you can assume that all years have 365 days) and then prints one of the following messages: If the user is 1 year...
I have two questions that need to be answered, I can't figure them out: 1) Steve...
I have two questions that need to be answered, I can't figure them out: 1) Steve Coleman has just won the state lottery and has the following three payout options for afterminus?tax prize? money:1. $ 152 comma 000$152,000 per year at the end of each of the next six years2. $ 318 comma 000$318,000 ?(lump sum) now3. $ 500 comma 000$500,000 ?(lump sum) six years from now The annual discount rate is? 9%. Compute the present value of the second...
C++ and Java Zoo File Manager I need to complete these files: #include <iostream> #include <jni.h>...
C++ and Java Zoo File Manager I need to complete these files: #include <iostream> #include <jni.h> using namespace std; void GenerateData() //DO NOT TOUCH CODE IN THIS METHOD { JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine) JNIEnv *env; // Pointer to native interface //================== prepare loading of Java VM ============================ JavaVMInitArgs vm_args; // Initialization arguments JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options options[0].optionString = (char*) "-Djava.class.path="; // where to find java .class vm_args.version = JNI_VERSION_1_6;...
B has to be matched with A so please I need both in Java. the previous...
B has to be matched with A so please I need both in Java. the previous project mean A A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array. B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Please I need this to be done in Java, can I have it answered by anonymous...
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java? Thank you You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention. Algorithm: Inputs: integers m and n , assumes m and n are >= 1 Order is not important for this algorithm Local variables integers: remainder Initialize: No initialization...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT