Question

In: Computer Science

1. Create a new Java project called L2 and a class named L2 2. Create a...

1. Create a new Java project called L2 and a class named L2
2. Create a second class called ArrayExaminer.
3. In the ArrayExaminer class declare the following instance variables:
a. String named textFileName
b. Array of 20 integers named numArray (Only do the 1st half of the declaration here:
int [] numArray; )
c. Integer variable named largest
d. Integer value named largestIndex
4. Add the following methods to this class:
a. A constructor with one String parameter that
i. does the second half of declaring numArray
(numArray = new int [20];).
ii. Sets textFileName equal to the parameter
iii. Sets the other instance variables to 0.
b. A void method named fillArray that will read 20 integers from a text file (textFileName) and fill the array with the values.
It should also print the array all on one line. (Hint – use the Arrays.toString)
c. void method named findLargest (no parameters) that reads through numArray and sets the instance variable, largest, equal to the largest item in the array, and sets largestIndex equal to its index
Print the value of largest (with a label).
d. boolean method named goingUp (no parameters) that
i. returns a true if all the values in numArray from the beginning up to the largestIndex position are increasing, and false otherwise. (Example of increasing [1,2,6,9,23] )
e. boolean method named goingDown (no parameters) that
i. returns a true if all the values in numArray from largestIndex position until the end are decreasing, and false otherwise. (Example of decreasing [31,12,6,2,1])
f. void method named isPeak (no parameters) that
i. calls findLargest
ii. if goingUp and goingDown are both true, then print that the array has a peak. Otherwise print that it does not have a peak.
5. Back in the main method/class.
a. Declare and instantiate an ArrayExaminer object named array1 and send “L2-1.txt”) as the parameter to its constructor.
b. Call fillArray for array1
c. Call isPeak for array1
d. Declare and instantiate an ArrayExaminer object named array2 and send “L2-2.txt”) as the parameter to its constructor.
e. Call fillArray for array2
f. Call isPeak for array2

L2-1.txt

2
10
17
23
29
38
50
51
62
63
81
85
97
86
75
76
61
59
42
0

L2-2.txt
2
10
17
23
29
38
50
51
62
63
81
85
97
86
75
70
61
59
42
0

Solutions

Expert Solution

Implementation in JAVA:

Code:


// import some neccesary files
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

// class named L2
public class L2 {

//   main method or driver method
   public static void main(String[] args) throws FileNotFoundException {
      
       System.out.println("\n Outputs for File 1 : ");
      
//       path of file L2-1
       String L21="C:\\Users\\Ankit\\Desktop\\L2-1.txt";
      
//       create object of ArrayExaminer class and pass path string into constructor
       ArrayExaminer ae = new ArrayExaminer(L21);
      
//       call the functions
      
       ae.fillArray();
      
       ae.findLargest();
      
       System.out.println("Array is going up : "+ae.goingup());
      
       System.out.println("Array is going down : "+ae.goingdown());
      
       ae.isPeak();
      
      
//       same done for file 2
System.out.println("\n\n Outputs for File 2 : ");
      
       String L22="C:\\Users\\Ankit\\Desktop\\L2-2.txt";
      
       ArrayExaminer ae1 = new ArrayExaminer(L22);
      
       ae1.fillArray();
      
       ae1.findLargest();
      
       System.out.println("Array is going up : "+ae1.goingup());
      
       System.out.println("Array is going down : "+ae1.goingdown());
      
       ae1.isPeak();

   }

}

// class named ArrayExaminer
class ArrayExaminer{
  
//   instance Variables
  
   String textFilename;
  
   int arr[];
  
   int largest;
  
   int largestindex;
  
//   constructor
   public ArrayExaminer(String textFilename ) {
      
       this.textFilename=textFilename;
      
       arr = new int[20];
      
       largest=0;
      
       largestindex=0;
      
   }
  
//   read file and fill into array
   public void fillArray() throws FileNotFoundException {
      
//       create object of File class and pass path of file
       File file = new File(textFilename);
      
//       Scanner object
       Scanner s = new Scanner(file);
         
       int i=0;
         
//       read file and Store into array
       try
       {
       while( s.hasNext() )
       {
          
           int element = s.nextInt();
          
           arr[i]=element;
          
           i++;
          
       }
       }
       finally
       {
       s.close();
       }
         
//       print Array into one line
       System.out.println("\nArray : "+Arrays.toString(arr));
         
   }
  
//   find largest element and its index
   public void findLargest() {
      
      
       for(int i=0;i<arr.length;i++) {
          
           if(largest<arr[i]) {
              
               largest=arr[i];
              
               largestindex=i;
           }
          
       }
      
       System.out.println("The largest element in Array is : "+largest);
       System.out.println("The largest element index in Array is : "+largestindex);
      
   }
  
//   return is array is going up
   public boolean goingup() {
      
       for(int i=1;i<largestindex;i++) {
          
           if(arr[i-1]>arr[i]) {
               return false;
           }
          
       }
      
       return true;
   }
  
//   return is array is going down
public boolean goingdown() {
      
       for(int i=largestindex+1;i<arr.length;i++) {
          
           if(arr[i-1]<arr[i]) {
               return false;
           }
          
       }
      
       return true;
   }

// print oeak State of Array
public void isPeak() {
     
   if(goingup() && goingdown()) {
         
       System.out.println("Array has a peak.");
         
   }
   else {
     
   System.out.println("Array Does not has a peak.");
   }
}

  
  
}

SAMPLE OUTPUT;

TEXTFILE L2-1;

TEXTFILE L2-2

// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in commnets
// THANK YOU:-)


Related Solutions

Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
open up a new Java project on Eclipse named Review and create a new class called...
open up a new Java project on Eclipse named Review and create a new class called Review.java. Copy and paste the below starter code into your file: /** * @author * @author * CIS 36B */ //write your two import statements here public class Review {        public static void main(String[] args) { //don't forget IOException         File infile = new File("scores.txt");         //declare scores array         //Use a for or while loop to read in...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Step 1: Create a new Java project named ContainerPartyProject --------- Step 2: Add a ContainerParty class...
Step 1: Create a new Java project named ContainerPartyProject --------- Step 2: Add a ContainerParty class to your project. It should contain:             - The date of the party             - A list of people attending             - A list of containers at the party             - The address of the party (this can be either a String or a Class) --------- Step 3: Add a Container class in your project. It should be an abstract class. Your Container class...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT