Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Please help

  Asked By: Manju    Date: Oct 31    Category: Java    Views: 738
  

1.For this problem you will modify the Name class discussed in class,
create a class called Friend and write a driver. (Turn in all three
classes)
Name.java can be found under week1. You must modify this class.


For the Name class, implement the Comparable interface. Names will be
Compared by last name. If the last names are the same, Compare by
first, if the first and last names are the same then Compare by
middle.
Example: Let

n1 = new Name("bob", "smith");
n2 = new Name("joan","smith");
n3 = new Name("bob", "jones");

then

n1.compareTo(n2) returns -1
n2.compareTo(n1) returns 1;
n1.compareTo(n3) returns 1;

Note: the first, middle and last names are Strings. The String Class
has a compareTo method, use it. Make sure you test this
class before you move on.

Friend class

Each friend HAS - A name(you must use the above Name class) and a
phone number. You must use composition as your design. Therefore your
Friend class will have two instance variables one of type Name, the
other type String.
Add the appropriate accessor and mutators (getters and setters)
Implement the Comparable interface. Friend objects will be
compared based on their names and phone numbers. Add a toString
method to the class. The toString method will print
the name on one line in the following form last, first MI. Then the
phone number on the second line using the following form.
(312) 555-1212. (Hint see the API for the String class, look at
method substring)
Override the equals method. Two Friends are equal if they have the
same name and phone number.

Example: Let

f1 = new Friend( new Name
("paul","reed", "smith"),"3125551212" )
then
System.out.println( f1 ) // equivalent to
f1.toString()

The Driver
Do whatever you want but you must have an array of Friend objects,
call the sort method and print the list to show that they are sorted.
You can use ObjectArrayTool discussed in class. Please call the
Driver, FriendDriver.java
Turn in Name.java, Friend.java, FriendDriver.java and any helper
classes you are using. Zip all files and upload to COL using the link
Hw3A


Write a program that will read in a word from the user and encrypt
the word as follows. You must write a separate method to do the
encryption. You must get the line using the command prompt.(That is
use A BufferedReader object NOT a pop-up window)

i)Swap the first and last character of the word

ii)Replace each vowel as follows:

'a' with '*'
'e' with '#'
'i' with '$'
'o' with '%'
'u' with '&'


Note: Please name the class that has the main method as Encrypt.java.
Hint: See the WordGame worked out in class.


public class Name {
private String first;
private String middle;
private String last;

public Name(String first, String middle, String last) {
this.first = first;
this.middle = middle;
this.last = last;
}

public Name(String first, String last) {
this(first,"", last);
}

public String getFirst() {
return first;
}

public String getMiddle() {
return middle;
}

public String getLast() {
return last;
}

public void setFirst(String first){
this.first = first;
}

public void setMiddle(String middle){
this.middle = middle;

}

public void setLast(String last) {
this.last = last;
}

public String toString() {
String name = last + ", " + first;

if( !middle.equals("") ) {
name += " " + middle.charAt(0);
}
return name;
}

public boolean equals(Object that ) {
if( this == that ) return true;
if( that == null ) return false;
if( getClass() != that.getClass() ) return false;

Name name = (Name) that;

return ( this.first.equals( name.first) && this.middle.equals(
name.middle) && this.last.equals( name.last) ) ;
}

public static void main(String [] args ) {
Name n1,n2,n3;
n1 = new Name("Wilma","Anna", "FlintStone");
n2 = new Name("Fred", "Kazoo", "FlintStone");
System.out.println(n1);
System.out.println(n2);
System.out.println(n2.equals(n1));
System.out.println(n1.equals(n2));
System.out.println(n2.equals(n2));

n3 = new Name("Pebbles","Flinstone");
System.out.println(n3);

}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Please help Or get search suggestion and latest updates.




Tagged: