Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Working with Methods and regular Arrays In this assignment, you are to write a program that behaves as described below. First, you will prompt the

Working with Methods and regular Arrays

In this assignment, you are to write a program that behaves as described below. First, you will prompt the user to enter a size for a numeric array, which you instantiate accordingly. Then you will ask the user to enter that many numbers and fill the array with these values. This array will be a local variable of your main method and will be passed as an argument to some of the methods you create.

image text in transcribed

After, this, your program should loop repeatedly, presenting a menu for the user to select from, like this:

The user will enter a choice, and the program should perform the appropriate operation. The following are done: 1) 1)Determining if a number is prime User enters a number:

User enters a number.

A statement appears indicating whether or not the number is a prime number. Here are a couple examples:

image text in transcribed

2) List all prime numbers below a given value User enters the number:

User enters a number

Response is a list all primes up to that number as shown below (each line displays a maximum of ten values)

image text in transcribed

3) Compute statistics from an array:

Your program will calculate and display the average and the standard deviation for the values in the array. Output for above numbers in array will look like this:

image text in transcribed

4) Perform linear search for number in the array:

User enters the number to search for. If number is found in the array, the position is displayed, otherwise a not found message appears: For the above array values, here are a couple outputs:

image text in transcribed

5) Display a bar chart of values:

Using a bar chart class I provide, display a GUI visualization showing the values in the array.

This bar chart is interactive. If you click a bar a window will pop up showing details, including whether the value is higher or lower than the average of values in the array

image text in transcribed

0) Quit the program

Program says goodbye and terminates:

NOTICE: ERROR HANDLING (Hint: the Exception class has a toString() method that you can call in the catch section)

Your program must handle any errors that the user has. If the user enters an invalid number, or if the user enters a non-numeric value, you must present an error message and allow the user to try again. For example, if the user enters an invalid menu choice, the message could look say something like Invalid choice, please try again. If an exception occurs (for example if a user enters a string when a number is expected), you should catch this exception and print the exception content, then loop to present the menu again. Something like this: image text in transcribed

---------------------------

PROGRAMMING TASKS

For this assignment, you will write a program that includes methods for various tasks. The signatures for these methods and descriptions of the tasks they perform are listed below: 1) static boolean isPrime(int number) This method returns True if the parameter is a prime number. A prime number is a number that has no factors other than itself and 1. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. The trick to test if a number is prime is to make sure it is not divisible by any number (other than 1) lower than itself. So, if you had a loop that went from 2 to the value of the number you are testing, and checked whether the number you are testing was divisible by the loop counter number, and if NONE of these came out true, then you would know it is a prime number. 2) static String listPrimes(int number) This method returns a string that contains all of the prime numbers that are less than or equal to the passed in parameter. For example, if the value of the parameter is 10, then the string should have 1, 2, 3, 5, 7. This method involves a loop in which all the primes are concatenated to the string. When completed, the string is returned. This method should call the isPrime method in order to determine, for each value in the loop, whether it is prime. Try to avoid long lists appearing on a single line. Something like 10 numbers per line of output. 3) static double average(double[] nbrs) This method takes a double array as a parameter and returns the average of its values. 4) static double standardDeviation (double[] nbrs) This method takes a double array as a parameter and returns the standard deviation of its values. This method should call average for the array, then use this value to calculate the standard deviation. 5) public static int linearSearch(double[] list, double value) This performs a linear search of an array of doubles. It returns the position of a value if found in the array, or -1 if not found. You can see an example in chapter 7 of the textbook. 6) static void displayBarChart(double[] values) This uses a barchart package that I provide for you (see below). Note that in addition to preparing data arrays for the bar chart, you will need to call the average function to determine whether a value is higher or lower than the user-entered arrays average. Also note that the bar chart expects integer values so you will need to convert doubles from the original array to integers.

THE main METHOD

Your main method should begin by asking the user how to size the array, then create the array and input values into each element of the array. Next, you should have a loop that presents a menu of options to the user, and then performs the desired option. For some options, your main method will need to ask the user to input the values that will be sent as arguments to the methods, call the appropriate method, and then will need to display the results that were returned from the method. The overall structure of the menu-processing in the main method will be a loop with a nested decision structure (either an IF statement or a SWITCH). Again, as stated above, the above-listed methods DO NOT involve any user interaction. They merely take in parameters, perform the appropriate computations, and return results. All data shared between methods is done via parameter passing. There should be no class-wide variables in this assignment. Also, make sure to do any necessary exception and error handling in the main method.

THE BAR CHART PACKAGE

.You can include this the barchart package in the src file of your NetBeans project and then import the package in your assignment. The BarChartFrame class, which you will instantiate, also includes a main method. From this you can see how a BarChartFrame should be instantiated and the data it needs for displaying the appropriate visualization.

image text in transcribedimage text in transcribedimage text in transcribed

How many numbers in the array? 6 Please enter 6 numbers. 20.5 33 15.2 35 22 19.3 Enter a number: 99 99 is not a prime number Enter a number: 101 a 101 IS a prime number Enter a number: 200 The prime numbers up to 200 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 192 193 197 199 The average of: 20.5 33.0 15.2 35.0 22.0 19.3 is 24.166666666666668 The standard deviation of: 20.5 33.0 15.2 35.0 22.0 19.3 is 7.970110831517129 Enter a double value: 22 The value 22.0 is found in position 4 Enter a double value: 100 The value 100.0 is not in the array Array Values - 35 33 22 20 19 15 Bar 0 Bar 1 Bar 2 Bar 5 details Bar 5 value is 19.3 Bar Lower than average OK Enter a number: x Error. Please try again: java.util.InputMismatchException Enter : 1 -- to determine if a number is prime 2 to list prime numbers below a given value 3 to compute the statistics from an array of numbers 4 -- to perform linear search finding a number in the array 5 -- to display a bar chart of values 0 -- to quit BarChartFrame - Notepad File Edit Format View Help package barchart; import javax.swing.*; import java.awt.*; public class BarChartFrame extends JFrame{ BarChartPanel bcp; public BarChartFrame(String title, int[] values, String[] labels, String[] info) { bcp = new BarChartPanel(values, labels, info, values.length); getContentPane().add(bcp, BorderLayout.CENTER); setSize(800,400); this.setTitle(title); show(); } public static void main(String[] args) { // set up data int[] values = {10,20,30}; String[] labels = {"one","two", "three"}; String[] info = {"one stuff hi", "two stuff \tbye", "three stuff \thi \tbye"}; // instantiate and display BarChartFrame bcf = new BarChartFrame("Test title", values, labels, info); // only do this if you want the application to exit when window is closed bcf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } BarChartPanel - Notepad File Edit Format View Help package barchart; import java.awt."; import java.awt.event.*; import javax.swing.*; public class BarChartPanel extends JPanel // variables for bar drawing private Color[] colors = {Color.blue, Color.red, Color.green, Color.yellow, Color.cyan, Color.pink,Color magenta}; private int[] barValues; private String[] barLabels; private Rectangle[] bars; private String[] info; private int nbrBars; public BarChartPanel(int[] values, String[] labels, String[] info, int nbr){ setValuesAnd Labels (values, labels, nbr); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); for (int i=0; i=getMinBars(){ barValues[--nbrBars] = 0; /brBars = nbrBars - 1; repaint(); } public void paintComponent(Graphics g){ super.paintComponent(s); // get rectangle bounding the graphic region Rectangle r = getBounds(); // r2 will be the region of the bar chart Rectangle r2 = new Rectangle(r.x+20,r.y+20,r.width-20,r.height-60); // set the bar width based on width of // chart and number of bars in chart int bar_width = (r2.width-r2.x)brBars; // find the largest number in the array float largest=-1000; for (int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What are the factors affecting organisation structure?

Answered: 1 week ago

Question

What are the features of Management?

Answered: 1 week ago

Question

Briefly explain the advantages of 'Management by Objectives'

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago