Question

In: Computer Science

Java Programming create a program that calculates the average of the prime values in the file....

Java Programming

create a program that calculates the average of the prime values in the file.

Process:

  1. Read Lines from "Values.txt"
  2. Determine if the line is an integer
  3. Determine if the integer is prime
  4. Keep running average of primes
  5. Print running average of primes (no formatting necessary

"Values.txt"

751
1090
971
1054
7
300
19
834
100
751
549
288
641
989
479
1388
1319
584

Code Outline:


public class ChapterSixMain {
   public static void main(String[] args) {
       // Read Lines from "Values.txt"
       // Determine if the line is an integer
       // Determine if the integer is prime
       // Keep running average of primes
       // Print running average of primes (no formatting necessary)
   }
}

Example 1:

Values.txt Prime Values Average
751
1090
971
1054
7
300
19
834
100
751
549
288
641
989
479
1388
1319
584
751
971
7
19
751
641
479
1319

617.2499999999999

Values.txt Prime Values Average
61
121
124
479
Blackberry
787
416
541
1011
Lemon
1051
493
827
140
Jackfruit
1103
Mango
Blackberry
113
Blackberry
439
Orange
Kiwi
1103
562
504
61
479
787
541
1051
827
1103
113
439
1

Solutions

Expert Solution

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class ChapterSxiMain {

public static void main(String[] args) throws FileNotFoundException {

FileInputStream fis = new FileInputStream("E://values.txt");

Scanner sc = new Scanner(fis);

double avg = 0;

int numOfelement = 0, sum = 0;

// Read Lines from values.txt

while (sc.hasNextLine()) {

// Determine if line is integer

try {

int val = Integer.parseInt(sc.nextLine());

int count = 0;

// Determine if integer is prime

for (int i = 2; i <= val / 2; i++) {

if (val % i == 0)

count++;

}

if (count == 0) {

sum += val;

numOfelement++;

}

// Keep running average of prime

} catch (Exception e) {

}

}

// Print Average of prime

System.out.println(sum * 1.0 / numOfelement);

sc.close();

}

}

Screenshot for reference understanding:

Do UPVOTE, if you found solution helpful!!


Related Solutions

Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
In JAVA • Write a program that calculates the average of a group of test scores,...
In JAVA • Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following methods: • int getScore() should ask the user for a test score, store it in a reference parameter variable, and validate it. This method should be called by main once for each of the five scores to be entered. • void calcAverage() should calculate and display the average of the...
Create a program that reads a file of 2D coordinates and calculates the bounding box and...
Create a program that reads a file of 2D coordinates and calculates the bounding box and center of the bounding box. The bounding box is defined as the minimum area that fully encompasses all the coordinates. The center of that bounding box is calculated by taking the mean of the bounding box coordinates: ( x1+x2 2 , y1+y2 2 ). • If the input file cannot be opened, warn the user by printing "CANNOT OPEN FILE." to stdout. • Print...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Create the flowchart and a C++ .cpp program that calculates the average snowfall for a week....
Create the flowchart and a C++ .cpp program that calculates the average snowfall for a week. The program should use a loop to execute 7 times asking the user to input the snowfall for each day of the week in inches. The program should calculate the total and average snowfall for the week. The program should then display the result and then ask the user if they wish to repeat the program or close the program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT