NOTE: Solutions to the problems are meant to be used as a learning aid. Simply copying and pasting the material will not result in your learning how to program.
Problem 1 Solution - So that you may learn by this first example the code has been heavily commented.
Class EnterNum:
import java.util.*; // import the util library so we can create a Scanner object
public class EnterNum
{
Scanner in = new Scanner(System.in);
private int enteredNum; // declare a field variable named enteredNum
// constructor that sets the field variable named enteredNum to 0
public EnterNum()
{
enteredNum = 0;
}
// method that asks for an integer to be entered and then
// the value entered is placed in the field variable named enteredNum
public void setEnteredNum()
{
System.out.print("Enter an integer: ");
enteredNum=in.nextInt();
}
// a method that is used to get the current value of the field variable named enteredNum
public int getEnteredNum()
{
return enteredNum; // send the value of enteredNum to where it is being requested
}
}
Class Multiply:
public class Multiply
{
private int product; // declare a field variable named product
// constructor that sets the field variable named product to 0
public Multiply()
{
product=0;
}
// this method receives two integer values passed as parameters
// the values are multiplied together and then the value is
// placed in the field variable named product
public void setProduct(int n1, int n2)
{
product = n1*n2;
}
// this method sends the value in the field variable named product to the place that requested it
public int getProduct()
{
return product;
}
}
Class Add:
public class Add
{
private int sum; // declare a field variable named sum
// constructor that sets the field variable named sum to 0
public Add()
{
sum=0;
}
// this method receives two integer values passed as parameters
// the values are added together and then the value is
// placed in the field variable named sum
public void setSum(int n1, int n2)
{
sum = n1+n2;
}
// this method sends the value in the field variable named sum to the place that requested it
public int getSum()
{
return sum;
}
}
Class Problem1:
public class Problem1
{
static EnterNum numGetter = new EnterNum(); // create an instance of the EnterNum Class named numGetter
static Multiply multiplier = new Multiply(); // create an instance of the Multiply Class named multiplier
static Add adder = new Add(); // create an instance of the Add Class named adder
// this is the main method
public static void main (String[] args)
{
int num1,num2,theSum,theProduct; // declare the local variable that will be used
numGetter.setEnteredNum(); //the numGetter object executes its askForNumber() method
num1=numGetter.getEnteredNum();//the numGetter object executes its getNumber() method and places the value in num1
numGetter.setEnteredNum(); //the numGetter object executes its askForNumber() method
num2=numGetter.getEnteredNum();//the numGetter object execute its getNumber() method and places the value in num2
multiplier.setProduct(num1,num2);//the multiplier object executes its multiplyNums() method sending along the values in num1 & num2
theProduct=multiplier.getProduct();//the multiplier object executes its getProduct() method and places the result in theProduct
adder.setSum(num1,num2);//the adder object executes its addNums() method sending along the values in num1 & num2
theSum=adder.getSum();//the adder object executes its getSum() method and places the result in the variable theSum
System.out.println(num1+" * "+num2+" = "+theProduct);// display the output on the computer screen
System.out.println(num1+" + "+num2+" = "+theSum);// display the output on the computer screen
}
}