Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the first part of this assignment, you will design and write a class that rep

ID: 653691 • Letter: I

Question

In the first part of this assignment, you will design and write a class that represents a real-world object formed around a collection of numeric values. The collection can be any type of collection you wish except for any used in class or the textbook examples.

In the second part, you will write a program that demonstrates the use of the class.

In the third part, you will write java doc style comments for the Part I class you wrote.

Part I:

Select a "real-world" object that has not been used in class lecture and/or the textbook.

The class that you choose to represent the object must minimally meet the following requirements:

The "data part must declare the attributes correctly consisting of:

A miniumum of the two attributes of :

An array that represents a collection of items of the same type.

A counter that represents the number of items in the collection.

The "operations" part must include a minimum of methods that perform the following when called:

A no-arg constructor method that creates an object with reasonable defaults for the initial state, which in this case is an empty collection of numeric values.

adds a user supplied value to the collection.

returns the count of items in the collection.

returns the sum of the values in the collection.

returns the minimum value in the collection.

returns the maximum value in the collection.

returns the average of the values in the collection.

searches for the parameter target value, returning true if found and false otherwise.

prints all of the values stored in the array.

The set and get methods for each attribute.

Bonus 1 : An additional constructor that takes an array of initial values for the object's array as a parameter.

Bonus 2 : Add a method to this class that returns the median value of the collection.

Bonus 3 : Add a method to this class that returns the mode of the collection.

Part II: Write a "client" class that demonstrates the use of the object. Examples from class: Sales which uses class SalesData & CalcAverage which uses Grader.

Note that the examples are "similar" but not identical to your assignment.

Part III: Javadoc is a tool that generates html documentation from Javadoc comments in the code.

To use javadoc

add Javadoc style comments for the class heading and method headings.

run the javadoc program with the source file as an argument.

The javadoc program will create a file named index.html and "several" other documentation files in the same directory as the input file.

When you double-click on index.html, the browser will display the documentation created by javadoc for your class as a web page such as those you have viewed for the Scannner class or the Random class from the Java API.

Here is an example to use for #1.

Explanation / Answer

package com.tutai;

import java.util.ArrayList;

class MyClass {
  
   /** The collection. */
   private ArrayList<Integer> collection;
  
   /** The no of items. */
   private int noOfItems;
  
   /**
   * Instantiates a new my class.
   */
   MyClass(){
       collection=new ArrayList<Integer>();
       noOfItems=0;
   }
  
   /**
   * Instantiates a new my class.
   *
   * @param c the c
   */
   MyClass(ArrayList<Integer> c){
       collection=c;
   }
  
   /**
   * Sets the collection.
   *
   * @param c the new collection
   */
   public void setCollection(ArrayList<Integer> c){
       collection=c;
   }
  
   /**
   * returns the collection.
   *
   * @return the collection
   */
   public ArrayList<Integer> getCollection(){
       return collection;
   }
  
   /**
   * Sets the no of items.
   *
   * @param n the new no of items
   */
   public void setNoOfItems(int n){
       noOfItems=n;
   }
  
   /**
   * Gets the no of items.
   *
   * @return the no of items
   */
   public int getNoOfItems(){
       return noOfItems;
   }
  
   /**
   * Adds the item.
   *
   * @param item the item to be added
   */
   public void addItem(int item){
       collection.add(item);
       noOfItems+=1;
   }
  
   /**
   * Count of items.
   *
   * @return the no of items in collection
   */
   public int countOfItems(){
       return noOfItems;
   }
  
   /**
   * Sum.
   *
   * @return the sum
   */
   public int sum(){
       int sum=0;
       for(int i=0;i<collection.size();i++){
           sum+=collection.get(i);
       }
       return sum;
   }
  
   /**
   * Min.
   *
   * @return the min value
   */
   public int min(){
       int minValue=collection.get(0);
       int item;
       for(int i=0;i<collection.size();i++){
           item=collection.get(i);
           if(item<minValue){
               minValue=item;
           }
       }
       return minValue;
   }
  
   /**
   * Max.
   *
   * @return the max value
   */
   public int max(){
       int maxValue=collection.get(0);
       int item;
       for(int i=1;i<collection.size();i++){
           item=collection.get(i);
           if(item>maxValue){
               maxValue=item;
           }
       }
       return maxValue;
   }
  
   /**
   * Avg.
   *
   * @return the average
   */
   public double avg(){
       double avgValue=0.0;
       float sum=0;
       for(int i=0;i<collection.size();i++){
           sum+=collection.get(i);
       }
       avgValue=sum/collection.size();
       return avgValue;
   }
  
   /**
   * Find an element in the collection
   *
   * @param key the item to be found
   * @return true, if item is found
   */
   public boolean find(int key){
       for(int i=0;i<collection.size();i++){
           if(collection.get(i)==key){
               return true;
           }
       }
       return false;
   }
  
   /**
   * Prints the collection
   */
   public void print(){
       System.out.println("Values stored in the collection are:");
       for(int i=0;i<collection.size();i++){
           System.out.println(collection.get(i));
       }
   }
  
   /**
   * Median.
   *
   * @return the median
   */
   public double median()
   {

       // Sort our array
       ArrayList<Integer> sortedList = bubbleSort(collection);
      
       double median = 0;
      
       // If our array's length is even, then we need to find the average of the two centered values
       if (sortedList.size() % 2 == 0)
       {
           int indexA = (sortedList.size() - 1) / 2;
           int indexB = sortedList.size() / 2;
          
           median = ((double) (sortedList.get(indexA) + sortedList.get(indexB))) / 2;
       }
       // Else if our array's length is odd, then we simply find the value at the center index
       else
       {
           int index = (sortedList.size() - 1) / 2;
           median = sortedList.get(index);
       }
                      
       return median;  
   }
  
   /**
   * Bubble sort.
   *
   * @param c the collection
   * @return the sorted collection
   */
   public ArrayList<Integer> bubbleSort(ArrayList<Integer> c){
       int temp;
       for(int i=0;i<c.size()-1;i++){
           for(int j=0;j<c.size()-i-1;j++){
               if (c.get(j) > c.get(j+1)) /* For descending order use < */
                {
                  temp = c.get(j);
                  c.set(j,c.get(j+1));
                  c.set(j+1,temp);
                }
           }
       }
       return c;
   }
  
   /**
   * Mode.
   *
   * @return the mode
   */
   public int mode()
   {
      
       int modeCount = 0;   // The count of the mode value
       int mode = 0;       // The value of the mode

       int currCount = 0;  
       int candidateMode;
       int element;
       // Iterate through all values in our array and consider it as a possible mode
       for (int i=0;i<collection.size();i++)
       {
           candidateMode=collection.get(i);
           // Reset the number of times we have seen the current value
           currCount = 0;
          
           // Iterate through the array counting the number of times we see the current candidate mode
           for (int j=0;j<collection.size();j++)
           {
               element=collection.get(j);
               // If they match, increment the current count
               if (candidateMode == element)
               {
                   currCount++;
               }
           }
          
           // We only save this candidate mode, if its count is greater than the current mode
           // we have stored in the "mode" variable
           if (currCount > modeCount)
           {
               modeCount = currCount;
               mode = candidateMode;
           }
       }
      
       return mode;
   }
}

/**
* The Class Client.
*/
public class Client{
  
   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args){
       MyClass mc=new MyClass();
       mc.addItem(5);
       mc.addItem(4);
       mc.addItem(3);
       mc.addItem(3);
       System.out.println("Sum: "+mc.sum());
       System.out.println("Average: "+mc.avg());
       System.out.println("Minimun: "+mc.min());
       System.out.println("Maximum: "+mc.max());
       System.out.println("Count: "+mc.countOfItems());
       if(mc.find(4)==true){
           System.out.println("4 is present");
       }else{
           System.out.println("4 is NOT present");
       }
       if(mc.find(1)==true){
           System.out.println("1 is present");
       }else{
           System.out.println("1 is NOT present");
       }
       mc.print();
       System.out.println("Median: "+mc.median());
       System.out.println("Mode:"+mc.mode());
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote