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...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is...
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....
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
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...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for college) 2. A class named Instructor (more specific, for college) 3. A class named Staff (more specific, for college, HR officer, Marking staff) Tasks: 1. Figure out the relationships among the classes; 2. Determine the abstract class, and the child classes; 3. For the abstract class, determine at least one abstract method; 4. Each class should at least two data members and one extra...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT