Good day!!
I was observing this class doing their laboratory exercise on java. We are of the same professor that is why I had this instinct that we will be doing the same exercise on our class too.
They were asked to do this:
->Make a class that has the method for accepting infinite input numbers. But when the user enters (zero)”0’, the program stops asking for inputs. No specific range of inputs and should not be stored in an array! Using array is prohibited!
->Your class should also have a method for getting the MAXIMUM and the MINIMUM numbers of the input.
->Also, it should have a method for getting the SUM, the DIFFERENCE and the PRODUCT of the EVEN and ODD NUMBERS of the input.
->Implement this class on your main program.
The output goes like this:
Enter numbers: _ <input “0” to stop>
Maximum number input:
Minimum number input:
Sum of EVEN inputs:
Sum of ODD inputs:
Difference of EVEN inputs:
Difference of ODD inputs:
Product of EVEN inputs:
Product of ODD inputs:
I find it very hard to understand OOPs’ in java. And its too frustrating knowing the fact that I really don’t have any idea on how to implement a class on my main program!
As I was practicing at home, I have made this one… Its totally useless…can you give me a revised form of these…or any ideas about this!!
//My Class
public class Exercise
{
private int input;
private int sum, difference, product, sumE, differenceE, productE;
public Exercise()
{
input=0;
sum=difference=product=sumE=differenceE=productE=0;
}
public Exercise(int s,int d,int p,int sE,int dE,int pE)
{
sum=s;
difference=d;
product=p;
sumE=sE;
differenceE=dE;
productE=pE;
}
public void set(int s,int d,int p,int sE,int dE,int pE)
{
sum=s;
difference=d;
product=p;
sumE=sE;
differenceE=dE;
productE=pE;
}
public int getInput()
{
return input;
}
public int getSum()
{
return sum;
}
public int getSumEven()
{
return sumE;
}
public int getDiff()
{
return difference;
}
public int getDiffEven()
{
return differenceE;
}
public int getProd()
{
return product;
}
public int getProdEven()
{
if(input%2==0)
productE+=input*input;
return productE;
}
public void display
{
int count;
for(int i;i<count;i++)
{
System.out.println(productE);
}
}
}
//My Main Program
import java.io.*;
public class TestExercise
{
public static void main(String []args)
throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Exercise nums=new Exercise();
String str;
do
{
System.out.print("Enter number: ");
str=br.readLine();
nums=Integer.parseInt(str);
}
while(nums!=0);
if(nums==0)
System.exit(0);
System.out.println("SUM OF EVEN: ");
productE.display;
}
}
Thanks for further replies!!
God Bless!!