So I am trying to create a method that finds a title in a ArrayList of photos.
public class Album {
private String albumtitle;
private ArrayList<Photo> photos;
/**
* This constructor should initialize the
* instance variables of the class.
*/
public Album(String title) {
this.albumtitle = title;
photos = new ArrayList<>();
}
This is the code I have got to for trying to search for a specific title of the photo. I am not sure if i should put (int index) or (String title) in the methods parameters.
public Photo searchByTitle(int index) {
if (index >= 0 && index < photos.size()) {
String title = photos.get(index);
System.out.println(title);
}
return null;
}
I am beginner programmer, and I feel a little guidance will help a lot.
Edit: So lots of people are telling me to use for loops. My project requires me to not do for loops for methods, hence why I have displayed it in this way.
I'll give you an example the lecturer gave us:
https://lms.uwa.edu.au/bbcswebdav/pid-1134902-dt-content-rid-16529804_1/courses/CITS1001_SEM-2_2018/lectures/BooksReadJournal.java.pdf
She doesn't use for loops.
You could use the stream-API:
Arrays.stream(photos).filter(p -> p.getTitle().equalsIgnoreCase(searchTitle)).findFirst().orElseGet(...);
You iterate over each photo called p in this array and compare p's title with the one you search and return the first match.
Or simply use a normal for-loop:
for(int i = 0; i < photos.size(); i++) {
if(photos[i].getTitle().equalsIgnoreCase(searchTitle)){ return photos[i]; }
return new ErrorPhoto(); //or some error state
}
More enhanced for-each-loop:
for(Photo p: photos) {
if(p.getTitle().equalsIgnoreCase(searchTitle)) { return p; }
return new ErrorPhoto(); //or some error state
}
You iterate over each photo and compare its title with the one you search and return the first match:
for(int i = 0; i < photos.size, i++) {
if(photos.get(i).getTile().equals("title name"){
System.out.println("Found the title");
}
}
Your current code will always return null. If this is a search method to return the found object, you return the object if it is found. If not found, then return null:
public Photo searchByTitle(String title) {
for(Photo p : photos)
if(p.getTitle().equals(title))
return p;
return null;
}
This is the code I have got to for trying to search for a specific title of the photo. I am not sure if i should put (int index) or (String title) in the methods parameters
Since the method name itself suggested searchByTitle, whoever is using this method would expect it to receive String title.
If it is to be searched by index, then there can be another method as such:
public Photo searchByIndex(int index){
}
You have two options for a search, either you look a Photo up by its title or by its index. If your class had both methods, it would look like this:
public class Album {
private String albumtitle;
private ArrayList<Photo> photos;
/**
* This constructor should initialize the instance variables of the class.
*/
public Album(String title) {
this.albumtitle = title;
photos = new ArrayList<>();
}
/**
* Searches the {#link Photo} with the given title.
* #param title the title of the desired {#link Photo}
* #return the {#link Photo} with the given title or
* <code>null</code> if it is not in the list
*/
public Photo searchByTitle(String title) {
// initialize with null to return that if the photo was not found
Photo photo = null;
// iterate all photos and check if one of them has the title
for (int i = 0; i < photos.size(); i++) {
if (title.equals(photos.get(i).getTitle())) {
photo = photos.get(i);
// exit the loop when the photo was found
break;
}
}
return photo;
}
/**
* Searches the {#link Photo} with the given index.<br>
* Checks if the index is valid in the list of {#link Photo}s
* #param index the index of the {#link Photo}
* #return the {#link Photo} with the given index or <code>null</code>
* if it is not in the list or the index is invalid.
*/
public Photo searchByIndex(int index) {
try {
// use the method of the list to get the photo
return photos.get(index);
} catch (IndexOutOfBoundsException ioe) {
// print some error message for the case of an invalid index
System.err.println("The given index is not available!");
return null;
}
}
}
package simple;
import java.util.ArrayList;
public class Album {
private static String albumtitle;
public Album(String title) {
Album.albumtitle = title;
}
public Photo searchByTitle(ArrayList<Photo> photos) {
if (index >= 0 && index < photos.size()) {
Photo title = photos.get(index);
if (title.getTitle().equals(albumtitle))
System.out.println("tile Found" + title);
}
return null;
}
public static void main(String[] args) {
ArrayList<Photo> photos = new ArrayList<>();
//Searching title2 or u can pass as parameter
Album album = new Album("title2");
for (int i = 0; i < 5; i++) {
Photo photo = new Photo();
photo.setTitle("Title" + i);
photos.add(photo);
}
album.searchByTitle(photos);
}
}
package simple;
public class Photo {
String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Are looking for this..?
Related
Item searchByPattern(String pat)
{
for(Iterator iter = items.iterator(); iter.hasNext(); )
{
Item item = (Item)iter.next();
if ((xxxxxxxxxxxx).matches(".*"+pat+".*"))
{
return item;
}
}
}
The above code is part of a class from my java program
public class Item
{
private String title;
private int playingTime;
private boolean gotIt;
private String comment;
/**
* Initialise the fields of the item.
*/
public Item(String theTitle, int time)
{
title = theTitle;
playingTime = time;
gotIt = true;
comment = "";
}
public String getTitle() {
return title;
}
/**
* Enter a comment for this item.
*/
public void setComment(String comment)
{
this.comment = comment;
}
/**
* Return the comment for this item.
*/
public String getComment()
{
return comment;
}
/**
* Set the flag indicating whether we own this item.
*/
public void setOwn(boolean ownIt)
{
gotIt = ownIt;
}
/**
* Return information whether we own a copy of this item.
*/
public boolean getOwn()
{
return gotIt;
}
public int getPlayingTime()
{
return playingTime;
}
/**
* Print details about this item to the text terminal.
*/
public void print()
{
System.out.println("Title: " + title);
if(gotIt) {
System.out.println("Got it: Yes");
} else {
System.out.println("Got it: No");
}
System.out.println("Playing time: " + playingTime);
System.out.println("Comment: " + comment);
}
}
I want to access all the methods that return values from class Item and once it matches the statement in Item searchByPattern, it will return the object.
I knew that I can do it by or operator like item.getTitle().matches(".*"+pat+".*") ||item.getComment().matches(".*"+pat+".*")||.......
but is it possible to get the same result by using a method in (xxxxxxxxxx)?
This isn't directly possible to do, however there are a few things you can try (from easiest to hard):
Just check all String type methods yourself in your code.
Add a special method in Item that does the match so Item class can decide itself when it matches. Here again you will need to make check all Strings manually.
You could add a method to Item that returns all methods that return a String as functions:
Code:
List<Supplier<String>> getAllStringMethods() {
return Arrays.asList(this::getComment, this::getTitle);
}
You can then use that to check all Strings one at a time by doing:
boolean match = item.getAllStrings().stream()
.map(Supplier::get)
.anyMatch(s -> s.matches("pattern"));
You can use Reflection to inspect the Item.class to find all methods that take no parameters and return a String, and then invoke them one by one. This is complicated and slow, and beyond the scope of this answer to explain further.
I'm currently in the process of creating a shopping cart simulation program. The main GUI contains two lists, one is a list of products or the inventory. (products stored within a .dat file which is automatically loaded upon launch) The other is blank and is to model my shopping basket. The idea is to be able to scan items from my inventory into the checkout basket. As this is happening i want a text field i created to dynamically update with the cost of all the items in the basket.
Below is the method for my scan button, which is supposed to perform the above :
public void actionPerformed(ActionEvent evt) {
//Get the newly added list values.
JList list = productList.getSelectedValuesList();
double totalAddedValue = 0.0;
double oldCartValue = 0.0;
//Iterate to get the price of the new items.
for (int i = 0; i < list.getModel().getSize(); i++) {
CartItem item = (CartItem) list.getModel().getElementAt(i);
totalAddedValue += Double.ParseDouble(item.getPrice());
}
//Set total price value as an addition to cart total field.
//cartTotalField must be accessible here.
string cartFieldText = cartTotalField.getText();
//Check that cartTextField already contains a value.
if(cartTextField != null && !cartTextField.isEmpty())
{
oldCartValue = Double.parseDouble(cartFieldText);
}
cartTotalField.setText(String.valueOf(oldCartValue + totalAddedValue));
checkoutBasket.addElement(list);
}
Currently however scanning the item will add it to the list but print strange results in the total. (Adds 5.5 for each item regardless of their actual value**). It will also print a line under the item name as such javax.swing.JList[,0,0,344x326,layout=java.awt.BorderLa... .
Below are the classes for my CartItem and ItemList if they may help. Thank you.
-Cart Item
import java.io.*;
public class CartItem implements Serializable {
private String barcodeNo;
private String itemName;
private String price;
public CartItem() {
}
public CartItem (String barno, String in, String cost) {
barcodeNo = barno;
itemName = in;
price = cost;
}
public String getBarcodeNo(){
return barcodeNo;
}
public String getItemName(){
return itemName;
}
public void setitemName(String itemName){
this.itemName = itemName;
}
public String getPrice(){
return price;
}
public String toString(){
return barcodeNo + ": " + itemName + ", " + price;
}
public Object getID() {
// TODO Auto-generated method stub
return null;
}
}
-ItemList
import javax.swing.DefaultListModel;
public class ItemList extends DefaultListModel {
public ItemList(){
super();
}
public void addCartItem(String barcodeNo, String itemName, String price){
super.addElement(new CartItem(barcodeNo, itemName, price));
}
public CartItem findItemByName(String name){
CartItem temp;
int indexLocation = -1;
for (int i = 0; i < super.size(); i++) {
temp = (CartItem)super.elementAt(i);
if (temp.getItemName().equals(name)){
indexLocation = i;
break;
}
}
if (indexLocation == -1) {
return null;
} else {
return (CartItem)super.elementAt(indexLocation);
}
}
public CartItem findItemByBarcode(String id){
CartItem temp;
int indexLocation = -1;
for (int i = 0; i < super.size(); i++) {
temp = (CartItem)super.elementAt(i);
if (temp.getBarcodeNo().equals(id)){
indexLocation = i;
break;
}
}
if (indexLocation == -1) {
return null;
} else {
return (CartItem)super.elementAt(indexLocation);
}
}
public void removeItem(String id){
CartItem empToGo = this.findItemByBarcode(id);
super.removeElement(empToGo);
}
}
You're adding the JList itself to the check out basket, and that doesn't make sense:
checkoutBasket.addElement(list);
This, javax.swing.JList[,0,0,344x326,layout=java.awt.BorderLa... . shows that something is displaying the toString() representation of the JList, likely your checkout basket.
Regarding,
Adds 5.5 for each item regardless of their actual value
I don't think your current code shows why this is happening, and you might want to create and post an mcve.
Other thoughts:
Don't use String to represent price, but rather consider a numeric field, perhaps BigDecimal if you want to be accurate for money.
You look to be mixing your view with your model too much. Try to keep them separate as possible, meaning, you should likely have some more non-GUI classes including one to represent the shopping basket, and any other nouns.
I'm making a program with a superclass called Experiment and a subclass of Group with a subclass of Subject. When I hit printFinalExperiment I want it to print out the group name and all the Subjects contained within the groups, but whenever i try that it prints out all the subjects, and i cant figure out why. I am just learning java and this is probably really inefficient, but I've been experimenting with different types of lists to try to solve this problem, but i cant figure out what to do. Also is there an easier way to arrange my arraylist of subjects in alphabetical order by their name instead of having to create a separate arraylist of strings? seen in alphabetize method in experiment class.
//class Experiment
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Experiment
{
public Random number;
public ArrayList<String> allSubject;
public ArrayList<Subject> allSubjects,alphaSubjects,matched;
public ArrayList<Group> experiment;
public int value,groups;
private ArrayList<Integer> numbers;
/**
* Make a new Experiment. Then use method addSubject to add
* Subjects to your experiment. Then call the assignGroups
* method to assign Subjects to each group.
*/
public Experiment(int numberOfGroups)
{
groups = numberOfGroups;
number = new Random();
numbers = new ArrayList<Integer>();
experiment = new ArrayList<Group>();
matched = new ArrayList<Subject>();
allSubjects = new ArrayList<Subject>();
allSubject = new ArrayList<String>();
alphaSubjects = new ArrayList<Subject>();
for(int i = 0; i < numberOfGroups; i++)
{
experiment.add(new Group(i));
}
}
/**
* Input the number of desired subjects and groups
* for you experiment. This will create the number
* of groups specified and will assign the subjects
* as close to even as possible.
*/
public Experiment(int numberOfSubjects, int numberOfGroups)
{
number = new Random();
numbers = new ArrayList<Integer>();
experiment = new ArrayList<Group>();
matched = new ArrayList<Subject>();
allSubjects = new ArrayList<Subject>();
allSubject = new ArrayList<String>();
alphaSubjects = new ArrayList<Subject>();
for(int i=0;i<numberOfSubjects;i++)
{
addDefaultSubject(i);
}
assignGroups(numberOfGroups);
printDefaultExper();
}
/**
* Adds a new Subject to the experiment.
*/
public void addSubject(String name, String description)
{
allSubjects.add(new Subject(name,description));
allSubject.add(name.toLowerCase());
}
/**
*Used only for the second constructor.
*/
private void addDefaultSubject(int i)
{
allSubjects.add(new Subject(i));
allSubject.add(allSubjects.get(i).getName().toLowerCase());
}
/**
* Alphabetizes the list of Subjects based on their
* name input by the user. As of right now, this method
* is case sensitive meaning Strings starting with
* capitals will be listed before those without capitals.
*/
private void alphabetize()
{
alphaSubjects.clear();
Collections.sort(allSubject);
//compare the String arraylist to the subject arraylist to reset the subject arraylist indeces in alphabetical order.
for(int i =0;i<allSubject.size();i++)
{
String theName = allSubject.get(i);
for(Subject subject:allSubjects)
{
if(subject.getName().toLowerCase().contains(theName))
{
alphaSubjects.add(new Subject(subject.getName(),subject.getDescription()));
}
}
}
}
/**
* Creates random numbers from 0 to
* the number of Subjects in the experiment.
*/
private void randomize()
{
alphabetize();
value = number.nextInt(allSubjects.size());
for(int i = 0; i < allSubjects.size();i++)
{
while(numbers.contains(value))
{
value = number.nextInt(allSubjects.size());
}
numbers.add(value);
}
}
/**
* Assigns the numbers created randomly by
* Blue Jay's random number generator to the
* alphabetized list Subjects.
*/
public void assignNumbers()
{
matched.clear();
numbers.clear();
randomize();
for(int i =0; i < numbers.size();i++)
{
matched.add(alphaSubjects.get(numbers.get(i)));
experiment.get(i%groups).addSubject(matched.get(i));
}
}
//The previous method and the next method are what i was trouble shooting so either one can be changed.
/**
* Splits subjects into groups. Every nth (n is the number of
* groups input) is assign to a new group. For example:
* Say you have 17 subjects (0-16) and you want 4 groups.
* Subjects 0,4,8,12, and 16 will be in group 1, subjects
* 1,5,9,and 13 will be in group 2 and so on until 4 complete
* groups are made.
*/
public void assignGroups(int numberOfGroups)
{
numbers.clear();
assignNumbers();
if(numberOfGroups <=0)
{
System.out.println("You need at least one group.");
}
else{
int numberOfSubjects = allSubjects.size();
experiment = new ArrayList<Group>();
for(int i = 0; i < numberOfGroups; i++)
{
Group group = new Group(i);
for(Integer j = i; j < matched.size(); j+=numberOfGroups)
{
group.addSubjects(matched.get(j).getName(),matched.get(j).getDescription());
}
experiment.add(group);
}
}
}
/**
* Prints the final layout of the Groups with its
* subjects in alphabetical order
*/
public void printFinalExperiment()
{
for(Group group: experiment)
{
System.out.println("Group " + group.getGroupName());
group.printGroupList();
}
System.out.println();
}
//this next method is only used for the second constructor.
private void printDefaultExper()
{
for(Group group: experiment)
{
System.out.println("Group " + group.getGroupName());
System.out.println(" " + group.getSize());
}
System.out.println();
}
//class group
import java.util.ArrayList;
public class Group
{
// instance variables - replace the example below with your own
public static ArrayList<Subject> group;
public int groupNumber;
public int size;
public String groupName;
public Group(int groupNumber)
{
this.groupNumber = groupNumber;
groupName = "Group" + groupNumber;
group = new ArrayList<Subject>();
}
public Group(String groupName)
{
this.groupName=groupName;
group = new ArrayList<Subject>();
}
public void addSubject(Subject subject)
{
group.add(subject);
}
public void addSubjects(String name,String description)
{
group.add(new Subject(name,description));
}
public int getGroupNumber()
{
return groupNumber;
}
public int getSize()
{
size = group.size();
return size;
}
public String getGroupName()
{
return groupName;
}
public void printGroupList()
{
for(Subject subject: group)
{
System.out.println(" " + subject.getName() + ": " + subject.getDescription());
}
}
}
//class Subject
public class Subject
{
public final String name;
public final String description;
public Subject(String name, String description)
{
this.name = name;
this.description = description;
}
public Subject(int aNumber)
{
name = "Subject" + aNumber;
aNumber++;
description = "default";
}
public String getName()
{
return name;
}
public String getDescription()
{
return description;
}
}
This may have to do with the way you declared the ArrayList group in your Group class.
Is there any reason you declared it as static? Non-static instance variables mean that each instance of your class, in this case each Group object, will have its own group ArrayList. On the other hand, static instance variables are variables that are associated with the class in a general sense. Each Group object you instantiate will not have its own group ArrayList in your case.
Try making it a non-static instance variable (by simply deleting the word static.)
Also, here's a link to better explain the difference between static and non-static in Java: http://www.caveofprogramming.com/articles/java/java-for-beginners-static-variables-what-are-they/
Hello ive added two new functions to the implementation of an interface.
this is the implementation file...
import au.edu.uow.Collection.Album;
import java.util.ArrayList;
public class CDAlbum implements Album {
private String Title;
private String Genre;
private String Artist;
private String MediaType;
private ArrayList<String> trackList;
public CDAlbum(String TempTitle, String TempGenre, String TempArtist, ArrayList<String> TempTracklist, String TempMediaType){
//Set initail variable values
Title = TempTitle;
Genre = TempGenre;
Artist = TempArtist;
trackList = TempTracklist;
MediaType = TempMediaType;
}
//Accessor Functions
public String getMediaType(){
//Return Media Type
return MediaType;
}
public String getTitle(){
//Return Title
return Title;
}
public String getGenre(){
//Return Genre
return Genre;
}
public String getArtist(){
//Return Artist
return Artist;
}
public ArrayList<String> getTrackList(){
//Return Tracklist
return trackList;
}
}
The bottom two functions are the added functions( getArtist(), getTrackList())
the problem im having is that when i try to call these functions from a class file it give me the following errors.
./au/edu/uow/UserInterface/UserInterface.java:95: cannot find symbol
symbol : method getArtist()
location: interface au.edu.uow.Collection.Album
System.out.println(albumCollection.get(number).getArtist());
^
./au/edu/uow/UserInterface/UserInterface.java:98: cannot find symbol
symbol : method getTrackList()
location: interface au.edu.uow.Collection.Album
ArrayList<String> trackList = albumCollection.get(number).getTrackList();
When i call the functions
import au.edu.uow.Collection.Album;
System.out.println(albumCollection.get(number).getArtist());
//Access the track titles
ArrayList<String> trackList = albumCollection.get(number).getTrackList();
//Output collection
int arrayListSize = trackList.size();
for(int i = 0; i < arrayListSize; i++)
{
System.out.println( i + ": " + trackList.get(i));
}
Album specifies neither getArtist nor getTrackList:
public interface Album {
/**
* This method returns the media type of the album.
* #return the media type of the album, either CD or DVD
* #see #getTitle()
* #see #getGenre()
*/
String getMediaType();
/**
* This method returns the title of the album.
* #return the title of the album
* #see #getMediaType()
* #see #getGenre()
*/
String getTitle();
/**
* This method returns the genre of the album.
* #return the genre of the album
* #see #getTitle()
* #see #getMediaType()
*/
String getGenre();
}
... only CDAlbum does.
You can determine whether the Album is a CDAlbum or DVDAlbum by checking Album.getMediaType; then, if it is a CD, you can cast to CDAlbum and invoke getArtist and getTrackList then.
for (final Album album : albumCollection) {
final String type = album.getMediaType();
System.out.print(type + " album: " + album.getTitle()
+ " (" + album.getGenre() + ") - ");
if (type.equals("CD")) {
final CDAlbum cd = (CDAlbum) album;
System.out.println(cd.getArtist());
int n = 0;
for (final String track : cd.getTrackList()) {
System.out.printf("#%2d - %s\n", ++n, track);
}
} else {
final DVDAlbum dvd = (DVDAlbum) album;
System.out.println(dvd.getDirector());
System.out.println(dvd.getPlotOutline());
}
}
Is Album the return type of albumCollection.get(number)? If so, then you need to add getArtist and getTrackList method into your interface as well.
When you add new method in your interface you must implement in your implementation class.
check the type of albumCollection.get(number)
if albumCollection.get(number) return Album then you can call getArtist()
Create a super class say for example MasterAlbums and extend your CDAlbum and DVDAlbum from it.Place the methods getArtist() & getTrackList() in the super class.Do not override it in the subclasses.Now when you call System.out.println(albumCollection.get(number).getArtist()); , It will refer to the superclass method and your artist & tracklist will be printed.
I've given an assigment and its the last day of itself. I did most of it but on the last question I have a problem with creating a compareTo() function.
Here is what it does want from us ;
compareTo
public int compareTo(java.lang.Object other)
Specified by:
compareTo in interface java.lang.Comparable
Here is what I did
public int compareTo(Object obj)
{
Document tmp = (Document)obj;
if(this.text < tmp.text)
{
/* instance lt received */
return -1;
}
else if(this.text > tmp.text)
{
/* instance gt received */
return 1;
}
/* instance == received */
return 0;
}
Here is my whole Document.java file
class Document
{
private String text;
/**
* Default constructor; Initialize amount to zero.
*/
public Document()
{
text = "";
}
/**
* Default constructor; Initialize text to input value
* #param text New text value
*/
public Document(String text)
{
this.text = text;
}
/**
* Returns as a string the contents of the Document.
*/
public String toString()
{
return text;
}
and Here is the test file of itself.
import java.util.Arrays;
public class Homework2 extends Document {
/** ======================
* ContainsKeyword
* Returns true if the Document
* object passed in contains keyword as a substring
* of its text property.
* ======================
*/
public static boolean ContainsKeyword(Document docObject, String keyword)
{
if (docObject.toString().indexOf(keyword,0) >= 0)
return true;
return false;
}
public static void main(String[] args){
Email email1= new Email("Programming in Java",
"Larry", "Curly", "Programming");
Email email2 = new Email("Running marathons",
"Speedy", "Gonzales", "races");
System.out.println(email1);
File file1 = new File("Some Java file", "file.txt");
File file2 = new File(
"Boluspor wins against Besiktas. Muahahahaha",
"bolutas.txt");
Document doc = new Document (
"ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?,ok?");
System.out.println("\n"+file1);
System.out.println("\nWhich contains Java?");
if (ContainsKeyword(email1,"Java")) System.out.println(" Email1");
if (ContainsKeyword(email2,"Java")) System.out.println(" Email2");
if (ContainsKeyword(file1,"Java")) System.out.println(" File1");
if (ContainsKeyword(file2,"Java")) System.out.println(" File2");
Document [] da = new Document [5];
da[0] = email1;
da[1] = email2;
da[2] = file1;
da[3] = file2;
da[4] = doc;
Arrays.sort(da);
System.out.println("\nAfter sort:");
for(Document d : da){
System.out.println(d);
}
}
}
What I wanted to ask is, I cannot compare the objects from my Email.java and File.java , I can do anything else but the last part which starts with Document [] da... That part gives an error. What am I doing wrong here?
The error is ;
Exception in thread "main" java.lang.ClassCastException: Email cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at Homework2.main(Homework2.java:53)
UPLOAD ** Here is my Email and File Class..
/**
* First define class for Email, derive from Document
*/
class Email extends Document
{
private String sender;
private String recipient;
private String title;
private String body;
/**
* Constructors
*/
public Email()
{
super();
sender = "";
recipient = "";
title = "";
body = "";
}
public Email(String body, String sender, String recipient, String title)
{
this.sender = sender;
this.recipient = recipient;
this.title = title;
this.body = body;
}
// ======================
// Various accessor and mutator methods
// ======================
public String getSender()
{
return sender;
}
public void setSender(String sender)
{
this.sender = sender;
}
public String getRecipient()
{
return recipient;
}
public void setRecipient(String recipient)
{
this.recipient = recipient;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getBody(){
return body;
}
public void getBody(String body){
this.body = body;
}
/**
* Returns as a string the contents of the text fields concatenated
* together. Uses super.toString to get the parent's text.
*/
public String toString()
{
return "Sender:" + sender + ",Recipient:" + recipient + ",Title:" + title + ",Body:" + body + " " +
super.toString();
}
} // Email
and File ;
/**
* Next define class for File, derive from Document
* For brevity, short one-line methods are defined here in the
* header.
*/
class File extends Document
{
private String pathname;
/**
* Constructors.
*/
public File()
{
super();
pathname = "";
}
public File(String body, String pathname)
{
super(body);
this.pathname = pathname;
}
// ======================
// Various accessor and mutator methods
// ======================
public void setPathname(String s)
{
pathname = s;
}
public String getPathname()
{
return pathname;
}
/**
* Returns as a string the contents of the text fields concatenated
* together. Uses super.toString to get the parent's text.
*/
public String toString()
{
return "Pathname " + pathname + " Body " + super.toString();
}
} // File
Where did you put the compareTo method? If you're trying to sort an array of Documents, you need to have Document implement Comparable (or pass in a Comparator):
public class Document implements Comparable<Document> {
Or, if for some bizarre reason you're not allowed to use generics:
public class Document implements Comparable {
Then put compareTo within Document.
The exact reason it is failing is because you are trying to call Arrays.sort on a class that does not implement comparable
Implementing Comparable allows
calling Collections.sort and Collections.binarySearch
calling Arrays.sort and Arrays.binarySearch
using objects as keys in a TreeMap
using objects as elements in a TreeSet
Email did not implement the Comparable interface
use
public class Email implements Comparable<Email>
read this to do yourself a favor http://www.javapractices.com/topic/TopicAction.do?Id=10
The other note is you said you want to compare
Email.java and File.java
You will need a custom function for that based on the logic.
compareTo is used to compare two instances of the same type. It also means that the function lives in the Email class
Email myEmail = new Email();
Email hisEmail = new Email();
myEmail.compareTo(hisEmail);
What you are doing wrong is in the error message.
You can only sort objects for classes which implement Comparable. Your class does not.
As you are sorting a number of different types you may want to provide a custom Comparator instead, or make Document implement Comparable.
try this:
Here is my whole Document.java file
class Document implements Comparable { //this line is your solution.
private String text;
/**
* Default constructor; Initialize amount to zero.
*/
public Document()
{
text = "";
}
/**
* Default constructor; Initialize text to input value
* #param text New text value
*/
....}