I need to write a simple program that displays up to 5 pairings of data types (int, string) (string, long) ect. I need to have at least two classes, a Pair class (generic) and an PairTest class. Can anyone help give me a head start?
Thanks!
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.
I need to write a simple program that displays up to 5 pairings of data types (int, string) (string, long) ect. I need to have at least two classes, a Pair class (generic) and an PairTest class. Can anyone help give me a head start?
Thanks!
Read the tutorial on defining a class: Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
And the one on generics: Lesson: Generics (Updated) (The Java™ Tutorials > Learning the Java Language)
Start with a few lines of code that defines each class and nothing more. Compile them, fix the errors and continue until no errors.
Post the code and ask any questions about the problems you are having. Be sure to copy and paste here any error messages.
If you don't understand my answer, don't ignore it, ask a question.
jackzorz (September 8th, 2014)
Ok. I tinkered around last night and this is what I came up with.
Now. Is this optimized? Or is there a way to cut it down? And from here I need to create a test class. I was thinking of running a Scanner and getting input from the user about how many pairs they want displayed. How would I Print out these pairs?package lab2; public class OrderedPair<F, S>{ public F getFirst; public S getSecond; private F first; private S second; public OrderedPair(F first, S second ){ this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() {return second;} // Five pairs of data OrderedPair<String, Integer> c1 = new OrderedPair<String, Integer>("Pi", 3); OrderedPair<String,String> c2 = new OrderedPair<String, String>("Great", "Super"); OrderedPair<Integer, Integer> c3 = new OrderedPair<Integer, Integer>(1, 2); OrderedPair<Float, String> c4 = new OrderedPair<Float, String>( 5f, "Maximum"); OrderedPair<Integer, Float> c5 = new OrderedPair<Integer, Float>(69, 69f); }
Would it be just something like:
System.out.println(" Here are your pair(s): /n" + c1);
Thank you for your help.
Last edited by jackzorz; September 8th, 2014 at 10:49 AM.
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
That println() would need the classes to have a toString() method that returned a String describing the contents of the object.Would it be just something like:
System.out.println(" Here are your pair(s): /n" + c1);
Get it working first, then if there are problems, work on changing it.Is this optimized? Or is there a way to cut it down?
If you don't understand my answer, don't ignore it, ask a question.