Read in grades and create histogram

Errors when trying to create this Java program

Page 1 of 1

1 Replies - 10876 Views - Last Post: 29 September 2009 - 07:56 AM Rate Topic: -----

#1 Rita1957   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 27-September 09

Read in grades and create histogram

Posted 29 September 2009 - 07:11 AM

I'm a beginner and have this homework assignment -

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

Is This A Good Question/Topic? 0
  • +

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Read in grades and create histogram

Posted 29 September 2009 - 07:56 AM

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 = null;
	   Scanner infile = null;
	   String reply = JOptionPane.showInputDialog("Input File"); //combined declaration and assignment
	   try {
		   infile = new Scanner(new File(reply));
		 }
	  catch(FileNotFoundException e){
			System.out.printf("Unable to open input file %s\n", fileName);
						System.exit(1);
					 }
						 		  
	}//end main method, without this curly brace you had an inner class and methods declared w/in a method
}//end class
		 			
public class GradeComponent extends JComponent {
				 	int aGrade = 0;
								int bGrade = 1;
		int cGrade = 2;
		int dGrade = 3;
		int fGrade = 4;
								int[] gradeCount = new int[5];
					
//I changed the return-type of this method from String to char because you only need to increment
//coutners, and return the character letter grade  Also, in methods with non-void return-types, you can   
//only return 1 value. If you want values to be returned based on conditions, place a return statement in each 
//conditional and a default return outside of all the conditionals.
//Also, where are you going to be interpreting these characters for the letter grade?
//If you aren't going to be parsing these characters, then set the return type of this method to void	

								public char letterGrade(int score){ 
								  //it is bad practice if not illegal to reassign the value of a parameter
		   // score = infile.nextInt();, commented out
			if (score >= 93){
											gradeCount[aGrade]++;
											return 'a';}
			else if ( score >= 84 ){ 
										gradeCount[bGrade]++;
										return 'b';
										 }
									else if ( score >= 75 ){ 
										 gradeCount[cGrade]++;
										 return 'c';
										}
			else if ( score >= 66 ){ 
										 gradeCount[dGrade]++;
										 return d;
										   }
			gradeCount[fGrade]++; //If all the other conditionals evaluate to false
									return 'f'; //then increment the fGrade counter and return 'f'
			}//end method
		   			
				   for(int i = 0; i < str.length; i++) {
	   		 System.out.println(str[i]);
	   	}
}//end class			   	 

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);
						 
}//end class			 
					
						}  
							 
						 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);  
  }
}




I've gone through and cleaned up some of your code, but I would scrap it and start over. Let's go ahead and take a high-level view of some classes you might want to use as well as some concepts.

To begin, let's look at some basic File I/O using the Scanner class:
/*
  This section reads in a file and prints out each token on a new line on the console
*/
Scanner scan = null;
File f = null;
String pathname = "somepathname.txt"; //replace the text inside the double quotes with the actual classname
try{
   f = new File(pathname);
   scan = new Scanner(f);
}
catch(Exception e){
	System.exit(0);
}

while(scan.hasNext()){
   System.out.println(scan.next());
}



The next class I would use is a subclass of JPanel (create your own). It should recieve the following information:
-Number of elements
-Quantity of each letter grade (number of a's, number of b's, etc.)
Next, it should have 5 bars of equal width, but varying height. You need to equate the number of pixels with the number of elements in each category. In order to do this, override the paintComponent() and use the Graphics class.

-Use a subclass of JFrame to hold your subclass of JPanel
-Instantiate your subclass of JPanel in here
-Add it to the frame
-Set the frame to visible

If you have any more questions on OO design, feel free to post. Hope this helps some.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1