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.

Results 1 to 4 of 4

Thread: Java Program Help

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Program Help

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program Help

    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.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jackzorz (September 8th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Help

    Ok. I tinkered around last night and this is what I came up with.
    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);
     
     
    }
    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?











    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.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program Help

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    Would it be just something like:

    System.out.println(" Here are your pair(s): /n" + c1);
    That println() would need the classes to have a toString() method that returned a String describing the contents of the object.

    Is this optimized? Or is there a way to cut it down?
    Get it working first, then if there are problems, work on changing it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  2. convert java program to java ME program
    By abhishek1212 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 03:22 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 05:16 AM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM

Tags for this Thread