Question:

Delete Object in Array?

by  |  earlier

0 LIKES UnLike

I get two file: Member.java and GolfClub.java

First Member Class:

public class Member

{

private String name;

private char type;

private double fee;

private int year;

public Member()

{

name=" not set";

type='p';

year= 2001;

}

public Member(String nameIn, char typeIn, int yearIn)

{

name= nameIn;

type= typeIn;

year= yearIn;

}

//getter method

public String getName()

{

return name;

}

public char getType()

{

if(type== 'S' || type=='s')

type= 'S';

else if(type=='P' || type=='p')

type='P';

else

type='P';

return type;

}

public double getFee()

{

if(type== 'S' || type=='s')

fee= 100.00;

else if(type=='P' || type=='p')

fee= 500.00;

else

fee= 500.00;

return fee;

}

public int getYear()

{

return year;

}

//setter method

public void setName(String nameIn)

{

name= nameIn;

}

public void setType(char typeIn)

{

if(type=='S' && type=='s'&& type=='P' && type=='p')

type= typeIn;

}

public void setYear(int yearIn)

{

year= yearIn;

}

// method return type of Member

public String getMemberTypeString()

{

if(type=='S'|| type== 's')

return "Social";

else if(type=='P'|| type== 'p')

return "Playing";

else

return "Playing";

}

// method return a single String

public String toString()

{

return name + "(" + getMemberTypeString() + " member" + ")" + " joined in " + year;

}

}

Second, GolfClub class:

import java.util.AbstractList;

public class GolfClub // class provider - container class

{

// attributes

private static final int MAX_SIZE = 5;

private Member[] account; // to store the collection of Member

private int counter; // to keep track how many Member are currently stored

public GolfClub(int size)

{

if (size > 0)

account = new Member[size];

else

account = new Member[MAX_SIZE];

counter = 0;

}

public Member searchMember(String nameIn)

{

for (int i = 0; i < account.length; i++)

if (account[i].getName().equalsIgnoreCase(n...

return account[i];

return null;

}

public void removeMember(String name)

{

int count=0;

boolean looking= true;

while(count < account.length && looking)

{

Member meb= account.get(count);

if(meb.getName().equals(name))

{

member.remove(count);

looking= false;

}

else

count++;

}

}

Now, I have two questions:

1.In Method (searchMember), I want to search Member but only find two last alphabet:

Example: Prompt user enter: "KEN", I will input "EN", return exactly " KEN"

2. I want to remove 1 member (object) in array (call ArrayList), but my current method was wrong.

So, I posted in here.Hope anyone help me.Thank a lot.

 Tags:

   Report

1 ANSWERS


  1. To find the word by its&#039; last 2 letters you can use regular expressions. A simpler way is to do something like this:

    char c1 = ? //1st character

    char c2 = ? //2nd character

    String str = ? //string

    if (str.charAt(str.length()-2) == c1 &amp;&amp; str.charAt(str.length()-1) == c2) return str;

    To delete an object from ArrayList, use the function indexOf() to find the position of that object (don&#039;t forget that your object must have overridden method equals()) then use that index in the function remove().

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions