Question

I need help making this work correctly. I'm trying to do an array but it is...

I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance.

Here are the parameters:

Create a netbeans project called ArrayStuff with a single java main class also called ArrayStuff. All the code for the lab you will do today is in this single main class. Implement each problem in the main class and test it carefully as you go. In particular, watch out for off by one errors! Use your SafeInput library methods for input.

  1. Declare an array of type int named dataPoints.
    dataPoints should have a length of 100. (i.e. it should hold 100 int values.)
  2. Now, code a regular for loop that iterates through the dataPoints array and initializes each element in it to a random value between 1 and 100.
    (See notes above for using java.util.Random. Please do not use Math.Random!)
  3. Code a second loop that displays the dataPoints values like this (values are all on the same line and separated by “ | “ e.g. space, vertical bar, space):
    val1 | val2 | val3 | HINT: use printf or System.out.print()
  4. Now code a loop that calculates the sum and the average of the values in dataPoints. Add code to output/display the sum and the calculated average. (Be sure to include some sort of description in the output so it is clear (i.e. use complete sentences) : ‘The average of the random array dataPoints is: ‘ or something similar. Don’t just print out the average and sum as a raw numbers. (Do this for all the output here… just like you should in every program you code!)

    Paste the screen snip or copy of your output window here:
    (do the rest the same way…)

  1. Add code to prompt and input an int value between 1 and 100 from the user. (Use your static getRangedInt(…) method from SafeInput from the last lab here! Just include your SafeInput.java Library. file in your Netbeans project and access your bullet-proofed input methods there.   SafeInput.getRangedInt(…)
  2. Code a loop that iterates through the entire dataPoints array and counts how many times the user’s value is found within the array. Indicate with a clear output statement how many times the loop found the user’s value within the array.

    Paste the screen shot of the output here:
  3. Prompt the user again for a val between 1 and 100.
    Code a loop that iterates through the dataPoints array checking each value to see if it matches the one the user input. This loop should short circuit (break) when it finds the value and display the first position of the value within the array. Again, make the output clear and explicit here. (e.g. “The value VALUE was found at array index POSITION” where VALUE is the user’s value you input and POSITION is the location (index) where the value is found in the array. If the value is not in the array, then the output statement should indicate that. Note you can use the break keyword to jump out of a loop before it completes. The problem here is that you have to keep track of whether or not the value was found at all.

    Paste the output from running your code here:
  4. Write a loop to scan for the minimum and maximum values in the dataPoints array. Display these values to the user. (Note that these might not be unique, there might be several occurrences of either the min or the max value in the random array.) Hint: you can assume initially that the first element is the min and max and iterate through the rest of the array to determine if there is a lower or higher value which then becomes the min or max respectively. You can also write a single loop that determines both the min and the max at the same time.
      
    Paste the output from running your code here:
  5. Write this static method which takes an array of double values and returns the average and test it. Remember static methods go in the main java class file immediately after the main method.

    public static double getAverage(int values[])
    {

}

In your main code call it like this:

System.out.printLn(“Average of dataPoints is: “ + getAverage(dataPoints));

Paste the output from running your code here:



  1. Refactor you program and create the following static array methods. Recode your main method so you can compare the values returned by your inline code with the equivalent methods:

    public static int min(int values[])
    Returns the min value found
    public static int max(int values[]) Returns the max value found

public static int occuranceScan(int values[], int target)
            Returns the number of times target is found in the values array
public static int sum(int values[]) Returns the sum of the values array elements

public boolean contains(int values[], int target) Returns true if the values array contains target

Here is my array code, then below is the safeInput class I am also using.

import java.util.Random;
import java.util.Scanner;

/**
*
* @author heynow
*/
public class ArrayStuff {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int dataPoints[]=new int[100];
int count=0;
int index=0;
int min,max;
Random rand = new Random();
  
//initializing array with random numbers between 1 to 100
for(int i=0;i<100;i++){
dataPoints[i] = rand.nextInt(100) + 1;
}
  
//printing array values
System.out.println("Values in dataPoints......");
for(int i=0;i<99;i++){
System.out.print(dataPoints[i]+" | ");
}
System.out.println(dataPoints[99]+" ");
  
//requesting a value between 1 to 100 from user
  
int number=SafeInput.getRangedInt(pipe,"Enter an Integer", 1,100);
//finding the count of value entered
for(int i=0;i<100;i++){
if(number==dataPoints[i])
count++;
}
System.out.println("Count of "+number+" in array: "+count+" ");
  
//requesting a value between 1 to 100 from user

number=SafeInput.getRangedInt(pipe,"Enter an Integer",1,100);
int i;
//finding the first index where value occurs
for(i=0;i<100;i++){
if(number==dataPoints[i]){
index=i;
break;
}
}
if(i<100)
System.out.println("The value "+number+" was found at array index "+index);
else{
System.out.println("Value "+number+" not found in array");
}
System.out.println(" ");
  
//finding the minimum and maximum values
min=dataPoints[0];
max=dataPoints[0];
for(i=1;i<100;i++){
if(min>dataPoints[i])
min=dataPoints[i];
if(max max=dataPoints[i];
}
System.out.println("Minimum value: "+min);
System.out.println("Maximum value: "+max);
  
//finding the average value of dataPoints in array
System.out.println("Average of dataPoints values: "+getAverage(dataPoints));
  
}

}

//////////////////////////////safeInput class//////////////////////////////////////////////////////////////////

public static int getRangedInt(Scanner pipe, String prompt, int low, int high) {
int retInt = 0;
do {
System.out.print("\n" + prompt + ": ");
retInt = pipe.nextInt();
  
} while (retInt <= low - 1 || retInt >= high + 1);
return retInt;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

import java.util.Scanner;

/**

*

* @author heynow

*/

public class Main {

//method to find average of dataPoints

static float getAverage(int dataPoints[]){

int sum=0;

for(int i=0;i<100;i++){

sum=sum+dataPoints[i];

}

return(sum/100);

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

int dataPoints[]=new int[100];

int count=0;

int index=0;

int min,max;

Random rand = new Random();

//initializing array with random numbers between 1 to 100

for(int i=0;i<100;i++){

dataPoints[i] = rand.nextInt(100) + 1;

}

//printing array values

System.out.println("Values in dataPoints......");

for(int i=0;i<99;i++){

System.out.print(dataPoints[i]+" | ");

}

System.out.println(dataPoints[99]+" ");

//requesting a value between 1 to 100 from user

Scanner pipe =new Scanner(System.in);

int number=SafeInput.getRangedInt(pipe,"Enter an Integer", 1,100);

//finding the count of value entered

for(int i=0;i<100;i++){

if(number==dataPoints[i])

count++;

}

System.out.println("Count of "+number+" in array: "+count+" ");

//requesting a value between 1 to 100 from user

number=SafeInput.getRangedInt(pipe,"Enter an Integer",1,100);

int i;

//finding the first index where value occurs

for(i=0;i<100;i++){

if(number==dataPoints[i]){

index=i;

break;

}

}

if(i<100)

System.out.println("The value "+number+" was found at array index "+index);

else{

System.out.println("Value "+number+" not found in array");

}

System.out.println(" ");

//finding the minimum and maximum values

min=dataPoints[0];

max=dataPoints[0];

for(i=1;i<100;i++){

if(min>dataPoints[i])

min=dataPoints[i];

if(max<dataPoints[i])

max=dataPoints[i];

}

System.out.println("Minimum value: "+min);

System.out.println("Maximum value: "+max);

//finding the average value of dataPoints in array

System.out.println("Average of dataPoints values: "+getAverage(dataPoints));

}

}

//////////////////////////////safeInputclass//////////////////////////

class SafeInput{

public static int getRangedInt(Scanner pipe, String prompt, int low,int high){

int retInt = 0;

do {

System.out.print("\n" + prompt + ": ");

retInt = pipe.nextInt();

} while (retInt <= low - 1 || retInt >= (high+1));

return retInt;

}

}

Add a comment
Know the answer?
Add Answer to:
I need help making this work correctly. I'm trying to do an array but it is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help editing or rewriting java code, I have this program running that creates random numbers...

    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...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I need help asking the user to half the value of the displayed random number as...

    I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in);    ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • 23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a...

    23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit. Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT