HELP ME PLEASE! I AM NEW TO JAVA AND AM SO CONFUSED. I have attempted to write q
ID: 3629267 • Letter: H
Question
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;
Explanation / Answer
I think u are a little confuse on how to use array, so I did the all programm with ordinary array, he would be much easier with ArrayList cause they are dynamic.
Name Class
//import java.lang.String; ==== NO need is imported by Default
public class Name {
// Each person must presents these characteristics!
private String firstName;
private String lastName;
//Date can be stores as intger, it's easy to compare a date in int type than in String!
int day,month,year;
/*public String[] studentList;
// PRETTY SURE THIS SHOULD BE INT HE TEST FILE BUT
YES IT DOES WE SETTING PARAMETER FOR A SINGLE STUDENT*/
public Name(String fName,String lName,int dd, int mm, int yyyy)
{
firstName=fName;
lastName=lName;
day=dd;
month=mm;
year=yyyy;
}
// Always better inizialize variables
public Name()
{
firstName="";
lastName="";
day=0;
month=0;
year=0;
}
//Copy constructor;
public Name(Name n){
firstName=n.firstName;
lastName=n.lastName;
day=n.day;
month=n.month;
year=n.year;
}
//Accessors and Mutators
public void setFirstName(String fName)
{
firstName=fName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lName)
{
lastName=lName;
}
public String getLastName()
{
return lastName;
}
public void setBirth(int d, int m, int y)
{
day=d;
month=m;
year=y;
}
// I cant return 3 element so I need to return an array or you can create 3 different accessors.
public int[] getBirth()
{
int[] temp = {day,month,year};
return temp;
}
@Override
public String toString() {
return firstName + " " + lastName + " " + day + "/" + month + "/" + year;
}
}
The main class does what you need, but you might want to change the format of the output or
implement the "searching" part (for instance a search with only the day).
Driver
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size;
do{
System.out.print("How many name do you want to insert?: ");
size=input.nextInt();
}while(size<=0);
//Inizializing an array that can store the amount of name objct
//<ClassType> + [] + name = new + <ClassType> +[<Size of array>]
Name[] list = new Name[size];
for(int i = 0; i<size; i++){
boolean error=false; // used for validate input
System.out.print("Insert first name: ");
String fname=input.next();
System.out.print("Insert last name: ");
String lname=input.next();
// The switch verify that for a specified month, there are 30 or 31 days;
int day_in_month=0;
int month=0;
do{
error=false;
System.out.print("Insert birth month: ");
month=input.nextInt();
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day_in_month = 31;
break;
case 2:
day_in_month = 28; // I'm not assuming leap years
break;
case 4:
case 6:
case 9:
case 11:
day_in_month = 30;
break;
default:
System.out.println("Invalid month");
error=true;
}
}while(error);
int day=0;
do{
error=false;
System.out.print("Insert birth day: ");
day=input.nextInt();
if(day>day_in_month)
error=true;
}while(error);
System.out.print("Insert birth year: ");
int year=input.nextInt();
//Creating the object
Name new_member = new Name(fname, lname, day, month, year);
//Adding the new_member to the array
list[i]=new_member;
System.out.println();//Junk line
}
while(true){
//MENU
System.out.println(" 1 - Display 2 - Search 3 - Exit");
System.out.print("What you wish to do?: ");
//Storing and execute command
int command = input.nextInt();
switch(command){
case 1:
//Display
//Sorting array
//new array
Name[] order = new Name[list.length];
int index, small=0;
System.out.println(" 1 - First Name 2 - Last Name 3 - Birth Date");
System.out.print("How you want to sort the list?: ");
switch(input.nextInt()){
case 1:
System.out.println(" -------- RESULT");
//Comparing each position i in the array with the following k positions
for(int i = 0; i<size; i++){
index=0;
small=0;
//Searching
for(int k=0; k<list.length; k++){
if(list[0].getFirstName().compareToIgnoreCase(list[k].getFirstName())>small){
small=list[i].getFirstName().compareToIgnoreCase(list[k].getFirstName());
index = k;
}
}
//new temp array
Name[] temp = new Name[list.length-1];
//Dividing the array from the found value Good value in [order]
//everything else [temp] that will be the new list
for(int j=0, f=0; j<list.length; j++)
if(j==index)
order[i]=list[j];
else{
temp[f]=list[j];
f++;
}
list=temp;
}
list=order;
//Output result
for(int i=0; i<list.length; i++)
System.out.println(i+" - "+list[i]);
break;
case 2:
System.out.println(" -------- RESULT");
//Same for the getLastName() methods
for(int i = 0; i<size; i++){
index=0;
small=0;
//Searching
for(int k=0; k<list.length; k++){
if(list[0].getLastName().compareToIgnoreCase(list[k].getLastName())>small){
small=list[i].getLastName().compareToIgnoreCase(list[k].getLastName());
index = k;
}
}
//new temp array
Name[] temp = new Name[list.length-1];
//Dividing the array from the found value Good value in [order]
//everything else [temp] that will be the new list
for(int j=0, f=0; j<list.length; j++)
if(j==index)
order[i]=list[j];
else{
temp[f]=list[j];
f++;
}
list=temp;
}
list=order;
//Output result
for(int i=0; i<list.length; i++)
System.out.println(i+" - "+list[i]);
break;
case 3:
System.out.println(" -------- RESULT");
//Damn! birth return an array one level more >.<
for(int i = 0; i<size; i++){
index=0;
small=0;
//Searching
for(int k=0; k<list.length; k++){
int[] dateA = list[0].getBirth();
int[] dateB = list[k].getBirth();
//compare Year, month and day
//Year
if(dateB[2]<dateA[2]){
index=k;
dateA=dateB;
}
//Month
else if(dateB[2]==dateA[2]){
if(dateB[1]<dateA[1]){
index=k;
dateA=dateB;
}
//Day
else if(dateB[1]==dateA[1]){
if(dateB[0]<dateA[0]){
index=k;
dateA=dateB;
}
}
}
}
//new temp array
Name[] temp = new Name[list.length-1];
//Dividing the array from the found value Good value in [order]
//everything else [temp] that will be the new list
for(int j=0, f=0; j<list.length; j++)
if(j==index)
order[i]=list[j];
else{
temp[f]=list[j];
f++;
}
list=temp;
}
list=order;
//Output result
for(int i=0; i<list.length; i++)
System.out.println(i+" - "+list[i]);
break;
default:
}
break;
case 2:
//Search
System.out.println(" 1 - By First Name 2 - By Last Name 3 - By Birth Date");
System.out.print("How you want to sort the list?: ");
switch(input.nextInt()){
case 1:
//input
System.out.print("Insert First Name: ");
String fname=input.next();
//Output result
for(Name i : list)
if(i.getFirstName().equalsIgnoreCase(fname))
System.out.println(i);
break;
case 2:
//input
System.out.print("Insert Last Name: ");
String lname=input.next();
//Output result
for(Name i : list)
if(i.getFirstName().equalsIgnoreCase(lname))
System.out.println(i);
break;
case 3:
//input
int [] date = new int[3];
System.out.print(" Insert birth month: ");
date[1]=input.nextInt();
System.out.print("Insert birth day: ");
date[0]=input.nextInt();
System.out.print("Insert birth year: ");
date[2]=input.nextInt();
//Output result
for(int i=0; i<list.length;i++){
if(date[2]==list[i].getBirth()[2]
&&date[0]==list[i].getBirth()[0]
&&date[1]==list[i].getBirth()[1])
System.out.println(list[i]);
}
break;
default:
}
break;
case 3:
System.exit(0);
break;
default:
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.