Reputation: 37
I'm building a program and we are specifically not allowed to use multiple returns within a submodule.
I was wondering how to pass jimArea
, ashtynArea
and steveArea
from the submodule areaCalc
so that they can be used in main. Here is the submodule I'm referring to, followed by the full code.
public static int areaCalc(double depth, double width, double length)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
}
Here is the full code. Originally I had it just returning area, but it turns out I needed 3 areas, so I'm not sure how to do that without doing multiple returns.
import java.util.*;
public class PoolStorageCalculation
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the Depth of Steve's Pool in Metres.");
double steveDepth = sc.nextDouble();
System.out.println("Please enter the Width of Steve's Pool in Metres.");
double steveWidth = sc.nextDouble();
System.out.println("Please enter the Length of Steve's Pool in Metres.");
double steveLength = sc.nextDouble();
System.out.println("Please enter the Depth of Jim's Pool in Metres.");
double jimDepth = sc.nextDouble()
System.out.println("Please enter the Width of Jim's Pool in Metres.");
double jimWidth = sc.nextDouble();
System.out.println("Please enter the Length of Jim's Pool in Metres.");
double jimLength = sc.nextDouble;
System.out.println("Please enter the Depth of Ashtyn's Pool in Metres.");
double ashtynDepth = sc.nextDouble();
System.out.println("Please enter the Width of Ashtyn's Pool in Metres.");
double ashtynWidth = sc.nextDouble();
Systemm.out.println("Please enter the Length of Ashtyn's Pool in Metres.");
double ashtynLength = sc.nextDouble();
int area = areaCalc(steveDepth,steveWidth,steveLength,jimDepth,jimWidth,jimLength,ashtynDepth,ashtynLength,ashtynWidth);
int numRays = rayCalc(steveArea);
int numSharks = sharkCalc(jimArea);
int numTurtles = turtleCalc(ashtynArea);
System.out.println("Steve can store " + numRays + " Sting Rays in his " + steveArea + " Metres Cubed Pool.");
System.out.println("Jim can store " + numSharks + " Sharks in his " + jimArea + " Metres Cubed Pool.");
System.out.println("Ashtyn can store " + numTurtles + " Turtles in her " + ashtynArea + " Metres Cubed Pool.");
}
public static int areaCalc(double depth, double width, double length)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
return area;
}
public static int rayCalc(int steveArea)
{
int numRays = (int)(steveArea * 0.5);
return numRays;
}
public static int sharkCalc(int jimArea)
{
int numSharks = (int)(jimArea * 0.1);
return numSharks;
}
public static int turtleCalc(int ashtynArea)
{
int numTurtles = (int)(ashtynArea * 1.2);
return numTurtles;
}
}
Any help is greatly appreciated, thanks. John
Upvotes: 2
Views: 93
Reputation: 308763
Return an object or a data structure. New programmers tend to think too much in terms of primitives. You need to combine them into objects.
Does this code even compile?
public static int areaCalc(double depth, double width, double length)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
return area;
}
Where are jimDepth
and the rest defined? I see them in your main()
method, but none of them are in scope for areaCalc()
.
Learn about data structures. Something like this:
public class Cube {
private final double length;
private final double width;
private final double depth;
public Cube(double l, double w, double d) {
// Question: Do negative or zero dimensions make physical sense?
// What would you do about them here?
// Hint: Programming by contract and preconditions.
this.length = l;
this.width = w;
this.depth = d;
}
public double volume() {
return this.length*this.width*this.depth;
}
}
Now you can have different sets for each person:
Map<String, Cube> volumes = new HashMap<>();
volumes.put("jim", new Cube(1, 2, 3));
volumes.put("steve", new Cube(4, 5, 6));
volumes.put("ashtyn", new Cube(7, 8, 9));
Easy to add more or look up by name.
Upvotes: 3
Reputation: 521209
The fastest fix here is probably to just return a collection of areas. For example, you could return a List<Integer>
instead of a single int
, e.g.:
public static List<Integer> areaCalc(double depth, double width, double length)
{
// Note: you don't actually use the method parameters in the assignments below.
// This is probably a typo.
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
List<Integer> areaList = new ArrayList<>();
areaList.add(jimArea);
areaList.add(steveArea);
areaList.add(ashtynArea);
return areaList;
}
If you want to get fancier than this, then you could consider creating a bona-fide POJO which wraps the three area values:
public class AreaClass {
private int jimArea;
private int steveArea;
private int ashtynArea;
// constructor, getters and setters
}
Upvotes: 5
Reputation: 2530
One other option is to return an array holding all your values. But I doubt this will solve all your problems as duffymo pointed out in his answer.
public static int[] areaCalc(double depth, double width, double length)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
int[] area = {jimArea,steveArea,ashtynArea};
return area;
}
Upvotes: 0
Reputation: 1054
The solution is to use dto - data transferring object: type to wrap multiply objects:
public static class AreaDto {
private int jimArea;
private int steveArea;
private int ashtynArea;
public AreaDto(int jimArea, int steveArea, int ashtynArea) {
this.jimArea = jimArea;
this.steveArea = steveArea;
this.ashtynArea = ashtynArea;
}
public int getJimArea() {
return this.jimArea;
}
public int getSteveArea() {
return this.steveArea;
}
public int getAshtynArea() {
return this.ashtynArea;
}
}
public static AreaDto areaCalc(int steveDepth, int steveWidth, int steveLength, int jimDepth, int jimWidth, int jimLength, int ashtynDepth, int ashtynLength, int ashtynWidth)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
return new AreaDto(jimArea, steveArea, ashtynArea);
}
Upvotes: -1