Problem Statement

Write a program that will calculate weighted grades for students in a college course. The input will be given in a comma-delimited format. The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

Each subsequent line of input will contain information for a student. The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas. Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

It is guaranteed that at least two lines of input will be provided, the first containing the weights and at least one additional line containing data for a student. In addition, it is guaranteed that each line of input will contain the same number of parts.

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

Yikes!

Let's break it down into smaller parts

// Load required classes
import java.util.Scanner;
import java.io.File;

public class Example{

public static void main(String[] args) throws Exception{ // Scanner variable Scanner reader;

// If an argument is present, read from a file
// specified in args[0]
if(args.length > 0){
  reader = new Scanner(new File(args[0]));
// If no argument, read from System.in
}else{
  reader = new Scanner(System.in);
}

/* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */

} }

Skeleton


The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.


The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 0; i < weights.length; i++){ weights[i] = weightParts[i]; }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 0; i < weights.length; i++){ weights[i] = weightParts[i]; }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 0; i < weights.length; i++){ weights[i] = Double.parseDouble(weightParts[i]); }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 0; i < weights.length; i++){ weights[i] = Double.parseDouble(weightParts[i]); }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 0; i < weights.length; i++){ weights[i] = Double.parseDouble(weightParts[i]); }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 1; i < weights.length; i++){ weights[i] = Double.parseDouble(weightParts[i]); }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 1; i < weights.length; i++){ weights[i] = Double.parseDouble(weightParts[i]); }

The first line will contain a number of weights as floating-point numbers, separated by commas. The first entry should be ignored.


Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.


Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){

}

Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){

}

Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){
  String line = reader.nextLine();

// parse the input }

Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){
  String line = reader.nextLine();

// parse the input }

Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

// parse the input }

Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

// parse the input }

Each subsequent line of input will contain information for a student. ... Input will be terminated by the end of the input file, or by a blank line when input is provided via the terminal.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

// parse the input }

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

// parse the input }

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(","); }

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(","); }

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ parts[j]; }

}

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ parts[j]; }

}

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ Integer.parseInt(parts[j]); }

}

The first entry on that line will contain that student’s name. The rest of the line will contain that student’s scores on each assignment as an integer value, separated by commas.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ Integer.parseInt(parts[j]); }

}

Write a program that will calculate weighted grades for students in a college course.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ Integer.parseInt(parts[j]); }

}

Write a program that will calculate weighted grades for students in a college course.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ Integer.parseInt(parts[j]); }

}

Write a program that will calculate weighted grades for students in a college course.

Total =
Sum(Score[i] * Weight[i])

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

for(int j = 1; j < parts.length; j++){ weights[j] * Integer.parseInt(parts[j]); }

}

Write a program that will calculate weighted grades for students in a college course.

Total =
Sum(Score[i] * Weight[i])

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

}

Write a program that will calculate weighted grades for students in a college course.

Total =
Sum(Score[i] * Weight[i])

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

System.out.println();

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…();

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…();

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format());

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format());

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format("%s: ", parts[0]));

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format("%s: “, parts[0]));

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(”,");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format("%s: %f", parts[0], totalScore));

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format("%s: %f", parts[0], totalScore));

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format("%s: %5.2f", parts[0], totalScore));

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

while(reader.hasNext()){
  String line = reader.nextLine();
  if(line.length() == 0){
    break;
  }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

…(String.format("%s: %5.2f", parts[0], totalScore));

}

The program should output the student’s name, followed by a colon, and a space, and then the student’s score. The score should be formatted to be exactly 5 characters wide, with exactly two characters after the decimal point.

String weightLine = reader.nextLine();
String[] weightParts = weightLine.split(",");

double[] weights = new double[weightParts.length];

for(int i = 1; i < weights.length; i++){ weights[i] = Double.parseDouble(weightParts[i]); }

while(reader.hasNext()){ String line = reader.nextLine(); if(line.length() == 0){ break; }

String[] parts = line.split(",");

double totalScore = 0.0; for(int j = 1; j < parts.length; j++){ totalScore += weights[j] * Integer.parseInt(parts[j]); }

System.out.println(String.format("%s: %5.2f", parts[0], totalScore));

}

"/js/highlight.pack.js"