This is the assignment.....
Write a class DataSet that stores a number of values of type double. Provide a constructor
public DataSet(int maxNumberOfValues)
and a method
public void addValue(double value)
that add a value provided there is still room.
Provide methods to compute the sum, average, maximum and minimum value.
This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE; are there so I don't need to create an element. I need to use enhanced for loop, not if else statments. thank you
import java.util.Scanner;
public class DataSet1 {
double[]values;
int sum;
double average;
double maximum;
double minimum;
public DataSet1(int maxNumberOfValues){
values = new
double[maxNumberOfValues];
}
public void addValue(int[]values){
for(int i=0; i<values.length;i++){
sum+= values[i];
}
}
public double getSum(double[] values){
for(int i=0; i<values.length;i++){
sum+= values[i];
}
return sum;
}
public double getAverage(){
double total =0;
for(double element : values){
total = total + element;
}
double average = 0;
if(values.length>0){
average = total/values.length;
}
return sum/values.length;
}
public double getMaximum(){
double largest = Double.MIN_VALUE;
for(int i =
1;i<values.length;i++){
if(values[i]<largest)
largest=values[i];
}
return Math.max(largest, largest);
}
public double getMinimum(){
double smallest = Double.MAX_VALUE;
for(int i =0;i<values.length;i++){
if(values[i]<smallest)
smallest = values[i];
}
return Math.min(smallest, smallest);
}
public static void main(String[] args){
final int LENGTH = 100;
double[]values = new double[LENGTH];
DataSet1 d1 = new DataSet1(LENGTH);
int currentSize =0;
Scanner in=new Scanner(System.in);
System.out.print("Enter number : ");
while(in.hasNextDouble()){
values[currentSize]=in.nextDouble();
currentSize++;
}
System.out.println("Sum is : "+d1.getSum(values));
System.out.println("Average is :"+d1.getAverage());
System.out.println("Maximum is :"+d1.getMaximum());
System.out.println("Minimum is :"+d1.getMinimum());
}
}
JAVA :
import java.util.*;
import java.util.Arrays;
class DataSet1 {
double[] values;
int sum;
double average;
double maximum;
double minimum;
public DataSet1(int maxNumberOfValues) {
values = new double[maxNumberOfValues];
}
public void addValue(int[] values) {
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
}
public double getSum(double[] values) {
sum = 0;
for (int i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
public double getAverage() {
double total = 0;
for (double element : values) {
total = total + element;
}
double average = 0;
if (values.length > 0) {
average = total / values.length;
}
return sum / values.length;
}
public double getMaximum() {
double largest = Double.MIN_VALUE;
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
largest = Math.max(largest, values[i]);
}
return largest;
}
public double getMinimum() {
double smallest = Double.MAX_VALUE;
for (int i = 0; i < values.length; i++) {
smallest = Math.min(smallest, values[i]);
}
return smallest;
}
public static void main(String[] args) {
final int LENGTH = 100;
int currentSize = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter number : ");
int size = in.nextInt();
DataSet1 d1 = new DataSet1(size);
for(int i=0;i<size;i++){
d1.values[currentSize] = in.nextDouble();
currentSize++;
}
// d1.values = values;
System.out.println("Sum is : " + d1.getSum(d1.values));
System.out.println("Average is :" + d1.getAverage());
System.out.println("Maximum is :" + d1.getMaximum());
System.out.println("Minimum is :" + d1.getMinimum());
}
}
OUTPUT:
Enter number : 5
2
1
3
5
4
Sum is : 15.0
Average is :3.0
2.0
1.0
3.0
5.0
4.0
Maximum is :5.0
Minimum is :1.0
This is the assignment..... Write a class DataSet that stores a number of values of type...
I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores; public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i]; } } public double getAverage() { int sum=0;...
USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please do not change the DataSetDemo code. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element. Skeleton Code: DataSet: /** Computes the maximum of a set of data values. */ public class DataSet { private int[] values; private int first; private int last; /** Constructs a DataSet object. @param...
Need help with the UML for this code? Thank you. import java.util.Scanner; public class Assignment1Duong1895 { public static void header() { System.out.println("\tWelcome to St. Joseph's College"); } public static void main(String[] args) { Scanner input = new Scanner(System.in); int d; header(); System.out.println("Enter number of items to process"); d = input.nextInt(); ...
Consider the following classes: Dollhouse, Legos, TeddyBear, Toy, ToyBox Which should not be included in a class hierarchy? ToyBox Dollhouse TeddyBear Legos Toy __________________ Questions 6 - 9 Refer to the following code: public class WhatsIt { private int[] values; private double average; public WhatsIt () { values = new int[10]; findAvg(); } public WhatsIt (int[] n) { values = n; findAvg(); } public void findAvg () { double sum = 0; for (int i...
Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation { public static void main(String[] args) { ...
In the code shown above are
two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM
ON THE SAME CLASS BUT SO WE CAN RECALL them threw different
methods. Thank you.
1-First exercise
import java.util.Scanner;
public class first {
public static int average(int[] array){
int total = 0;
for(int x: array)
total += x;
return total / array.length;
}
public static double average(double[] array){
double total = 0;
for(double x: array)
total += x;
return total /...
Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList { private ShoppingItem [] list; private int amtItems = 0; public ShoppingList() { list=new ShoppingItem[8]; } public void add(ShoppingItem item) { if(amtItems<8) { list[amtItems] = ShoppingItem(item);...