### Thread: Write a program that prompts two integers.....

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2011
Posts
18
Rep Power
0

#### Write a program that prompts two integers.....

Hi everyone,

I have been given this question for homework and I am struggling. I have attempted the question but the professor said I have done it wrong. Below is the question and my incorrect coding:

Write a program that prompts the user for two numbers then prints, the sum, the difference, the product, the average, the distance(abs of difference), the maximum and the minimum. It also says to use "aFirst" as the first integer, and "aSecond" as the second integer".

My Code:

import java.util.Scanner;
import java.lang.Math;
/**
* Calculates simple statistics based on two input integers.
* The sum, The difference, The product, The average,
* The distance, The maximum, The minimum.
*/
public class Pair
{
public static void main (String[] args)
{
int aFirst;
int aSecond;

Scanner input = new Scanner(System.in);

System.out.print("Enter first number: ");
aFirst = input.nextInt();
System.out.print("Enter second number: ");
aSecond = input.nextInt();

int sum = aFirst + aSecond;
int difference = aFirst - aSecond;
int product = aFirst * aSecond;
double average = (aFirst + aSecond) / 2.0;
int distance = Math.abs(aFirst) - Math.abs(aSecond);
int minimum = aFirst < aSecond ? aFirst : aSecond;
int maximum = aFirst > aSecond ? aFirst : aSecond;

// output results
System.out.println(sum + "\tSum");
System.out.println(difference + "\tDifference");
System.out.println(product + "\tProduct");
System.out.println(average + "\tAverage");
System.out.println(Math.abs(distance) + "\tDistance");
System.out.println(minimum + "\tMinimum");
System.out.println(maximum + "\tMaximum");

}
}

He told me I am required to write a class with constructors and methods and not with a main method. but not sure how to do this. Any help would be greatly appreciated.
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2011
Location
NY
Posts
4
Rep Power
0
What development environment are you using? I.E BlueJ, NetBeans?

Yes it is possible to write this program using constructors and classes that contain methods for the prompt. The professor just wants you to do it in a particular way.

Code:
```public class Calculation()
{
private int aFirst, aSecond;

public Calculation()
{
// empty constructor
}

public void input()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
aFirst = input.nextInt();
System.out.print("Enter second number: ");
aSecond = input.nextInt();
}
public int getInput1()
{
return aFirst;
}
public int getInput2()
{
return aSecond;
}

}```
Something like that and you could fill the other methods.