Grundlagen der Programmierung 2 SS 2005 - Datei Notenspiegel.java
/* * Notenspiegel.java: * Anzahl der vorkommenden Noten werden als Balkendiagramm * dargestellt */ import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Graphics; import javagently.Stream; import java.io.EOFException; import java.io.IOException; import javax.swing.JFrame; class Notenspiegel extends JFrame { Notenspiegel() { // Konstruktor Container content = getContentPane(); content.setBackground(Color.magenta); getData(); setTitle("Notenspiegel"); setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void paint(Graphics g) { super.paint(g); int x = 50; int y = 250; // Schnittpunkt der Achsen int width = 20; int gap = 5; // Balken und Zwischenraum g.drawLine(x, y, x + 11 * (width + gap), y); // x-Achse g.drawLine(x, y, x, 80); // y-Achse for (int n = 0; n < 11; n++) { // Beschriftung der x-Achse g.drawString(String.valueOf(notenstufen[n]), n * (width + gap) + gap + x, y + 20); } g.drawString("Endnoten", 11 * (width + gap) + gap + x, y + 20); for (int i = 0; i < 200; i += 50) { // Beschriftung der y-Achse g.drawLine(48, y - i, 52, y - i); g.drawString(String.valueOf(i / 10), 20, y - i); } g.drawString("Anzahl", 20, 70); g.setFont(new Font("SansSerif", Font.BOLD, 14)); // Überschrift g.drawString("Klausurergebnis", 140, 60); g.setColor(Color.cyan); // die Balken for (int stufe = 0; stufe < notenstufen.length; stufe++) { int a = notenVorkommen[stufe] * 10; g.fillRect(stufe * (width + gap) + gap + x, y - a, width, a); } } int[] notenVorkommen = new int[11]; double notenstufen[] = { 1.0, 1.3, 1.7, 2.0, 2.3, 2.7, 3.0, 3.3, 3.7, 4.0, 5.0 }; void getData() { /* * Einlesen der Noten aus einer Textdatei (Endnoten.txt). Die Anzahl der * Vorkommen jeder Note wird im Array notenVorkommen gezählt: * notenVorkommen[i] gibt an, wieoft die i-te Notenstufe vorkommt. Die * Notenstufen 1.0, 1.3, ...,5.0 werden von 0 an indiziert. */ Stream eingabe = null; // Eingabedatei öffnen: try { eingabe = new Stream("Endnoten.txt", Stream.READ); } catch (IOException e) { System.out.println("Datei Endnoten.txt fehlt"); System.exit(0); } try { while (true) { // eine Note einlesen: double note = eingabe.readDouble(); if (note >= 1.0 && note <= 5.0) { // ungültige Notenwerte ignorieren int index = 0; // note auf Index der Notenstufe abbilden: while (index < notenstufen.length-1 && note > notenstufen[index]) index++; notenVorkommen[index]++; } } } catch (EOFException e) { } // Dateiende catch (IOException e) { } // falsche Zahl } public static void main(String[] args) { new Notenspiegel(); } }
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 22.04.2005