Question 1 A programmer creates the Java class shown on the facing page, representing a numeric dataset read from a file. The class has a constructor that creates an object containing the data from the file, given the name of that file as a string. It also has a method called mean that returns the arithmetic mean value of the stored data. The questions that follow concern this class. You may include small fragments of code in your answers if appropriate, but you should not reimplement the class. Keep in mind that full marks can be obtained with purely descriptive answers. (a) The class as shown here will not compile. The compiler identifies two issues that need to be fixed, one occurring in the constructor and the other within the mean method. Explain what the issues are and describe how you would fix them. (b) Another programmer who is reviewing the code describes line 6 as 'dangerous'. What possible reason might there be for this comment, and how could you modify the code to fix the problem? (c) Explain the problems with the error handling done on lines 26–29, and describe a better and more flexible way of indicating an error. (d) Imagine that the creator of this class later finds that it would be useful to add more values to the dataset, after a Data object has been created. Explain why the class cannot support this in its current form and describe that changes that you would need to make in order to provide this feature.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Question 1
A programmer creates the Java class shown on the facing page, representing a numeric
dataset read from a file. The class has a constructor that creates an object containing the
data from the file, given the name of that file as a string. It also has a method called mean
that returns the arithmetic mean value of the stored data.
The questions that follow concern this class. You may include small fragments of code in
your answers if appropriate, but you should not reimplement the class. Keep in mind that full
marks can be obtained with purely descriptive answers.
(a) The class as shown here will not compile. The compiler identifies two issues that need
to be fixed, one occurring in the constructor and the other within the mean method.
Explain what the issues are and describe how you would fix them.
(b) Another programmer who is reviewing the code describes line 6 as 'dangerous'. What
possible reason might there be for this comment, and how could you modify the code
to fix the problem?
(c) Explain the problems with the error handling done on lines 26–29, and describe a better
and more flexible way of indicating an error.
(d) Imagine that the creator of this class later finds that it would be useful to add more
values to the dataset, after a Data object has been created. Explain why the class
cannot support this in its current form and describe that changes that you would need
to make in order to provide this feature.
Transcribed Image Text:Question 1 A programmer creates the Java class shown on the facing page, representing a numeric dataset read from a file. The class has a constructor that creates an object containing the data from the file, given the name of that file as a string. It also has a method called mean that returns the arithmetic mean value of the stored data. The questions that follow concern this class. You may include small fragments of code in your answers if appropriate, but you should not reimplement the class. Keep in mind that full marks can be obtained with purely descriptive answers. (a) The class as shown here will not compile. The compiler identifies two issues that need to be fixed, one occurring in the constructor and the other within the mean method. Explain what the issues are and describe how you would fix them. (b) Another programmer who is reviewing the code describes line 6 as 'dangerous'. What possible reason might there be for this comment, and how could you modify the code to fix the problem? (c) Explain the problems with the error handling done on lines 26–29, and describe a better and more flexible way of indicating an error. (d) Imagine that the creator of this class later finds that it would be useful to add more values to the dataset, after a Data object has been created. Explain why the class cannot support this in its current form and describe that changes that you would need to make in order to provide this feature.
1 import java.io.File;
2 import java.util.Scanner;
4 public class Data
5 {
6.
public double[] values;
8
public Data (String filename)
9.
{
File inputFile = new File (filename);
Scanner input = new Scanner (inputFile);
10
%3D
11
12
if (input.hasNext Int ()) {
int size = input.nextInt () ;
values = new double[size];
int i = 0;
13
14
15
16
17
while (input.hasNextDouble () && i < size) {
values[i] = input.nextDouble ();
18
19
i++;
20
}
21
22
23
24
public double mean ()
{
if (values == null || values.length
System.out.println ("No data!");
25
0) {
26
==
27
return 0.0;
29
30
else {
31
double sum;
for (double value: values) {
sum += value;
32
33
34
return sum / values.length;
35
36
}
37
38}
N N N mm
Transcribed Image Text:1 import java.io.File; 2 import java.util.Scanner; 4 public class Data 5 { 6. public double[] values; 8 public Data (String filename) 9. { File inputFile = new File (filename); Scanner input = new Scanner (inputFile); 10 %3D 11 12 if (input.hasNext Int ()) { int size = input.nextInt () ; values = new double[size]; int i = 0; 13 14 15 16 17 while (input.hasNextDouble () && i < size) { values[i] = input.nextDouble (); 18 19 i++; 20 } 21 22 23 24 public double mean () { if (values == null || values.length System.out.println ("No data!"); 25 0) { 26 == 27 return 0.0; 29 30 else { 31 double sum; for (double value: values) { sum += value; 32 33 34 return sum / values.length; 35 36 } 37 38} N N N mm
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education