Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Have tried to use an actionlistener all night.
Nothing is working for me... even with all the damn youtube videos around, my books or the examples.
:/ Feel really lost.
"Tick, tack"
Okay, let's make this simple. Here is an interface.
interface EvenOdd {
boolean isEven(int v);
}
Create a small class that implements the interface and returns either true if the integer is even or false if it is odd. To test it, simply call the method with a number.
The only difference structure wise between this and the Comparable interface is that the Comparable interface is provided by the JDK API so all you have to do is implement it.
In this case, I created it so you need to copy the EvenOdd declaration to your source file.
Regards,
Jim
Okay so i made one main class, interface and the class that does everything for me :
main:
public class tets { public static void main(String[] args) { EvenOdd c = new small(); System.out.println(c.isEven(3)); } }
interface:
interface EvenOdd { boolean isEven(int v); }
class:
public class small implements EvenOdd { public boolean isEven(int v) { if (v % 2 == 0) { return true; } return false; } }
This kinda makes sense but my own code doesn't make sense haha.
Last edited by MrLowBot; January 20th, 2019 at 05:19 AM.
"Tick, tack"
And also, been looking at the comparable from java.
It has it's own method yet i don't know how to use it nor do i know
if i should call all my methods from "TheMainCity" --> "Interface" --> "CityImplementedWithComparable".
"Tick, tack"
Here's another example:import java.util.*; public class InterfaceExample { // Define the interface with a method interface TalkToMe { public void speak(); } // Define some classes that implement the interface class Dog implements TalkToMe { public void speak() { System.out.println("bow wow"); } } class Cat implements TalkToMe { public void speak() { System.out.println("Meow"); } } class Bird implements TalkToMe { public void speak() { System.out.println("Tweet"); } } //------------------------------------------------------- InterfaceExample() { // Define a list to hold objects that implement the TalkToMe interface List<TalkToMe> ttmList = new ArrayList<>(); Cat cat = new Cat(); ttmList.add(cat); TalkToMe ttm = new Dog(); ttmList.add(ttm); ttmList.add(new Bird()); // iterate the list and call the interface's method for(TalkToMe x : ttmList) { callSpeak(x); } } // Handle TalkToMe objects void callSpeak(TalkToMe ttm) { ttm.speak(); } public static void main(String[] args) { new InterfaceExample(); } }
If you don't understand my answer, don't ignore it, ask a question.
Think that i got this now:
Main:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; public class cityMain { private static BufferedReader r; public static ArrayList<City2> City = new ArrayList<City2>(); public static void main(String[] args) { int Z = 0; String C = null; try { FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\BB.dat"); r = new BufferedReader(file); String currLine; while ((currLine = r.readLine()) != null) { if (currLine.trim().length() > 0) { String[] split = currLine.split(";"); Z = (Integer.parseInt(split[0])); C = (split[1]); } City2 d = new City2(Z, C); City.add(d); } City.sort(Comparator.comparing(City2::getZipCode)); for (int i = 0; i < cityMain.City.size(); i++) { System.out.println(cityMain.City.get(i).getZipCode() + " " + cityMain.City.get(i).getCityName()); } } catch (IOException e) { System.out.println("Erorr : " + e); } } }
City2:
I get the correct answers back and using the "compareTo" method.public class City2 implements Comparable<City2> { private int zipCode; private String cityName; public City2(int zipCode, String cityName) { this.zipCode = zipCode; this.cityName = cityName; } public int getZipCode() { return zipCode; } public String getCityName() { return cityName; } @Override public int compareTo(City2 city) { return this.getZipCode() - city.getZipCode(); } }
"Tick, tack"
public static ArrayList<City2> City = new ArrayList<City2>();
Variable names should begin with a lowercase letter: city vs City
The name of a list should reflect what is in the list. cities would be a list of cities, city is singular and does not represent a list.
Are you sure that method is ever used or called? Add a print statement to show that it is executed.using the "compareTo" method.
If you don't understand my answer, don't ignore it, ask a question.
AH hell it is not being called! And nothing is coming out of it because it isnt getting called... Not sure of what to do now.
Tried with a print statement but all i get is five zeros on a line one after eachother.
Print looks like this:
0 0 0 0 0 0 0 23642 Höllviken 35243 Växjö 51000 Jönköping 72211 Västerås 75242 Uppsala 90325 Umeå 96133 Boden
Is doing something... it is doing "1-1"... basically.
But it has override too... i don't get it?!
Last edited by MrLowBot; January 20th, 2019 at 12:02 PM.
"Tick, tack"
What print statement creates those zeros? Please post the code.all i get is five zeros
If you don't understand my answer, don't ignore it, ask a question.
This is from the main :
while ((currLine = r.readLine()) != null) { if (currLine.trim().length() > 0) { String[] split = currLine.split(";"); // We tell it where it should split. Z = (Integer.parseInt(split[0])); // We one value of the integers. C = (split[1]); // Same with the characters. City2 d = new City2(Z, C); // We put them in the other class. cities.add(d); // We also add them in the array list for "City2". System.out.println(d.compareTo(d)); //This part right here prints out Zeros.. But i cannot put anything else in the "d" spot of compareTo. } }
"Tick, tack"
Look at what the compareTo method does. It subtracts two values and returns the results. Where do the two values come from?d.compareTo(d)
They both come from the object referenced by d. A value minus itself will give 0
I was suggesting that the print statement be placed inside of the compareTo method to show if it was being called by the sort method.
If you don't understand my answer, don't ignore it, ask a question.
Well it is not being called. Just checked with a print inside the method.
"Tick, tack"
Ok, why not? Where in you code is it supposed to be called?it is not being called.
If you don't understand my answer, don't ignore it, ask a question.
Use a sort method that takes a list/collection of objects that implement the Comparable interface. Read the API doc for the different sort methods.how to make that happen
http://docs.oracle.com/javase/8/docs/api/index.html
If you don't understand my answer, don't ignore it, ask a question.