The code given below is not working:
My tasks are to,
- Write a program that generates 500 Random Integers and stores them into an array. Integer values will be between 1 - 1000.
- Print the 500 Random Integers to
.txt file
- The main method will control the run of the program. (menu, etc.)
- Create a menu using the if/else ladder or switch case to control the operation of the program. The menu should be in main.
- Create a “programmer created class” called Common_Algo
- Read the 500 Random Integers from th
.txt file
you created in Task 2: - Use the class constructor to fill the array with random integers from the .txt file. The 500 integer array (arr[500]) will be an instance variable in the “programmer created class” - Common_Algo
- All methods below will be encapsulated together with the 500 integer array in the Common_Algo class.
- The main method from outside the Common_Algo class will control the operation of the program.
Implement methods to achieve each outcome given below.
(Your menu will call the methods. All methods will operate on the same Array.)
Enter 1. Output all values
Enter 2. Output Sum of All Values and the Mean Average of Values
Enter 3. Output all odd numbers
Enter 4. Output all even numbers\ Enter 5. Output Middle Values (Values in the Middle)
Enter 6. Output First Value in the Array
Enter 7. Output Last Value in the Array
Enter 8. Enter a Search Value
Enter 9. Output Sorted Array
Enter 10. Output the Highest Value and its location in the array.
Enter 11: Output Lowest Value and its location in the array.
Enter 12: Exit
**Following is what I have implemented so far but it does not compile on the command prompt.**
import java.io.*;
import java.util.*;
public class Common_Algo
{
private int i;
int [] arr;
public Common_Algo()
{
try {
FileReader file = new FileReader("random.txt");
Scanner scanner = new Scanner(file);
arr = new int [500];
i = 0;
while (scanner.hasNextInt())
{
arr[i++] = scanner.nextInt();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
void printAllValues()
{
for (i = 0; i<500; i++)
{
System.out.println(arr[i]);
}
}
void sumAndMean()
{
// calculate the sum
int sum = 0;
float average = 0;
for (int i = 0; i< arr.length; i++){
sum = sum + arr[i];
}
System.out.println("The sum of all the elements in the array " + sum);
// calculate the average
average = sum/arr.length;
System.out.println("Average of all vlaues of an array:" + average);
}
void oddNumber()
{
System.out.println("Odd numbers in array is:");
for(int i = 0; i<arr.length; i++)
{
if (arr[i]%2 ==0)
{
System.out.println(arr[i]);
}
}
}
void evenNumber()
{
System.out.println("Even numbers in array is:");
for(int i=0; i<arr.length; i++)
{
if (arr[i]%2==0)
{
System.out.println(arr[i]);
}
}
}
void middleValue(){
int m = 0;
if (arr.length%2 == 1)
{
m=(arr[(arr.length + 1)/2-1]);
}
else
{
m=(arr[arr.length/2-1]+arr[arr.length/2])/2;
}
System.out.println("Middle Value = "+ m);
}
void firstValue(){
System.out.println("The first value in the array is: " + arr[0]);
}
void lastValue()
{
System.out.println("The last value in the array is: " +arr[arr.length-1]);
}
void searchValue()
{
Scanner scanner = new Scanner (System.in);
int search_Value,flag=0;
System.out.println("Enter search value: ");
search_Value = scanner.nextInt();
//this does linear search for searching the value in array
for (int i = 0; i<arr.length; i++)
{
if (search_Value == arr[i])
{
System.out.println("Value " + search_Value +" is found at location "+ i);
flag = 1;
}
}
if (flag == 0)
{
System.out.println("Value " + search_Value + " is not found inside the array");
}
}
void sortArray()
{
Arrays.sort(arr);
System.out.println("The sort array list: ");
//sort array list
for (i=0; i<arr.length; i++){
System.out.println(arr[i]);
}
}
void highValue()
{
int max = arr [0];
for (int i = 1; i < arr.length; i++){
if (arr[i] > max){
max = arr[i];
System.out.println("The highest value of array is: " + max);
}
}
}
void lowValue()
{
int min = arr[0];
for (int i = 1; i <arr.length; i++)
if (arr[i] < min)
min = arr[i];
System.out.println("The lowest value in the array: " + min);
}
}
class RandomData
{
public static void main(String[] args) {
int i,choice;
Scanner scanner = new Scanner(System.in);
Random rnum = new Random();
try {
PrintWriter fileout = new PrintWriter(new FileWriter ("random.txt"));
for (i = 0; i<500; i++)
{
fileout.println(rnum.nextInt(1000));
}
fileout.close();
}
catch (Exception e)
{
System.out.println(e);
}
Common_Algo object = new Common_Algo();
do
{
System.out.println("Enter 1 for: Output of all values ");
System.out.println("Enter 2 for: Ouput of Sum and mean Average values");
System.out.println("Enter 3 for: Output of all odd numbers");
System.out.println("Enter 4 for: Output of all even numbers" );
System.out.println("Enter 5 for: Output of middle values");
System.out.println("Enter 6 for: Output of first value in the array");
System.out.println("Enter 7 for: Output of last vlaue in the array");
System.out.println("Enter 8 for: Output of the entered search value");
System.out.println("Enter 9 for: Output of the Sorted Array");
System.out.println("Enter 10 for: Output of the highest value and its location in the array");
System.out.println("Enter 11 for: Output of lowest value and its location in the array");
System.out.println("Enter 12 for: Exit");
System.out.println("Please enter your choice");
choice = scanner.nextInt();
switch(choice)
{
case 1:
object.printAllValues();
break;
case 2:
object.sumAndMean();
break;
case 3:
object.oddNumber();
break;
case 4:
object.evenNumber();
break;
case 5:
object.middleValue();
break;
case 6:
object.firstValue();
break;
case 7:
object.lastValue();
break;
case 8:
object.searchValue();
break;
case 9:
object.sortArray();
break;
case 10:
object.highValue();
break;
case 11:
object.lowValue();
break;
case 12:
System.exit(0);
}
}
while(choice!=12);
}
}
Aucun commentaire:
Enregistrer un commentaire