java - Where should scanner go in this program? -
i'm working on program takes input of 2 numbers , different calculations. have twonumbers class several different methods calculate sum, distance, average, etc.
should put scanner in class, or should put in main method?
i know basic i've been learning java couple weeks , i'm having hard time finding how should done/how input correlate instance variables , firstnumber , secondnumber
public class twonumbers{ private double firstnumber; private double secondnumber; public double getsum() { double sum = firstnumber + secondnumber; return sum; } public double getdifference() { double difference = firstnumber - secondnumber; return difference; } public double getproduct() { double product = firstnumber - secondnumber; return product; } public double getaverage() { double average = (firstnumber + secondnumber) / 2; return average; } public double getdistance() { double distance = math.abs(firstnumber - secondnumber); return distance; } public double getmax() { double maximum = math.max(firstnumber, secondnumber); return maximum; } public double getmin() { double minimum = math.min(firstnumber, secondnumber); return minimum; } }
each class should follow single responsibility principle. twonumbers
class should work double
numbers , perform operations on them, nothing more. providing double
numbers class should in client, , ability provide numbers, means client may define scanner
or way provide data.
Comments
Post a Comment