Computing a Person’s Age and Students’ Grades
PlanetAge.java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
* Name:
* Date:
* Description: console application for calculating age on other planets
*/
public class PlanetAge {
// initializing dictionary for storing length of planet year
private static final Map planetD = new HashMap<>();
static {
// populating dictionary with data for each planet
planetD.put("Mercury", 88);
planetD.put("Venus", 225);
planetD.put("Jupiter", 4380);
planetD.put("Saturn", 10767);
}
public static void main(String[] args) {
// creating scanner to read from the console
try (Scanner scanner = new Scanner(System.in)) {
// asking user to enter the age
System.out.print("Please, enter your Earth age: ");
// reading user response
int age = Integer.parseInt(scanner.nextLine().trim());
// iterating over planets (records in the dictionary) to output information for each planet
for (Map.Entry pair : planetD.entrySet()) {
// calculating age on the current planet
int planetAge = (age * 365) / pair.getValue();
// outputting result
System.out.println("Your age on " + pair.getKey() + " is " + planetAge);
}
}
}
}
Exam.java
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
/*
* Name:
* Date:
* Description: console application for calculating age on other planets
*/
public class Exam {
public static void main(String[] args) {
// a java collection to store read data
List scores = new ArrayList<>();
// creating scanner to read from scores file
try (Scanner scanner = new Scanner(new File("scores.txt"))) {
// reading all lines in the file
while (scanner.hasNextLine()) {
// scanning and trimming new line
String line = scanner.nextLine().trim();
// skip empty lines
if (line.isEmpty()) {
continue;
}
// splitting line with commas
String[] parts = line.split(",");
// creating student name from second and third part
String name = parts[1].trim() + " " + parts[2].trim();
// parsing double score from a first comma-separated file
double score = Double.parseDouble(parts[0].trim());
// calculating student's grade
char grade;
if (score >= 90) {
grade = 'A';
}
else if (score >= 80) {
grade = 'B';
}
else if (score >= 70) {
grade = 'C';
}
else if (score >= 60) {
grade = 'D';
}
else {
grade = 'F';
}
// adding read score to collection
scores.add(new StudentScore(name, score, grade));
}
} catch (IOException e) {
// interrupt application in case file read error
throw new RuntimeException(e);
}
// creating writer to write data to report file
try (PrintWriter pw = new PrintWriter(new File("report.txt"))) {
// variable for cumulating all-students total score
double totalScore = 0.0;
// array, storing several students for each grade
int[] grades = new int[6];
// constructing output with a string builder
StringBuilder builder = new StringBuilder();
// adding title
builder.append(String.format("%55s", "Exam Results Report")).append(System.lineSeparator());
builder.append(System.lineSeparator());
// adding headers
builder.append("Student Name Letter Grade Score").append(System.lineSeparator());
builder.append("=========================== ============ =====").append(System.lineSeparator());
// iterating over a collection of student data and output it
for (StudentScore studentScore : scores) {
builder.append(String.format("%-37s%c%20.1f",studentScore.name,studentScore.grade,studentScore.score));
builder.append(System.lineSeparator());
// adding current student score to total score
totalScore += studentScore.score;
// incrementing correspondent grade counter
grades[studentScore.grade - 'A']++;
}
// outputting number of exams
builder.append(System.lineSeparator()).append(System.lineSeparator());
builder.append("Total number of Exams: ").append(scores.size()).append(System.lineSeparator());
builder.append(System.lineSeparator()).append(System.lineSeparator());
// outputting grades counters
for (int i = 0; i<6; i++) {
if (i == 4) {
continue;
}
builder.append("Total Number of ").append((char)('A'+i)).append("s: ").append(grades[i]).append(System.lineSeparator());
}
// outputting average exam score
builder.append(System.lineSeparator()).append(System.lineSeparator());
builder.append("Average Exam Score: ").append(String.format("%.2f", (totalScore/scores.size()))).append(System.lineSeparator());
pw.write(builder.toString());
}
catch (IOException e) {
// interrupt application in case file open error
throw new RuntimeException(e);
}
}
private static class StudentScore {
final String name;
final double score;
final char grade;
public StudentScore(String name, double score, char grade) {
this.name = name;
this.score = score;
this.grade = grade;
}
}
}