Program, help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 6
Reputation: vick is an unknown quantity at this point 
Solved Threads: 0
vick vick is offline Offline
Newbie Poster

Program, help

 
0
  #1
Oct 2nd, 2006
Hi how's everyone doing, please help me with this program, i am doing this from last 4 to 5 hours, it's over my head, please help.

here's the what program suppose to do:

Given an input data file containing dates, one date per line, write a Java application Project.java which
will read lines of text from data file and put them into a partially-filled array of strings, and will then print
the contents of the array to a series of three JOptionPane message dialog boxes with text areas. The
first message box will display a column of the dates in the original order in which they were listed in the
file. The second message box will display a column of the dates sorted in lexicographical order, as
strings. The third message box will display a column of the dates in ascending order by date (earliest
date first).

  1. import javax.swing.*;
  2. public class pro1111
  3. {
  4. public static void main (String [] args)
  5. {
  6. TextFileInput in = new TextFileInput("Dates.txt");
  7. TextFileOutput out = new TextFileOutput("Dates1.txt");
  8. String line = in.readLine();
  9. while ( line != null ) {
  10. StringTokenizer st = new StringTokenizer(line, "/");
  11. int monthNumber = Integer.parseInt(st.nextToken());
  12. int day = Integer.parseInt(st.nextToken());
  13. int year = Integer.parseInt(st.nextToken());
  14. out.println(monthNumberToName(monthNumber)
  15. + " " + day + ", " + year);
  16. line = in.readLine();
  17. }
  18. }
  19.  
  20. private static compareDate(string date1, string date2)
  21. {
  22. String date1 = "09/27/2004";
  23. String date2 = "05/04/2002";
  24. if (compareDate(date1, date2) < 0)
  25. System.out.println("date1 is earlier than date2.");
  26. return 0;
  27. }
  28. private static void display(short[] numbers,
  29. int lengthFilled)
  30. {
  31. final String lineBreak = System.getProperty("line.separator");
  32. // Create text area for output:
  33. JTextArea textArea = new JTextArea();
  34. textArea.setEditable(false);
  35. // Print numbers to output text area:
  36. for ( int i = 0; i < lengthFilled; i++ )
  37. textArea.append(numbers[i] + lineBreak);
  38. // Display the output text area via a message dialog box:
  39. JOptionPane.showMessageDialog(null, textArea);
  40. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,464
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Program, help

 
0
  #2
Oct 2nd, 2006
You need to seriously inspect your design. The compareDate method is not called anywhere, except from within itself, and then only with two date Strings (a date String is not a Date, just so you know) that are also defined within the method. Were this method ever called from anywhere else, it would lead to an infinite recursion error.

You need to go back to paper and chart out exactly how the program should operate, and then translate that plan of operations to code.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: vick is an unknown quantity at this point 
Solved Threads: 0
vick vick is offline Offline
Newbie Poster

Re: Program, help

 
0
  #3
Oct 2nd, 2006
ok here what i was suppose to do

Program should contain the following methods:

public static void main (String args[])
the main method for this program, it should call the
methods below as appropriate.

public static int readFile (String filename, String[] dates)
Reads from the file whose filename is given as a parameter, and fills array of strings
represending dates. Lines of the file that do not have proper date format (MM-DD-YYYY or
MM/DD/YYYY) or which represent invalid dates (such as 13/32/2006) should be rejected by
printing an error message to the console and not including them in the array. The test for validity
should be done by your isValidDate method below. The returned value is the number of dates
successfully put in the array.

public static void sortLines (String[] lines, int length)
Sorts the array of strings lexicographically using selection sort.

public static void sortDates (String[] dates, int length)
Sorts the array in date order using selection sort. In order to sort the date strings in order by
date, you will need to write an additional method called compareDate that will behave similarly
to compareTo method of the String class, except that it will sort by date rather than
lexicographically. To compare dates date, you will need to split the string into parts representing
the month, day, and year.

public static int compareDate(String date1, String date2)
compares two dates. Returns a negative number if date1 precedes date2, 0 if date1 is the
same as date2, or a positive number if date1 succeeds date2. This method is called from
within sortDate, which date strings by date.

public static void displayResults(String[] students, int length)
Prints the contents of the array to a JOptionPane.

public static boolean isValidDate(String date)
Returns true if and only if date has the proper format (MM-DD-YYYY or MM/DD/YYY) and has
appropriate values for the month (1 to 12) and day. Valid days may range from 1 to 31 in the
months of January, March, May, July, August, October, and December, and may range from 1
to 30 for the months of April, June, September, and November. In February, days may range
from 1 to 29 in a leap year, 1 to 28 in other years. Call your isLeapYear method, described
below, to determine whether a year is a leap year.

public static boolean isLeapYear(int year)
Returns true if and only if year is a leap year. A year is a leap year if it is a multiple of 400 or if
it is a multiple of 4 but not a multiple of 100.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,464
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Program, help

 
0
  #4
Oct 2nd, 2006
You misunderstood what I said. I am not going to do it for you, and posting the assignment text won't help. Take this text and a piece of paper. For each of the methods listed draw a little box on the paper and label it as the method and write a word or two to describe its purpose. Then begin drawing lines between these boxes with some text explaining the relationship to describe how these methods are to work with each other. Then take this information and begin coding your methods (remembering to call the write methods in the write order from the write places) as per your drawn up action/design plan.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: vick is an unknown quantity at this point 
Solved Threads: 0
vick vick is offline Offline
Newbie Poster

Re: Program, help

 
0
  #5
Oct 2nd, 2006
look what i understood i wrote code, i know it's wrong, so take my program (you don't need to do it for me), and put pointers, explation what i am doing wrong, what i need to add , and what need to removie. that's what i am asking, so look at the code and help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,464
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Program, help

 
1
  #6
Oct 2nd, 2006
  1. /*
  2.  * You should give your class a package as it is truely frowned upon these days
  3.  * to place your classes in the "main" namespace.
  4.  */
  5.  
  6. import javax.swing.*;
  7.  
  8. public class pro1111 {
  9.  
  10. public static void main (String [] args) {
  11. /*
  12.   * I assume you have your own TextFileInput and TextFileOutput
  13.   * classes as these are no part of the JDK. And if so, you have not
  14.   * imported them.
  15.   */
  16. TextFileInput in = new TextFileInput("Dates.txt");
  17. TextFileOutput out = new TextFileOutput("Dates1.txt");
  18. String line = in.readLine(); // I also assume these classes handle the exceptions
  19. while ( line != null ) {
  20. // StringTokenizer is a bit old fashioned. You should probably look into
  21. // Scanner and or String.split(). But what you really want is to simply read
  22. // the entire String and not break it down into pieces.
  23. StringTokenizer st = new StringTokenizer(line, "/");
  24. int monthNumber = Integer.parseInt(st.nextToken());
  25. int day = Integer.parseInt(st.nextToken());
  26. int year = Integer.parseInt(st.nextToken());
  27. out.println(monthNumberToName(monthNumber) + " " + day + ", " + year);
  28. // You should also be storing the dates here rather than simply printing them
  29. // out. Probably with an ArrayList. Then, after you have them read in and
  30. // stored somewhere, you can start using another loop or two to compare them
  31. // and get them into the proper order.
  32. line = in.readLine();
  33. }
  34. }
  35.  
  36. private static compareDate(string date1, string date2) {
  37. // Why do you designate two dates (in String format and it should be String not
  38. // string) and then define two new date Strings. You also have not defined a
  39. // return type.
  40. String date1 = "09/27/2004";
  41. String date2 = "05/04/2002";
  42. // calling compareDate here is recursion. Is this really what you want to do?
  43. // I don't think so. Also, doing it when the method defines it own date Strings
  44. // guarntees infinite recursion.
  45. if (compareDate(date1, date2) < 0)
  46. System.out.println("date1 is earlier than date2.");
  47. return 0;
  48. }
  49.  
  50. private static void display(short[] numbers, int lengthFilled) {
  51. // Why are the declared arguments here a short array and an int when according
  52. // to your assignment it should be a String array (and suppossedly of student
  53. // names which I don't understand at all since the entire has been concerning
  54. // dates up until this point. I think you copied that wrong.) and an int.
  55. // Other than that, this method actually looks like it might do something for
  56. // you. Where did you copy it from?
  57. final String lineBreak = System.getProperty("line.separator");
  58. // Create text area for output:
  59. JTextArea textArea = new JTextArea();
  60. textArea.setEditable(false);
  61. // Print numbers to output text area:
  62. for ( int i = 0; i < lengthFilled; i++ )
  63. textArea.append(numbers[i] + lineBreak);
  64. // Display the output text area via a message dialog box:
  65. JOptionPane.showMessageDialog(null, textArea);
  66. }
  67.  
  68. /*
  69.   * Where are all the rest of the listed methods? Should they generate
  70.   * themselves? Really, you could at least put a few skeleton methods for
  71.   * them in here so that it at least looks like you tried. You also have not
  72.   * placed a closing brace "}" for your class.
  73.   */
Find the remarks in the "code" (and I am using that term very generousy) above. Really, even if it is your first day (which it is not with this type of assignment) you could put in a little more effort.

You must throw this code away and start with what I suggested in my last post. Maybe that way you will gain at least a little insight into what it is like to actually be a developer (or at least of the development process).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: vick is an unknown quantity at this point 
Solved Threads: 0
vick vick is offline Offline
Newbie Poster

Re: Program, help

 
0
  #7
Oct 3rd, 2006
ok, thank u for the help, i am working on it as soon as i am done, i will post it here
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: vick is an unknown quantity at this point 
Solved Threads: 0
vick vick is offline Offline
Newbie Poster

Re: Program, help

 
0
  #8
Oct 3rd, 2006
masijade, man you got attuide son ,you know how to write a code and all stuff, remebmer one day you were on my postion. Son there's other community board on the net where i can get help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,464
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Program, help

 
0
  #9
Oct 3rd, 2006
It's not me that has attitude. I have told you what you needed to do a few posts ago, and that is to start with paper and try to design a flowchart/schematic for this program, and then start with the coding. But you insisted that I look at your code and place pointers in there, well I did. It is not my fault that the only method that is even anywhere remotely usable is the display method. It is also not my fault that the display method is obviously copied from some other place since all of sudden it is taking about studentNames instead of dates and uses the wrong type of array. If you don't like the code review that you got, then maybe you should put a little effort into writing the code next time. You say you spent four five hours, but seemingly you did not even try to compile those parts of it that you had or many of the problems that were pointed out would have been fixed. You would have imported the classes that needed to be imported, and the method parameters would at least have had the right types. I was simply being honest in my code review. Attempting to either "sugar-coat" the review, or do the work for you (which I am sure was what you actually wanted) would not help you, it would hurt you. The second might have let you pass this assignment, but then you would be even more lost than you are now (as hard as that is to believe). Why don't you ask your questions on geekinterview.com? They seem to have the level of expertise (and intelligence) that you want.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 6
Reputation: vick is an unknown quantity at this point 
Solved Threads: 0
vick vick is offline Offline
Newbie Poster

Re: Program, help

 
-1
  #10
Oct 4th, 2006
masijade: if you an't gonna help stop posting in my thread. don't give me no explation, stop posting in my thread.
Last edited by vick; Oct 4th, 2006 at 4:41 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC