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