Question

JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students....

JAVA Question

a)
Read a file named testScoreIn.txt representing test scores for a class of students.
Each record contains an id number and three test scores (Note: the test scores are integers).
For example, a student record might look like:
BK1234 87 72 91

b)
Without using an array, read each student’s information, one record at a time and compute the student’s average (as a double).

c)
Write to an output file, testScoreOut.txt, as a neatly formatted table with column headings, each student's id, three grades and average grade (as a real number to 3 decimal places)

d)
At the end, when the the sentinel ID, "LastID", is read, print to the screen the id number of the student with the highest average.

Homework Answers

Answer #1

Code:-

Main.java

import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
class Main{

public static void main (String[] args) throws Exception,IOException{

//for reading each line from textScoreIn.txt

File file_in = new File("textScoreIn.txt");
Scanner sc = new Scanner(file_in);

FileWriter fw = new FileWriter("textScoreOut.txt");

String id_1="";

Double avg=Double.valueOf(0);

while (sc.hasNextLine()){

String id=sc.next();

int f = sc.nextInt();

int s = sc.nextInt();

int t = sc.nextInt();

Double average =(Double.valueOf(f)+Double.valueOf(s)+Double.valueOf(t))/3;

if(average>avg){ //To get the maximum average and id

id_1=id;

avg=average;

}

String p=id+" "+String.valueOf(f)+" "+String.valueOf(s)+" "+String.valueOf(t)+" "+Double.toString(average)+"\n";

fw.write(p);

}

sc.close();

fw.close();

  

System.out.println("The Student ID having the highest average is");

System.out.println(id_1);

}

}

Output:-

Screenshot of Code:-

textScoreIn.txt

if u like my answer then please give a thumbs up..!!

Know the answer?
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
Similar Questions
Write a Java Program, that opens the file "students.txt" The program must read the file line...
Write a Java Program, that opens the file "students.txt" The program must read the file line by line The program parses each line that it reads For example, for this line: 1:mohamed:ali:0504123456:cs102:cs202 The program must print    >ID = 1    >First Name = Mohamed   >Last Name = Ali   >Mobie = 0504123456   >Courses = cs102, cs202 In addition, it adds the mobile phone number into an ArrayList called studentPhoneList Print the content and the size of studentPhoneList Show your results and provide...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to...
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to generate and populate Parallel-Array Data Structure: const int NG = 4; //Number of Grades string names[] = {"Amy Adams", "Bob Barr", "Carla Carr", "Dan Dobbs", "Elena Evans" }; int exams[][NG]= { {98,87,93,88}, {78,86,82,91}, {66,71,85,94}, {72,63,77,69}, {91,83,76,60} };    --------------------------------------------------------------------------------- 1 A) Create and Populate a Parallel-Array Data Structure using the code described in "StudentDataStructure.txt". B) Define a Record Data Structure for student records. It...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT