2 Replies - 2169 Views - Last Post: 27 September 2011 - 05:32 PM Rate Topic: -----

#1 alBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-September 11

HELP PLEASE! Java first/last name birth date Array Sort TestClass

Posted 27 September 2011 - 04:19 PM

HELP ME PLEASE! I AM NEW TO JAVA AND AM SO CONFUSED. I have attempted to write quite a few different programs for this assignment but I always end up just getting confused and the program looks like a complete mess & I just start over.

So far.. my best attemt involves using a Test class file & a data class file.

I am just going to post the bits & pieces of code I have been going in circles with. PLEASE HELP!

Assignment: Write a program that will accept ten (or more) names (first and last names) and associated
birth-date. Your program should ask the user for how many people s/he wants to enter and then read the
names (first and last) and the corresponding birth-date. The birth-date must be entered in the following
format: MM/DD/YYYY. Your program should then use a menu that allows the user to display, search and
exit. Display should display the list of the entries, sorted by last name, first name, or birth-date as
requested by the user. Search should search for a specific entry by a specific field (last name, first name or
birth-date) as requested by the user. Exit should terminate the program when the user selects exit from the
menu.

// TEST FILE

import java.util.Scanner;

public class StudentTest
{
public static void main(String[] args)
{
int n;
String students;
Scanner input = new Scanner(System.in);

System.out.print("How many students?"); // STUCK HERE.. KNOW ITS NOT RIGHT
n = input.nextInt();
Student[] students = new Student[n];

for(int i = 0; i < n; i++)
{
System.out.println("Enter Student's fName");
String fName = input.nextLine();

System.out.println("enter last name:");
String lName = input.nextLine();

System.out.println("enter snumber");
String sNumber = input.nextLine();

students[i] = new Student("fName", "lName", "sNumber");

System.out.println(students[i]);
}
}
}

// THIS IS JUST A "SIMILAR" ONE I HAVE BEEN TRYING TO MODIFY SO ILL BETTER UNDERSTAND THE CONCEPTS AND CAN HOPEFULLY WRITE AN ORIGINAL FOR MY SPECIFIC ASSIGNMENT

import java.lang.String;

public class Student {

private String firstName;
private String lastName;
private String studentNumber;

public String studentList[][][]; // PRETTY SURE THIS SHOULD BE INT HE TEST FILE BUT

COULDNT FIGURE OUT FORMAT

public Student(String fName,String lName,String sNumber)
{
firstName=fName;
lastName=lName;
studentNumber=sNumber;

}



public Student()
{
}

public void setFirstName(String fName)
{
firstName=fName;
}

public String getFirstName()
{
return firstName;

}//end the method setfirstName

public void setLastName(String lName)
{
lastName=lName; // Store the lastName

} // end the method setlastName

public String getLastName()
{
return lastName;
}

public void setStudentNumber(String sNumber)
{
studentNumber=sNumber;

} // end the method setstudentNumber

public String getStudentNumber()
{
return studentNumber;

Is This A Good Question/Topic? 0
  • +

Replies To: HELP PLEASE! Java first/last name birth date Array Sort TestClass

#2 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: HELP PLEASE! Java first/last name birth date Array Sort TestClass

Posted 27 September 2011 - 05:12 PM

well first off I might suggest using the [ code ] button and post your code inbetween the two tags. Makes it easier for us.

Second when you get an assignment and you feel overwhelmed about how complex it is I find it easier to break the assignment into smaller tasks and work on them individually. Often times looking at the entire picture can be daunting.

So lets break this up into a few smaller tasks and conquor them.
first off you have to enter 10 or more students. I see that you made a class for students which is a good start.

also although there is nothing wrong with using 1 letter before a variable sometimes certain letters can make it difficult to read.. I'm referring to lName, fName, sNumber. l looks like a a small L, and the number 1. so it might be better to write lastName, firstName, studenNumber.

Also you might find it easier to make just one constructor for student like

public Student(String firstName, String lastName, String studentNumber)
{
  this.firstName = firstName;
  this.lastName = lastName;
  this.studentNumber = studentNumber;
}



then use the appropriate getter and setter methods. It also wouldn't hurt to make a few more gets for when you want to display it by last name, or by first name or in a particular format such like

public String getStudentByFirstName()
{
  return firstName + " " + lastName + " - Born:" + birthDate;
}
public String getStudentByLastName()
{
  return lastName + "," + firstName + " - Born:" + birthDate;
}



it's not that you HAVE to do this, it just makes it much easier when you go to display them to just loop through a array and put something like

for (int i=0; i<studentArray.size; i++)
  someFrame.add(studentArray.get(i).getStudentByFirstName();



just makes it a littler easier to read in my opiion.
Was This Post Helpful? 0
  • +
  • -

#3 alBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-September 11

Re: HELP PLEASE! Java first/last name birth date Array Sort TestClass

Posted 27 September 2011 - 05:32 PM

Sorry.. like I said, am new to programming (and this forum).

Thank you for the tips!

I still am struggling however.. I changed the get methods but I am not familiar with the set method? Is there an example format I can follow that is similar to my assignment?

(that is my biggest problem.. all of my examples/format/info from class and my books are so basic & I know so little about java that I have a difficult time when things get any more complex)

Also, in the Test file.. I am not really sure WHAT all is supposed to be in it and what is supposed to be in the Student class file.

Anything anyone can help me with is much appreciated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1