1. Read numeric grades from a file keyed in
2. Count number of letter grades A's, B's, etc. depending on numeric grades
3. Display a histogram of the letter grade distribution
I know I'm using the curly braces incorrectly, but I can't seem to find out what the correct usage is. Also the way I'm grouping an defining the classes is a problem.
This is the program I've written. The graph section is incomplete (the part beginning with "public void paintComponent(Graphics g").
import java.util.Scanner;
import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import javax.swing.JComponent;
import java.awt.Graphics2D;
public class GradeViewer
{
public static void main(String[] args)
{
String fileName;
Scanner input = new Scanner(System.in);
Scanner infile = null;
String reply;
reply = JOptionPane.showInputDialog("Input File");
fileName = input.next(reply);
// Try to create a Scanner for the file name entered
try
{
infile = new Scanner(new File(fileName));
}
catch(FileNotFoundException e)
{
System.out.printf("Unable to open input file %s\n", fileName);
System.exit(1);
}
public class GradeComponent extends JComponent
{
int aGrade = 0;
int bGrade = 0;
int cGrade = 0;
int dGrade = 0;
int fGrade = 0;
public String letterGrade(int score)
{
score = infile.nextInt();
if ( score >= 93 )
{
aGrade++;
}
else if ( score >= 84 )
{
bGrade++;
}
else if ( score >= 75 )
{
cGrade++;
}
else if ( score >= 66 )
{
dGrade++;
}
else
{
fGrade++;
}
return aGrade;
return bGrade;
return cGrade;
return dGrade;
return fGrade;
}
{
String[]count = new String[5];
count[0] = letterGrade(aGrade);
count[1] = letterGrade(bGrade);
count[2] = letterGrade(cGrade);
count[3] = letterGrade(dGrade);
count[4] = letterGrade(fGrade);
// str.length is 5
for(int i = 0; i < str.length; i++)
{
System.out.println(str[i]);
}
}
public class RulerViewer
{
/**
* Displays a ruler with specified number of tick marks.
*/ {
JFrame f = new JFrame();
GradeComponent rc = new GradeComponent(6);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setLocation(300,100);
f.add(rc);
f.setVisible(true);
}
}
public void paintComponent(Graphics g)
{
int margin = 20;
int h = getHeight() - 2*margin; // not used in this program
int w = getWidth() - 2*margin;
// g can be cast to type Graphics2D to allow thick lines
// BasicStroke determines how thick; 4.0F means float value
Graphics2D g2 = (Graphics2D) g;
BasicStroke s4 = new BasicStroke(4.0F);
// Adjust w to be a multiple of len
if ( (w % len) != 0 )
{
w = w - w % len;
}
// draw the caption string at the top of the frame (in my margin)
String caption = "Grade Distribution " + len;
g2.drawString(caption, margin, margin);
g2.drawString(caption, margin + 1, margin);
// set the line width and color (red) and distance from the top of the frame
// then draw the line
g2.setStroke(s4);
g2.setColor(Color.red);
int y = 2*margin; // distance of line from top of frame
g2.drawLine(margin, y, margin + w, y );
// Draw tick marks (black)
// First determine the horizontal space between tic marks
int hunit = w/len;
int tickheight = 10;
g2.setColor(Color.black);
for(int i = 0; i < len + 1; i++)
{
int x = margin + i*hunit;
g2.drawLine(x, y, x, y - tickheight);
}
}
}
System.exit(0);
}
}
These are the errors I'm getting from Eclispe -
Description Resource Path Location Type
Type mismatch: cannot convert from int to String GradeViewer.java /GradeViewer/src line 73 Java Problem
Type mismatch: cannot convert from int to String GradeViewer.java /GradeViewer/src line 74 Java Problem
Type mismatch: cannot convert from int to String GradeViewer.java /GradeViewer/src line 75 Java Problem
Type mismatch: cannot convert from int to String GradeViewer.java /GradeViewer/src line 76 Java Problem
The constructor GradeComponent(int) is undefined GradeViewer.java /GradeViewer/src line 103 Java Problem
str cannot be resolved GradeViewer.java /GradeViewer/src line 91 Java Problem
str cannot be resolved GradeViewer.java /GradeViewer/src line 93 Java Problem
len cannot be resolved GradeViewer.java /GradeViewer/src line 126 Java Problem
len cannot be resolved GradeViewer.java /GradeViewer/src line 128 Java Problem
len cannot be resolved GradeViewer.java /GradeViewer/src line 132 Java Problem
len cannot be resolved GradeViewer.java /GradeViewer/src line 145 Java Problem
len cannot be resolved GradeViewer.java /GradeViewer/src line 148 Java Problem
Illegal modifier for the local class RulerViewer; only abstract or final is permitted GradeViewer.java /GradeViewer/src line 97 Java Problem
Illegal modifier for the local class GradeComponent; only abstract or final is permitted GradeViewer.java /GradeViewer/src line 39 Java Problem
Color cannot be resolved GradeViewer.java /GradeViewer/src line 139 Java Problem
Color cannot be resolved GradeViewer.java /GradeViewer/src line 147 Java Problem
Cannot refer to a non-final variable infile inside an inner class defined in a different method GradeViewer.java /GradeViewer/src line 49 Java Problem
BasicStroke cannot be resolved to a type GradeViewer.java /GradeViewer/src line 123 Java Problem
BasicStroke cannot be resolved to a type GradeViewer.java /GradeViewer/src line 123 Java Problem
Type mismatch: cannot convert from int to String GradeViewer.java /GradeViewer/src line 72 Java Problem
The import java.util.Scanner is never used Pair.java /Pair/src line 1 Java Problem
The serializable class GradeComponent does not declare a static final serialVersionUID field of type long GradeViewer.java /GradeViewer/src line 39 Java Problem
I don't ex[ect to get the answers to all the errors, but any assistance would be greatly appreciated.
Thank you,
Rita

New Topic/Question
Reply



MultiQuote





|