Question

This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

This is JAVA language
Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have the following methods:  A default constructor that creates the array with room for 100 integers and set the number of elements to 0  A fillArray method that will read integers from the file data.txt and store them in the array. This method has been partially written for you. You may assume that the file has no more than 100 items in it.  A method that finds and returns the smallest value in the array, looking only at the values actually read in from the file.  A method that finds and returns the largest value in the array, looking only at the values actually read in from the file.  A method that accepts an integer parameter and returns the index of the first instance of the value in the array (looking only at the values actually read in 2 from the file). If the value is not found, the method will return -1. Make sure that you stop looking as soon as you find the value. When you submit your program, your main must do the following: 1) Create an ArrayManager object using the default constructor 2) Call fillArray with that object 3) Print out the smallest value in the array with an appropriate label (having used the appropriate method to determine that value) 4) Print out the largest value in the array with an appropriate label (having used the appropriate method to determine that value) 5) Use a sentinel-controlled loop to ask the user for values and use the appropriate method to look up each value, printing the index with a label if found and “Not found” otherwise. Use a sentinel value of -999. Do not search for the sentinel value. Part 2 Implement the program you designed for prelab. Remember that you are using the indices of the array to represent the temperatures and then storing the actual counts in the array. Do not store any temperatures in an array. Your printout should not include any highs that never appeared in the file, only values that actually show up in the file. The temperatures will be read from the file temps.txt. Use the example code in ArrayManager’s fillArray method to help you write the file reading piece. Call your program HighTemps.java. You may do this entire program in main if you wish. Sample output: Temperatures Number of Days -40 3 -10 1 -3 1 11 1 45 3 69 2 78 2 79 1 89 1 99 1 110 1

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

1)

// ArrayManager.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ArrayManager {
   private int temps[] = null;
   private int counter;

   public ArrayManager() {
       this.temps = new int[100];
       this.counter = 0;
   }

   public void fillArray() {
       int val;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       //Getting the input entered by the user
System.out.print("Enter a number (999 to exit) :");
val=sc.nextInt();
while(val!=999)
{
   temps[counter]=val;
   counter++;
       //Getting the input entered by the user
System.out.print("Enter a number (999 to exit) :");
val=sc.nextInt();
}

   }

   public int findLargest() {
       int max = temps[0];
       for (int i = 0; i < counter; i++) {
           if (max < temps[i]) {
               max = temps[i];
           }
       }
       return max;
   }

   public int findSmallest() {
       int min = temps[0];
       for (int i = 0; i < counter; i++) {
           if (min > temps[i]) {
               min = temps[i];
           }
       }
       return min;
   }
public int search(int number)
{
   int indx=-1;
   for(int i=0;i<counter;i++)
   {
       if(temps[i]==number)
           indx=i;
   }
   return indx;
}
}

_________________________

// MoreArrayFun.java

import java.util.Scanner;

public class MoreArrayFun {

   public static void main(String[] args) {
       int temp;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);


       ArrayManager arm=new ArrayManager();
       arm.fillArray();
       //Getting the input entered by the user
System.out.print("Enter an element to search :");
temp=sc.nextInt();
int indx=arm.search(temp);
if(indx==-1)
{
   System.out.println(temp+" is not found in the array.");
}
else
{
   System.out.println(temp+" is found in the array at index position "+indx);
}
       System.out.println("The Largest Temperature is :"+arm.findLargest());
       System.out.println("The Smallest Temperature is :"+arm.findSmallest());

   }

}

_________________________

Output:

Enter a number (999 to exit) :56
Enter a number (999 to exit) :67
Enter a number (999 to exit) :78
Enter a number (999 to exit) :-34
Enter a number (999 to exit) :45
Enter a number (999 to exit) :56
Enter a number (999 to exit) :-77
Enter a number (999 to exit) :-89
Enter a number (999 to exit) :999
Enter an element to search :56
56 is found in the array at index position 5
The Largest Temperature is :78
The Smallest Temperature is :-89

___________________________

2)

// data.txt

-40 3 -10 1 -3 1 11 1 45 3 69 2 78 2 79 1 89 1 99 1 110 1

______________________

// ArrayManager.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ArrayManager {
   private int temps[] = null;
   private int counter;

   public ArrayManager() {
       this.temps = new int[100];
       this.counter = 0;
   }

   public void fillArray() {
       try {
           Scanner sc = new Scanner(new File("data.txt"));
           while (sc.hasNext()) {
               temps[counter] = sc.nextInt();
               counter++;
           }
           sc.close();
       } catch (FileNotFoundException e) {

           System.out.println("** File Not Found **");
       }

   }

   public int findLargest() {
       int max = temps[0];
       for (int i = 0; i < counter; i++) {
           if (max < temps[i]) {
               max = temps[i];
           }
       }
       return max;
   }

   public int findSmallest() {
       int min = temps[0];
       for (int i = 0; i < counter; i++) {
           if (min > temps[i]) {
               min = temps[i];
           }
       }
       return min;
   }

}

__________________________

// HighTemps.java

public class HighTemps {

   public static void main(String[] args) {
       ArrayManager arm=new ArrayManager();
       arm.fillArray();
       System.out.println("The Largest Temperature is :"+arm.findLargest());
       System.out.println("The Smallest Temperature is :"+arm.findSmallest());
   }

}
___________________________

Output:

The Largest Temperature is :110
The Smallest Temperature is :-40

______________________

Add a comment
Know the answer?
Add Answer to:
This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...
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
  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • In this assignment you will create two Java programs: The first program will be a class...

    In this assignment you will create two Java programs: The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above. You cannot use any of the home assignments as your submitted assignment. Your programs must meet the following qualitative and...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

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