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 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....
Using Java, define a train class and write code to demonstrate its functionality The train should...
Using Java, define a train class and write code to demonstrate its functionality The train should run between Boston and New York, stopping at different, different station along the way. Provide output to indicate the train’s current status.
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
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...
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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT