How to resolve the algorithm Return multiple values step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Return multiple values step by step in the Java programming language

Table of Contents

Problem Statement

Show how to return more than one value from a function.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Return multiple values step by step in the Java programming language

Main Function:

  • The main function is the entry point of the program. It parses command-line arguments to set values for nv_ and sv_.
  • It creates an instance of RReturnMultipleVals and calls the getPairFromPair, getPairFromList, and getPairFromMap methods to demonstrate returning multiple values.

Class:

  • RReturnMultipleVals contains the following methods:

    • getPairFromPair: Returns a pair of values (in this case, a Long and a String) as a custom Pair object.
    • getPairFromList: Returns a pair of values as a Java List.
    • getPairFromMap: Returns a pair of values as a Java Map.

Custom Class:

  • Pair is a custom class that represents a pair of values.

Inner Class:

  • The Point class is an inner class within the RReturnMultipleVals class. It represents a point with x and y coordinates.

Values Class:

  • The Values class is a generic class that can hold an arbitrary number of values of any type.
  • The constructor takes an array of objects and stores them in the objects array.
  • The get method can be used to retrieve a specific value by index.
  • The get method without arguments returns an array of all the values.

Usage of Values Class:

  • The getValues method in RReturnMultipleVals returns an instance of the Values class.
  • The getValues() method in the Values class can be used to get all the values as an array.
  • The get method can be used to get a specific value by index.

Sample Input and Output:

  • If no command-line arguments are provided, the program uses default values of nv_ = 1024 and sv_ = "Lorem ipsum dolor sit amet".
  • If command-line arguments are provided, they are used to set the values of nv_ and sv_.

Output:

Results extracted from a composite object:
1024, Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Results extracted from a Java Colections "List" object:
1024, Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Results extracted from a Java Colections "Map" object:
1024, Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Source code in the java programming language

Point getPoint() {
    return new Point(1, 2);
}

static class Point {
    int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}


Values<String, OutputStream> getValues() {
    return new Values<>("Rosetta Code", System.out);
}

static class Values<X, Y> {
    X x;
    Y y;

    public Values(X x, Y y) {
        this.x = x;
        this.y = y;
    }
}


import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

// =============================================================================
public class RReturnMultipleVals {
  public static final String K_lipsum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
  public static final Long   K_1024   = 1024L;
  public static final String L        = "L";
  public static final String R        = "R";

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  public static void main(String[] args) throws NumberFormatException{
    Long nv_;
    String sv_;
    switch (args.length) {
      case 0:
        nv_ = K_1024;
        sv_ = K_lipsum;
        break;
      case 1:
        nv_ = Long.parseLong(args[0]);
        sv_ = K_lipsum;
        break;
      case 2:
        nv_ = Long.parseLong(args[0]);
        sv_ = args[1];
        break;
      default:
        nv_ = Long.parseLong(args[0]);
        sv_ = args[1];
        for (int ix = 2; ix < args.length; ++ix) {
          sv_ = sv_ + " " + args[ix];
        }
        break;
    }

    RReturnMultipleVals lcl = new RReturnMultipleVals();

    Pair<Long, String> rvp = lcl.getPairFromPair(nv_, sv_); // values returned in a bespoke object
    System.out.println("Results extracted from a composite object:");
    System.out.printf("%s, %s%n%n", rvp.getLeftVal(), rvp.getRightVal());

    List<Object> rvl = lcl.getPairFromList(nv_, sv_); // values returned in a Java Collection object
    System.out.println("Results extracted from a Java Colections \"List\" object:");
    System.out.printf("%s, %s%n%n", rvl.get(0), rvl.get(1));

    Map<String, Object> rvm = lcl.getPairFromMap(nv_, sv_); // values returned in a Java Collection object
    System.out.println("Results extracted from a Java Colections \"Map\" object:");
    System.out.printf("%s, %s%n%n", rvm.get(L), rvm.get(R));
  }
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Return a bespoke object.
  // Permits any number and type of value to be returned
  public <T, U> Pair<T, U> getPairFromPair(T vl_, U vr_) {
    return new Pair<T, U>(vl_, vr_);
  }
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Exploit Java Collections classes to assemble a collection of results.
  // This example uses java.util.List
  public List<Object> getPairFromList(Object nv_, Object sv_) {
    List<Object> rset = new ArrayList<Object>();
    rset.add(nv_);
    rset.add(sv_);
    return rset;
  }
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Exploit Java Collections classes to assemble a collection of results.
  // This example uses java.util.Map
  public Map<String, Object> getPairFromMap(Object nv_, Object sv_) {
    Map<String, Object> rset = new HashMap<String, Object>();
    rset.put(L, nv_);
    rset.put(R, sv_);
    return rset;
  }

  // ===========================================================================
  private static class Pair<L, R> {
    private L leftVal;
    private R rightVal;

    public Pair(L nv_, R sv_) {
      setLeftVal(nv_);
      setRightVal(sv_);
    }
    public void setLeftVal(L nv_) {
      leftVal = nv_;
    }
    public L getLeftVal() {
      return leftVal;
    }
    public void setRightVal(R sv_) {
      rightVal = sv_;
    }
    public R getRightVal() {
      return rightVal;
    }
  }
}


public class Values {
	private final Object[] objects;
	public Values(Object ... objects) {
		this.objects = objects;
	}
	public <T> T get(int i) {
		return (T) objects[i];
	}
	public Object[] get() {
		return objects;
	}
	
	// to test
	public static void main(String[] args) {
		Values v = getValues();
		int i = v.get(0);
		System.out.println(i);
		printValues(i, v.get(1));
		printValues(v.get());
	}
	private static Values getValues() {
		return new Values(1, 3.8, "text");
	}
	private static void printValues(int i, double d) {
		System.out.println(i + ", " + d);
	}
	private static void printValues(Object ... objects) {
		for (int i=0; i<objects.length; i+=1) System.out.print((i==0 ? "": ", ") + objects[i]);
		System.out.println();
	}
}


  

You may also check:How to resolve the algorithm Bitmap step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Include a file step by step in the IWBASIC programming language
You may also check:How to resolve the algorithm EKG sequence convergence step by step in the jq programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm String case step by step in the XPL0 programming language