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

Step 1. Edit a new file called Array1.java in your lab8 directory (if you don\'t

ID: 3761187 • Letter: S

Question

Step 1. Edit a new file called Array1.java in your lab8 directory (if you don't have a lab8 directory, then create one) and type in a public class Static1 containing a main method with the usual signature: public static void main(String[] arg). Inside this file define another class called Histogram. A Histogram will contain four instance variables, and a constructor as follows.

private String title;
private double start, stop;
private double[] data;

public Histogram(String title, double start, double stop,
double[] data, int numBars)

When you write the constructor, make sure that you create a copy of the array passed to the constructor. Make this array of size numBars and ignore array entries with indexes not less than numBars. This is so that a Histogram object always has a private array, not just a reference to one known to the outside world. In this first part of the lab, you will not use start, stop or title, but the constructor should assign those values to the instance variables anyway. Write a toString method for Histogram so that it returns a string that when printed looks e.g. like this:

####################################
#####################
##################################
#########################

With one row per array entry. You may assume that the array entries are in the range zero through forty. Round the entries to the nearest integer, and concatenate that number of # characters for each row with newlines between, and a newline at the end of the entire String. Write a main method to test creating and printing a Histogram.

Step 2. Copy the file Array1.java to Array2.java, then edit Array2.java so that the name of the public class is Array2, not Array1. Now write a readHistogram method, to read a Histogram from the console. Your method should behave as follows at the console, where the user suppled input is emboldened here to help explain (you won't be able to do that!) e.g.

Enter a histogram.
Title:
My Histogram
Start value:
12.0
End value:
14.5
Data:
2.3 1.55 21.9 13.33 17.25 5.21

You may assume that no more than (say) 1000 numbers will be supplied. Your method should capture this information and then construct and return a Histogram object containing it. Write a main program to get a histogram from the console, and print it out using the toString method developed in part 1.

Step 3. Copy the file Array2.java to Array3.java, then edit Array3.java so that the name of the public class is Array3, not Array2. In this part the start, stop and title values will be used. Edit the toString method as follows. First find the maximum value in the data array. The values in the array are to be scaled so that the longest bar will be of length 40. Furthermore each bar will be labelled with a double. The first bar will be preceded by start, the last by stop, and the others evenly by numbers evenly spaced between the two. Overall, the Title will be first, and below the bottom row in position 40 or thereabouts will be the scale, i.e. a "nice" number near the maximum value. Each bar should be followed by it's length expressed So for the data above, printing the result returned by toString for a Histogram object should look e.g. something like this.

My Histogram
12.0 #### 2.3
12.5 ### 1.55
13.0 ######################################## 21.9
13.5 ########################### 13.33
14.0 ################################## 17.25
14.5 ########## 5.21
                                        20^

You may assume that the numbers supplied are positive, bigger than about 0.01 and smaller than 10,000. Your program should now correctly read, construct, convert to a string and print a Histogram.

Explanation / Answer

Array1.java:

package lab8;
import java.text.DecimalFormat;
import java.util.Arrays;

public class Array1
{
  
   public static void main(String[] args)
   {
       double data[] = {1,2,4,2,4,5,10.8,23,4,9};
       Histogram hist = new Histogram("Title", 0, 10, data, 10);
       System.out.println(hist);
   }
}
class Histogram
{
   private String title;
   private double start,stop;
   private double[] data;
   public Histogram(String title,double start,double stop,double[] data,int numBars)
   {
       this.title = title;
       this.start = start;
       this.stop = stop;
       this.data = new double[numBars];
       for(int i=0;i<numBars;i++)
           this.data[i] = data[i];
   }
  
  
   public String toString()
   {
       String line = this.title+" ";
       double maxValue = data[0];
       for(int i=0;i<data.length;i++)
       {
           if(data[i] > maxValue)
               maxValue = data[i];
       }
       int scaledData[] = new int[data.length];
       for(int i=0;i<data.length;i++)
       {
           scaledData[i] = (int)Math.round(40*(data[i]/maxValue));
       }
       double prev = start;
       double inc = (stop - start)/(data.length-1);
       DecimalFormat df = new DecimalFormat("0.00");
       for(int i=0;i<this.data.length;i++)
       {
           line += df.format(prev);
           prev += inc;
           for(int j=0;j<scaledData[i];j++)
           {
               line += "#";
           }
           line+= data[i]+" ";
       }
       return line;
   }
}

//#############################################################################

Array2.java:

package lab8;
import java.text.DecimalFormat;
import java.util.*;

public class Array2
{
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
       double data[] = {1,2,4,2,4,5,10.8,23,4,9};
       Histogram hist = readHistogram(sc);
       System.out.println(hist);
   }
   public static Histogram readHistogram(Scanner sc)
   {
       System.out.println("Enter a histogram.");
       System.out.println("Title:");
       String title = sc.nextLine();
       System.out.println("Start value:");
       double start = Double.parseDouble(sc.nextLine());
       System.out.println("End value:");
       double end = Double.parseDouble(sc.nextLine());
       System.out.println("Data:");
       String dString = sc.nextLine();
       String[] dWords = dString.split(" ");
       System.out.println(Arrays.toString(dWords));
       double[] data = new double[dWords.length];
       for(int i=0;i<data.length;i++)
       {
           data[i] = Double.parseDouble(dWords[i]);
       }
       Histogram hist = new Histogram(title, start, end, data, data.length);
       return hist;
   }
}
class Histogram
{
   private String title;
   private double start,stop;
   private double[] data;
   public Histogram(String title,double start,double stop,double[] data,int numBars)
   {
       this.title = title;
       this.start = start;
       this.stop = stop;
       this.data = new double[numBars];
       for(int i=0;i<numBars;i++)
           this.data[i] = data[i];
   }
  
  
   public String toString()
   {
       String line = this.title+" ";
       double maxValue = data[0];
       for(int i=0;i<data.length;i++)
       {
           if(data[i] > maxValue)
               maxValue = data[i];
       }
       int scaledData[] = new int[data.length];
       for(int i=0;i<data.length;i++)
       {
           scaledData[i] = (int)Math.round(40*(data[i]/maxValue));
       }
       double prev = start;
       double inc = (stop - start)/(data.length-1);
       DecimalFormat df = new DecimalFormat("0.00");
       for(int i=0;i<this.data.length;i++)
       {
           line += df.format(prev);
           prev += inc;
           for(int j=0;j<scaledData[i];j++)
           {
               line += "#";
           }
           line+= data[i]+" ";
       }
       return line;
   }
}

//##############################################################################

Array3.java:

package lab8;
import java.text.DecimalFormat;
import java.util.*;


public class Array3
{
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
       double data[] = {1,2,4,2,4,5,10.8,23,4,9};
       Histogram hist = readHistogram(sc);
       System.out.println(hist);
   }
   public static Histogram readHistogram(Scanner sc)
   {
       System.out.println("Enter a histogram.");
       System.out.println("Title:");
       String title = sc.nextLine();
       System.out.println("Start value:");
       double start = Double.parseDouble(sc.nextLine());
       System.out.println("End value:");
       double end = Double.parseDouble(sc.nextLine());
       System.out.println("Data:");
       String dString = sc.nextLine();
       String[] dWords = dString.split(" ");
       System.out.println(Arrays.toString(dWords));
       double[] data = new double[dWords.length];
       for(int i=0;i<data.length;i++)
       {
           data[i] = Double.parseDouble(dWords[i]);
       }
       Histogram hist = new Histogram(title, start, end, data, data.length);
       return hist;
   }
}


class Histogram
{
   private String title;
   private double start,stop;
   private double[] data;
   public Histogram(String title,double start,double stop,double[] data,int numBars)
   {
       this.title = title;
       this.start = start;
       this.stop = stop;
       this.data = new double[numBars];
       for(int i=0;i<numBars;i++)
           this.data[i] = data[i];
   }
  
  
   public String toString()
   {
       String line = this.title+" ";
       double maxValue = data[0];
       for(int i=0;i<data.length;i++)
       {
           if(data[i] > maxValue)
               maxValue = data[i];
       }
       int scaledData[] = new int[data.length];
       for(int i=0;i<data.length;i++)
       {
           scaledData[i] = (int)Math.round(40*(data[i]/maxValue));
       }
       double prev = start;
       double inc = (stop - start)/(data.length-1);
       DecimalFormat df = new DecimalFormat("0.00");
       for(int i=0;i<this.data.length;i++)
       {
           line += df.format(prev);
           prev += inc;
           for(int j=0;j<scaledData[i];j++)
           {
               line += "#";
           }
           line+= data[i]+" ";
       }
       return line;
   }
}

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