so guys I did this assignment and ended up with not so good grade, would like to
ID: 3813812 • Letter: S
Question
so guys I did this assignment and ended up with not so good grade, would like to see others do it so that I can learn from it the biggest problem was the directions. so take your time while working on this. Thanks in advance the genius out there
11.111111111Pay VERY CLOSE attention to the directions!! pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee and thanksssssssssssssss.
If the student does not follow the directions EXACTLY, they will receive an automatic grade of "F" on this assignment! I
a. Create a new Java class inside your project folder.
The name of the class should be: ListStats
Write a program that will read in a list of positive integers (including zero) and display some statistics regarding the integers.
The user is assumed to enter the list in sorted order, starting with the smallest number and ending with the largest number.
Your program must store the all of the integers in an array. The maximum number of integers that you can expect is 100, however there may not necessarily be that many. Your program should allow the user to continue entering numbers until they enter a negative number or until the array is filled - whichever occurs first. If the user enters 100 numbers, you should output an message stating that the maximum size for the list has been reached, then proceed with the calculations.
After you have placed all of the integers into an array, your program should perform the following tasks, in this order:
Display the count of how many numbers were read
Display the smallest number in the array
Display the largest number in the array
Display the median (the median is the integer that is stored in the middle position of the array)
Display the average of all the numbers
Allow the user to search the array for a specified value.
First, ask the user to enter an integer to search for.
Next, search the array to determine if the given integer is in the array.
If the integer is not found, display a message stating that the integer is not in the list.
If the integer is found, then display the position number of where you found the integer. If the integer happens to be in the array more than once, then you only need to tell the first position number where you found it.
After performing the search, ask the user if he/she wants to search for more integers. Continue searching for numbers until the user answers "N".
Technical notes and restrictions:
Your program MUST perform the exact steps in the exact order listed above.
Remember that the keyboard object should be declared as a class variable (global).
Absolutely NO other global variables (class variables) are allowed (except for the keyboard object).
You are only allowed to use the number 100 one time in your program! You are not allowed to use the numbers 101, 99, 98, or any other number that is logically related to 100 anywhere in your program. If you need to make reference to the array size then, use the length variable to reference this rather than hard-coding numbers into your program.
You are required to write and use at least the following FOUR methods in this program:
A method to get the numbers from the user, and place them into an array. This method should return the number of integers that it actually retrieved. The array will be passed back through a parameter (remember that the method can make changes to an array's contents and the main program will automatically see the results).
A method to calculate, and return, the median of the array. This will be a double value.
If there are an odd number of integers in the array, then the median is defined as the integer stored in the middle array position.
If there are an even number number of integers in the array, then the median is defined as the average of the integers stored in the two middle array positions.
A method to calculate, and return, the average of the numbers in the array. This will be a double value.
A method to search for a specified value in the array. This method should return the first position number where the item is found, or return -1 if the item is not found.
The two calculating methods, and the searching method, should not get any values from the user, and should not print anything to the screen. The main program is responsible for all of the printing.
The method which gets the numbers will need (of course) to get values from the user, but should not print anything to the screen.
Remember that at this stage, COMMENTING IS A REQUIREMENT! Make sure you FULLY comment your program. You must include comments that explain what sections of code is doing. Notice the key word "sections"! This means that you need not comment every line in your program. Write comments that describe a few lines of code rather than "over-commenting" your program.
You MUST write comments about every method that you create. Your comments must describe the purpose of the method, the purpose of every parameter the method needs, and the purpose of the return value (if the method has one).
Build incrementally. Don't tackle the entire program at once. Write a little section of the program, then test it AND get it working BEFORE you go any further. Once you know that this section works, then you can add a little more to your program. Stop and test the new code. Once you get it working, then add a little bit more.
Make sure you FULLY test your program! Make sure to run your program multiple times, inputting combinations of values that will test all possible conditions for your IF statements and loops. Also be sure to test border-line cases.
Explanation / Answer
import java.util.*;
public class ListStats
{
// To take input from the user through keyboard
Scanner sc = new Scanner(System.in);
// Main method of the class
public static void main(String args[])
{
// Creates object 'ls' for the class to access its objects
ListStats ls = new ListStats();
int list[] = new int[100];
System.out.println("Please enter the positive integers(including zero) in the ascending order: ");
//calls the method insert to add elements to the list
int listLength = ls.insert(list);
if(listLength == 100)
{
System.out.println("Maximum size for the list has been reached.");
}
//Display the count of integers in the array
System.out.println("The total number of integers entered by the user in the list: "+listLength);
//Display the smallest number in the array
System.out.println("The smallest number in the array is: " + list[0]);
//Display the largest number in the array
System.out.println("The largest number in the array is: " + list[listLength-1]);
//Display the median of the array
System.out.println("The median of the array is: "+ ls.calculateMedian(list,listLength));
//Display average of all the numbers in the array
System.out.println("The average of all the numbers in the array is: "+ ls.calculateAverage(list,listLength));
//Display the existence of a number in the array
String choice = "";
do
{
System.out.println("Enter the number to be searched for in the array: ");
int element = ls.sc.nextInt();
if(ls.find(list,listLength,element) == -1)
System.out.println("Specified number is not found in the array");
else
System.out.println("Specified number is found at position " + ls.find(list,listLength,element)+ " in the array.");
System.out.println("Do you want to continue? If yes, type /YES/ and if no, type /NO/");
choice = ls.sc.next();
}while(!choice.equals("NO"));
}
/*
This method inserts the integers into the array
@parameters
list: This is the array into which integers are inserted
@return
i: It is the integer value that contains the number of integers entered by the user into the array
*/
int insert(int []list)
{
int i;
for(i = 0; i < 100; i++)
{
int number = sc.nextInt();
if(number >= 0 && i < 100)
{
list[i] = number;
}
else
{
break;
}
}
return i;
}
/*
This method calculates the median value for the array of integers
@parameters
list: This is the array of integers whose median value is found
listLength : This is the count of number of integers in the array
@return
median: It is the double value that contains the calculated median value
*/
double calculateMedian(int [] list, int listLength)
{
double median;
int index;
if(listLength % 2 != 0 )
{
index = listLength/2;
median = list[index];
}
else
{
index = listLength/2;
double element1 = list[index];
double element2 = list[index-1];
median = (element1+element2)/2;
}
return median;
}
/*
This method calculates the average value for the array of integers
@parameters
list: This is the array of integers whose median value is found
listLength : This is the count of number of integers in the array
@return
median: It is the double value that contains the calculated average value
*/
double calculateAverage(int []list, int listLength)
{
int sum = 0;
double average = 0;
for(int i=0; i < listLength; i++)
{
sum = sum + list[i];
}
average = sum/listLength;
return average;
}
/*
This method searches for a specifed integer in the array
@parameters
list: This is the array of integers whose median value is found
listLength : This is the count of number of integers in the array
element : This is the integer to be searched for in the array
@return
index: It is the int value that contains position of the element in the array if it is found in the array, else -1
*/
int find(int []list, int listLength, int element)
{
int index;
int flag = 0;
int i;
for(i = 0; i< listLength; i++)
{
if(list[i] == element)
{
flag = 1;
break;
}
}
if(flag == 1)
return (i+1);
else
return -1;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.