How can i change my current code to have the same output but instead of using Fi
ID: 3699033 • Letter: H
Question
How can i change my current code to have the same output but instead of using FileReader and Text files, i would use import java.util.Scanner and would use (Example) reader.hasNext? all this would be in java.
in1.dat:
5
Johnson 93.5
Dirks 76.0
Timm 87.0
Faulk 55.5
Miser 69.0
Example Output (the names and scores would be aligned under their proper header in the column:
Student Name Raw Score With Curve
Johnson 64.00 87.00
Dirks 76.00 99.00
Timm 67.00 90.00
Faulk 55.50 78.50
Miser 49.00 72.00
Johns 50.00 73.00
Dirkson 52.50 75.50
Timmson 39.00 62.00
Faulkson 77.00 100.00
in2.dat:
5
Johnson 84.0
Dirks 76.0
Timm 87.5
Faulk 55.5
Miser 69.0
in3.dat:
9
Johnson 64.0
Dirks 76.0
Timm 67.0
Faulk 55.5
Miser 49.0
Johns 50.0
Dirkson 52.5
Timmson 39.0
Faulkson 77.0
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Record {
static String file_name = "E:\in1.txt";
//main method
public static void main(String args[]){
BufferedReader br11 = null;
FileReader fr11 = null;
try {
//array list
ArrayList<String> st_name = new ArrayList<String>();
ArrayList<Double> smarks = new ArrayList<Double>();
ArrayList<Double> smarks_new = new ArrayList<Double>();
fr11 = new FileReader(file_name);
br11 = new BufferedReader(fr11);
String line;
br11 = new BufferedReader(new FileReader(file_name));
while ((line = br11.readLine()) != null) {
String data11[] = line.split(" ");
st_name.add(data11[0]);
smarks.add(Double.parseDouble(data11[1]));
}
double maxvalue = smarks.get(0);
for(int i1=1;i1<smarks.size();i1++){
if(maxvalue<smarks.get(i1))
maxvalue = smarks.get(i1);
}
double diff11 = new Double(100) - maxvalue;
for(int i1=0;i1<smarks.size();i1++){
double d11 = smarks.get(i1);
d11=d11+diff11;
smarks_new.add(d11);
}
System.out.println("Student_Name Score Curve");
for(int i=0;i<smarks.size();i++)
{
System.out.println();
System.out.print(st_name.get(i));
if(st_name.get(i).length()<10)
System.out.print(" ");
else
System.out.print(" ");
System.out.println(smarks.get(i)+" "+smarks_new.get(i));
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (br11 != null)
br11.close();
if (fr11 != null)
fr11.close();
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
Description Write a program in java that reads in a series of student names and exam scores, then adjusts the scores on a curve. The highest score should be increased to 100.0 and all other scores increased by the same number of points. For example, a series of 2 scores 89.0? and 83.0? will become 100.0? and 94.0?, respectively (each increased by 11.0). A series of 2 scores 68.0 and 61.0? would become 100.0? and 93.0?, respectively. The program should print out each student’s name, raw score, and adjusted score.
Notes
? You will need to utilize arrays in this program.
? The input file will begin with a single integer n?, followed by n ?records. Each record consists of a single string (student name) and a decimal value (exam score).
? All exam scores will be between 0.0 and 100.0. This program does not need to check for invalid data.
? Format all exam scores to 2 decimal places.
Explanation / Answer
package January;
import java.io.*;
import java.util.*;
// Class Record definition
public class Record
{
// To store file name
static String file_name;
// main method definition
public static void main(String args[])
{
// Scanner class object for to read file
Scanner sc;
// Scanner class object for console input
Scanner input = new Scanner(System.in);
// Accept file name from the user
System.out.print(" Enter the file name: ");
file_name = input.next();
// To store number of records
int no = 0;
// try block begins
try
{
// Opens the file for reading
sc = new Scanner(new FileReader(file_name));
// Extracts number of record as first value from file
no = sc.nextInt();
// Creates a String array of size number of records to store names
String st_name[] = new String[no];
// Creates a String array of size number of records to store marks
double smarks[] = new double[no];
// Creates a String array of size number of records to store calculated marks
double smarks_new[] = new double[no];
// Reinitializes the counter to zero
no = 0;
// Loops till records available
while (sc.hasNext())
{
// Extracts name
st_name[no] = sc.next();
// Extracts mark and converts it into double
smarks[no] = Double.parseDouble(sc.next());
// Increase the counter by one
no++;
}// End of while loop
// Stores the first mark as the maximum value
double maxvalue = smarks[0];
// Loops till end of mark array
for(int i1 = 1; i1 < smarks.length; i1++)
{
// Checks if the current maximum value is less than the current mark
if(maxvalue < smarks[i1])
// Set the maximum value as the current mark
maxvalue = smarks[i1];
}// End of for loop
// Subtract 100 from maximum value
double diff11 = new Double(100) - maxvalue;
// Loops till end of mark array
for(int i1 = 0; i1 < smarks.length; i1++)
{
// Stores the current mark
double d11 = smarks[i1];
// Adds the difference value
d11 = d11 + diff11;
// Stores it as new mark
smarks_new[i1] = d11;
}// End of for loop
// Displays the heading
// minus sign for left align
System.out.printf("%-14s %10s %8s ", "Student_Name", "Score", "Curve");
// Loops till end of mark array
for(int i = 0; i < smarks.length; i++)
System.out.printf("%-17s %7.2f %9.2f ", st_name[i], smarks[i], smarks_new[i]);
}// End of try block
// Catch block to hand file not found exception
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}// End of catch block
}// End of main method
}// End of class
Sample Output:
Student_Name Score Curve
Johnson 93.50 100.00
Dirks 76.00 82.50
Timm 87.00 93.50
Faulk 55.50 62.00
Miser 69.00 75.50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.