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.
}
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:
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;
}
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;
}
}

I need help making this work correctly. I'm trying to do an array but it is...
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...
7.9.1: Find 2D array max and min. Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Min miles: -10 Max miles: 40 import java.util.Scanner; public class ArraysKeyValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_ROWS = 2; final int NUM_COLS = 2; int [][] milesTracker = new int[NUM_ROWS][NUM_COLS]; int i; int j; int...
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...
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...
. 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...
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 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...
I am almost done with this code, but for some reason I am still getting an error message: Some of the instructions were: Using a loop, prompt for a currency to find. Inside the loop that manages the input, call the lookUpCurrency() method can pass the inputted currency code value. The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the code in the array. If found, return true - otherwise return...
hi I need to understand how the out put become -4 in first question and out put 310 for the second question please can you explain to me thanks a lot 1.(ch12. Sc16. P. 820. loop) Consider the following code: import java.util.*; public class MyLoopOneSGTest{ public static void main(String[] args){ int w = 3; System.out.println(myLoop(w)); } public static int myLoop(int n){ int x = 1; for (int i = 1; i < n; i++){ x = x - i; }...
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...