The Artima Developer Community
Sponsored Link

Java Answers Forum
Need help with Java program

3 replies on 1 page. Most recent reply: May 4, 2006 9:36 AM by Jake Horvath

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Jake Horvath

Posts: 3
Nickname: horvath
Registered: May, 2006

Need help with Java program Posted: May 3, 2006 5:46 PM
Reply to this message Reply
Advertisement
I was wondering if someone could help me with this part of a Java program as soon as possible. Thanks.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class NameFrame extends JFrame implements ActionListener
{
/** Programmer: J. McTaggart
Additional implementation:
Assignment: Laboratory Assignment #5
Due Date: 05.04.06
Purpose: defines main container for graph, including user interface;
reads data file; defines searching routines
*/

private NameComponent graphPanel; // main plotting area
private JTextField txtName; // GUI components
private JButton btnGraph, btnClearAll, btnClearOne, btnSearch;
private JPanel pnlInterface; // panel for organization
private Container fr; // main container
private ArrayList<NameRecord> nameList; // holds plotted names

// constructor method
public NameFrame()
{
Container fr; // prepare container
fr = getContentPane();
setTitle("NameSurfer");

// interface - TO BE COMPLETED

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

private void read(String s)
{
nameList = new ArrayList<NameRecord>(); // instantiate ArrayList
String s1; // input string
NameRecord inRec; // NameRecord created from input string

try // try to read data file
{
Scanner scan = new Scanner(new File(s)); // set up Scanner
while (scan.hasNext()) // while there is data
{
s1 = scan.nextLine(); // read the line of data
inRec = new NameRecord(s1); // create NameRecord
nameList.add(inRec); // and add to list
}
}
catch (IOException exp) // handle any file I/O problems
{
System.out.println("File processing error.");
System.exit(0);
}
}

public void search(String s)
{
// search for substring s in all names - TO BE IMPLEMENTED
}

public NameRecord findName(String s)
{
// search for name s in all names - TO BE IMPLEMENTED
return null; // name not found - TO BE MODIFIED
}

public void actionPerformed(ActionEvent event)
{
// handles interface events - TO BE IMPLEMENTED
}

public static void main(String args[])
{
NameFrame nameframe = new NameFrame(); // instantiate frame
nameframe.read("names-data.txt"); // read data file
}
}


Jake Horvath

Posts: 3
Nickname: horvath
Registered: May, 2006

Re: Need help with Java program Posted: May 3, 2006 5:48 PM
Reply to this message Reply
Here are the other two classes that are almost fully complete except for a few minor parts.

import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;


public class NameComponent extends JComponent
{
Purpose: defines the "graph", painting grid lines and
plot for each searched and found name
*/

private int grSize = 600; // initial plot dimensions
// array of colors for plot lines
private Color[] lineClrs = {Color.black, Color.red, Color.blue, Color.green};
private ArrayList<NameRecord> plotVar; // list of currently plotted names
private final int DECADES = 11; // number of decades with data
private final int GAP = 20; // spacing gap in pixels
private final int maxPlotY = 1000; // largest rank plot limit

// constructor
public NameComponent()
{
setPreferredSize(new Dimension(grSize, grSize)); // set JComponent size
plotVar = new ArrayList<NameRecord>(); // instantiate list for plotted names
}

// adds name to be plotted to list
public void addName(NameRecord nameToPlot)
{
plotVar.add(nameToPlot);
repaint(); // call paintComponent
}

// clears all plotted names
public void clearAll()
{
plotVar.clear(); // remove all elements of list
repaint(); // call paintComponent
}

// clears earliest plotted name
public void clearOne()
{
if (plotVar.size() > 0) // checks for existence of a name plot
{
plotVar.remove(0); // remove 0th name plot
repaint(); // call paintComponent
}
}

// paintComponent
public void paintComponent(Graphics g)
{
int xMax = getWidth(); // get container size
int yMax = getHeight();

g.drawLine(0, GAP, xMax, GAP); // draw top and bottom lines GAP pixels "in"
g.drawLine(0, yMax - GAP, xMax, yMax - GAP);

for (int k=0; k<DECADES; k++) // draw and label vertical decade lines
{
int i1 = (xMax * k) / DECADES;
g.drawLine(i1, 0, i1, yMax);
g.drawString("" + (1900 + k * 10), i1 + 2, yMax - 2);
}

for(int i=0; i<plotVar.size(); i++)
{
g.setColor(lineClrs[i % lineClrs.length]);
drawLines(g, plotVar.get(i));
}
}

// draws lines to connect decades
public void drawLines(Graphics g, NameRecord nameToPlot)
{
int xMax = getWidth(); // get container dimensions
int yMax = getHeight();

// draw line graph for name ranks - TO BE IMPLEMENTED
}
}






public class NameRecord
{
/**
Purpose: class definition for one line of data: name and 11 ranks
*/

private String aName; // name from data record
private int[] ranks; // array of ranks by decade
private final int MAX_RANK = 1500; // assumed maximum rank
private final int START = 1900; // starting year for rank data
private final int DECADES = 11; // number of decades of rank data

// constructor
public NameRecord(String s)
{
String strArray[] = s.split(" "); // create array of strings by
// splitting input string at each blank
aName = strArray[0]; // store name from 0th element of strArray
ranks = new int[strArray.length-1]; // create array for ranks
for (int i=1; i<strArray.length; i++) // store each rank from 1st position forward
{ // of strArray
ranks[i-1] = Integer.parseInt(strArray[i]);
}
}

// returns name from data record
public String getName()
{
return aName;
}

// return rank stored in ith position
public int getRank(int i)
{
return ranks[i];
}

// determine and return year with best rank
public int bestYear()
{
int year;

return 1900; // return "year" of best rank - TO BE MODIFIED
}
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Need help with Java program Posted: May 3, 2006 10:52 PM
Reply to this message Reply
1. Don't use the "code" tag, but the "java" tag when formatting your post. It's really annoing reading code without insets. That's why I don't do it at all.

2. It's nice of you to share your homework with us, but you could at leas try to do it yourself.

3. If you want help, ask a quesstion.
Do you need hep with the algorithm or with some syntax?

Jake Horvath

Posts: 3
Nickname: horvath
Registered: May, 2006

Re: Need help with Java program Posted: May 4, 2006 9:36 AM
Reply to this message Reply
I managed to figure it out, thanks anyway though.

Flat View: This topic has 3 replies on 1 page
Topic: obgects in methods Previous Topic   Next Topic Topic: unsupported class version error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use