Question

In: Computer Science

Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...

Write these java classes:

1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such:

Write the method body for the default constructor

Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here:

public class DynArray
{
private double[] array;
private int size;
private int nextIndex;
  
public int arraySize()
{

}
  
public int elements()
{

}

public DynArray()
{

}
  
private void grow()
{

}
  
private void shrink()
{

}
}

2) DynArrayDriver.java A simple driver class to test your DynArray class, you must write this class from scratch.

Thanks.

Solutions

Expert Solution

//Create java class DynArray.java


public class DynArray {

public double array[];
private int size;
private int nextIndex;

//constructor to initialise the variabls
public DynArray() {
array = new double[1];
nextIndex = 0;//next index is 0
size = 1;// size of the array initially 1
}

public int arraySize() {
//return size of array
return size;
}

public int elements() {
//return element count
return nextIndex;
}
//makes size double of array

private void grow() {
double temp[] = null;
if (nextIndex == size) {
temp = new double[2 * size];//temp array is double in size
for (int i = 0; i < size; i++) {
temp[i] = array[i];//copy all elements in temp array
}
}
array = temp;
size = size * 2;
}

public void shrink() {
double temp[] = null;
if (nextIndex > 0) {

// temp is a count size array
// and store array elements
temp = new double[nextIndex];
for (int i = 0; i < nextIndex; i++) {

// copy all array value into temp
temp[i] = array[i];
}

size = nextIndex;

// count size array temp initialize
// into variable array again
array = temp;
}
}

//we need to add method to add new element in array
public void add(double data) {
// check no of element is equql to size of array
if (nextIndex == size) {
grow(); // make array size double
} // insert element at end of array
array[nextIndex] = data;
nextIndex++;
}
}
____________________________________________________________________________________________

//Craete driver class DynArrayDriver.java

public class DynArrayDriver {

//driver class for DynArray.java
public static void main(String[] args) {
//create object of DynArray class
DynArray dyn = new DynArray();
System.out.println("Size of DynArray " + dyn.arraySize());//check size of array using arraySIze function
System.out.println("Total elements in DynArray " + dyn.elements());//get element count using elements()
dyn.add(10);//add element to the array
  
dyn.add(25);
System.out.println("Size of DynArray "+dyn.arraySize());
dyn.add(60);
dyn.add(1.5);
System.out.println("Total Elements in Array "+dyn.elements());
System.out.println("Elements of DynArray");
for(int i=0;i<dyn.elements();i++)
{
System.out.print(dyn.array[i]+" ");//here array needs to public to access it in another class
}
System.out.println("");
}
}

_____________________________________OUTPUT_______________________________________________


Related Solutions

Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
Write a Java application, and an additional class to represent some real-world entity such as a...
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods). The class will: Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use...
This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use it as a base class: /** * Simple representation of a bank account * * @author Jo Belle * @version 0.5 (10/12/2020) */ import java.text.NumberFormat; public class SimpleBankAccount{ // fields (instance variables) private double balance; private String accountId; /** * Constructor for objects of class SimpleBankAccount */ public SimpleBankAccount(){ balance = 0.0; accountId = ""; } /** * Constructor for objects of class SimpleBankAccount...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT