Hi.
I'm having difficulty with a current Java assignment. I believe if I can get it started properly (and know how the whole object, method, main thing works) I could finish the rest. The assignment is :
Write a Java program that will create and manipulate an array of
double values in several different ways. You will have at least
two classes. Let's call them Mid2 and DVector. You may have more
classes as you need (many of you will want class Prompter, since
there will be keyboard input).
Mid2: class containing the main method
DVector: class that will be used in Mid2
The program will be similar in structure to assignment 7, in that
the main class Mid2 is a "driver" that will allow a user to create
and manipulate an object (or objects) of class DVector.
Here are the details of what each class should do.
-------------------------------
class DVector
-------------------------------
This class will contain an array of double (among other fields) and
will have several different methods for manipulating that array
(note that this will be a similar situation to your assignment 7
class Student having an array of grades):
public void show()
method "show" will print out all the elements in the array in
some nice readable fashion (show the value, and the slot number).
You probably want to write this one first, as it will be helpful
in determining if your other methods are working correctly as
you write them.
public void fill()
method "fill" with no parameters will read double values from the
keyboard and put them into the array. Get a value of every slot
in the array. Do not worry about error checking the input values;
you may assume the user will type well formed doubles.
public boolean fill(int n)
this version of fill will get keyboard input, reading doubles from
the user, but it will only put values into the first "n" slots.
Return "false" if the parameter n is too large (i.e., larger than
the maximum number of slots in the array) and don't do any input;
otherwise, do the keyboard input and return "true".
public void fill(double val)
write a second method called "fill" (overloaded method) that takes
a single parameter of type double; this version of fill will put
the parameter value into each array element; for example, the
call fill(0.0) will set each array element to zero, whereas a call
fill(7.3) will put the value 7.3 into each array element.
public boolean add(double val)
this method will put the parameter value into the next open slot
in the array. You will have to check to make sure there is an open
slot (make sure the array is not full already).
public boolean set(int slot, double val)
method "set" will take an integer and a double as parameters, and
put the double value into the array slot indicated by the integer
parameter. This method will return a boolean and you will have to
do some simple error checking in the method. First, you must check
the integer that is passed to make sure it can be used as a valid
subscript... i.e., that it is 0 or greater, and it is not larger
than the size of the array. Return true if the integer is good and
the set can be done; return false if the integer is bad, and do not
change any array values in that case.
public boolean full()
this method returns true if all array slots have values in them;
it returns false if there is at least one empty array slot.
public int size()
this method returns the number of elements stored in the array
public int max()
this method return the total number of slots in the array, i.e.,
maximum number of elements that could be stored
public double sum()
method "sum" will find the total sum of all elements in the array
public double largest()
method "largest" will find and return the largest value in the array.
public int filter(double val)
this method takes a single parameter of type double; it will compare
each array element to the parameter value, and set an element to
zero if the element is smaller than the parameter value; the method
will return the count of how many elements got set to 0.0; for example,
if 3 elements get changed, return a 3; if no elements are changed,
return a 0.
constructors as needed
You know you need a constructor that takes a single integer as
argument and uses that integer to set the size of the array that
is created (see Mid2 discussion; this is like you did in assignment
7 class Student to set the array length).
-------------------------------
class Mid2
-------------------------------
This class will contain the public static void main method.
It will read one run line parameter -- an integer that will tell how
many slots should be in the array (remember to do proper error
checking... you may assume the user types a well-formed integer,
but you need to make sure that integer is larger than 0).
Once you have a good integer value from the run line parameter, you
will create an object of class DVector and pass this integer parameter
to the DVector constructor. This integer will tell DVector how large
to make the array.
Once you have the object of class DVector you will do this:
a) enter a loop that asks the user what method he/she wants to run on
the DVector object. You can do this in several different ways...
print a menu of choices and let the user give an integer (like we did
way back in an early Javascript program, the converter). You can
designate choices by integer or string... you pick a way.
b) When the user makes a choice, you will call the corresponding method
on the object of class DVector (you may need to ask for more input...
for example, if the user wants to run method "set"). Remember to
make one of your choices "quit". Remember to also make two of your
choices the "fill" methods so the user can re-fill the array easily
if desired.
c) When you run the selected method on the object, print out the results
you get back (if there are return values). You might wish to use the
method "show" to show what is in the array after some operations.
-- Start with Mid2, get main running to where it accepts the
command line parameter... start without error checking and just
make sure you type in a correct positive integer for the run
line argument when you test.
-- Now create class DVector but only put one or two useful methods
in it... like the constructor to make the array, and something
like display, and fill(double val). With these, you will have
an array full of values, and can see them. This will allow then
growing the other methods.
-- Now add the code in main that creates the object of class DVector.
Since the compiler puts 0.0 in doubles by default, you should be
able to call show() on the object and see the values.
-- Now add code to allow the user to select a method to run and
make the object run it. Obviously at this point the only
methods you can choose are fill(double val) and display.
-- once you have fill(double val) working, you can do the smaller
methods like sum(), max(), size(), etc. and get a nice sense of
progress.
-- Now add the method "fill()" to DVector... this means adding code
for keyboard reading to that class.
-- Add the call to fill() back in class Mid2... then test
-- Add more methods to DVector, and more invocation choices to Mid2...
test... grow it until you have all methods.
Good ones at this point might be largest(), filter, etc.
-- after you have these working, you can go back and create the
other forms of fill, and the add method (which grows an un-full
array one element at a time) and the set method. Since you have
methods like show, largest, sum already, you will be able to see
if your new methods work properly.
-- Add error checking where input values are obtained to make the
program robust.
Can anyone help me?