By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,799 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,799 IT Pros & Developers. It's quick & easy.

hello need help to correct my code

P: 8
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; // program uses class Scanner
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4.  import javax.swing.JPanel;
  5.  
  6.  public class person {
  7.  
  8.  
  9. private String firstName;
  10. private String lastName;
  11. private String middleInitial;
  12. private String idNumber;
  13.  
  14.  
  15. public void personAttributes( String first, String last,String middle, String id )
  16. {
  17.  
  18. firstName = first;
  19. middleInitial= middle;
  20. lastName = last;
  21. idNumber = id;
  22.  
  23.  
  24.  
  25. // set first name
  26.  public void setFirstName( String first )
  27.  {
  28.  firstName = first; // should validate
  29.  } // end method setFirstName
  30.  
  31.  // return first name
  32.  public String getFirstName()
  33.  {
  34.  return firstName;
  35.  } // end method getFirstName
  36.  
  37.  // set last name
  38.  public void setLastName( String last )
  39.  {
  40.  lastName = last; // should validate
  41.  } // end method setLastName
  42.  
  43.  // return last name
  44.  public String getLastName()
  45.  {
  46.  
  47. return lastName;
  48.  } // end method getLastName
  49.  
  50.  // set social security number
  51.  public void setIDNumber( String id )
  52.  {
  53.      idNumber = id; // should validate
  54.  } // end method setSocialSecurityNumber
  55.  
  56.  // return social security number
  57.  public String getIDNumber()
  58.  {
  59.  return IDNumber;
  60.  } // end method getSocialSecurityNumber
  61. public class personAttributesTest
  62.  {
  63.  public void main( String[] args )
  64.  {
  65.  
  66.  
  67.  
  68.  
  69.  // get  Person Attributes data
  70.  System.out.println(
  71.  "Person information obtained by get methods: \n" );
  72.  System.out.printf( "%s %s\n", "First name is" );
  73.  System.out.printf( "%s %s\n", "Last name is");
  74.  System.out.printf( "%s %s\n", "Middle Initial is");
  75.  System.out.printf( "%s %s\n", "ID number is" );
  76.  
  77.  
  78.  } // end main
  79.  } // end class personAttributesTest
  80.  
  81. // create Scanner to obtain input from command window
  82. Scanner input = new Scanner( System.in );
  83. // Date class declaration.
  84.  
  85. public class Date
  86.  {
  87.  private int month; // 1-12
  88.  private int day; // 1-31 based on month
  89.  private int year; // any year
  90.  
  91. private static final int[] daysPerMonth = // days in each month
  92.  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  93.  
  94. // constructor: call checkMonth to confirm proper value for month;
  95.  // call checkDay to confirm proper value for day
  96.  public Date( int theMonth, int theDay, int theYear )
  97.  {
  98.  month = checkMonth( theMonth ); // validate month
  99.  year = theYear; // could validate year
  100.  day = checkDay( theDay ); // validate day
  101.  
  102.  System.out.printf(
  103.  "Date object constructor for date %s\n", this );
  104.  } // end Date constructor
  105.  
  106.  // utility method to confirm proper month value
  107.  private int checkMonth( int testMonth )
  108.  {
  109.  if ( testMonth > 0 && testMonth <= 12 ) // validate month
  110.  return testMonth;
  111.  
  112. else // month is invalid
  113.  throw new IllegalArgumentException( "month must be 1-12" );
  114.  } // end method checkMonth
  115.  
  116.  // utility method to confirm proper day value based on month and year
  117.  private int checkDay( int testDay )
  118.  {
  119.  // check if day in range for month
  120.  if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
  121.  return testDay;
  122.  
  123.  // check for leap year
  124.  if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
  125.  ( year % 4 == 0 && year % 100 != 0 ) ) )
  126.  return testDay;
  127.  
  128.  throw new IllegalArgumentException(
  129.  "day out-of-range for the specified month and year" );
  130.  } // end method checkDay
  131.  
  132.  // return a String of the form month/day/year
  133.  public String toString()
  134.  {
  135.  return String.format( "%d/%d/%d", month, day, year );
  136.  } // end method toString
  137.  } // end class Date
Oct 17 '13 #1
Share this Question
Share on Google+
6 Replies


P: 8
the question was:
designe a java class of person with the following
attributes: person should have:
1.Names
a. First name
b.Last name
c. Middle initial
d.ID number

2. Date of birth
a.Year of birth
b.Month of birth
c.Day of the month
d. Day of the week
Oct 17 '13 #2

Expert 100+
P: 785
That is not a question, that is your assignment description.
So what is your question or problem with the code above?
Oct 17 '13 #3

P: 8
im getting errors i dont know why?
Oct 17 '13 #4

Expert 100+
P: 785
Can you please post the error codes/messages here? Guessing them makes no sense.

You are using scanner class. So we need to know what you povide as input, too. I mean the exact string that you have typed in.
Oct 17 '13 #5

P: 8
Description Resource Path Location Type
IDNumber cannot be resolved to a variable person.java /helloe/src line 61 Java Problem
Return type for the method is missing hi.java /helloe/src line 14 Java Problem
smileButton cannot be resolved to a type hi.java /helloe/src line 14 Java Problem
Syntax error on token ")", { expected after this token hi.java /helloe/src line 14 Java Problem
Syntax error on token "smileButton", VariableDeclaratorId expected after this token hi.java /helloe/src line 14 Java Problem
Syntax error, insert "}" to complete ClassBody person.java /helloe/src line 139 Java Problem
Syntax error, insert "}" to complete MethodBody person.java /helloe/src line 23 Java Problem
The public type test1 must be defined in its own file hi.java /helloe/src line 6 Java Problem
This method requires a body instead of a semicolon hi.java /helloe/src line 11 Java Problem
Oct 19 '13 #6

Expert 100+
P: 785
DNumber cannot be resolved to a variable person.java /helloe/src line 61 Java Problem
See line 59 in program listing above:
"IDNumber" is misspelled. You have defined "idNumber" in line 12. (lowercase I and D)

Return type for the method is missing hi.java /helloe/src line 14 Java Problem
smileButton cannot be resolved to a type hi.java /helloe/src line 14 Java Problem
I can't see anything here. Have you removed the word "smileButton" from the program listing after the error message was created, but before you copied the code here to the forum?
...

General remark: put each of the classes in its own file. And the filename should the exact name of the file. There are inner classes, but it is an advanced topic, not suitable for beginner code.

The other errors are about missing closing brackets. Just make sure that you always have a closing bracket for each opening bracket.
Oct 21 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.