java code Rewrite Assignment using Java Class implementation. Name your class My
ID: 3646739 • Letter: J
Question
java codeRewrite Assignment using Java Class implementation. Name your class MyClass. Create all necessary functions and constructors. Write your own main() program to test and verify that your class works.
Below is your previous Assignment with code :
Write an application program that reads in a list of integer numbers and print out the sum of all numbers, the average, the lowest and the highest value entered. The first input number indicates how many numbers the program is going to read. For example, if the first number entered is 5, then it means five integer numbers will be expected as input. Display your result in an output dialog box.
import java.io.*;
import javax.swing.JOptionPane;
class array
{
public static void main(String args[])
{
String firstNumber;
int i=0,size;
float average=0,sum=0;
firstNumber =
JOptionPane.showInputDialog ( "Enter the number of integers" );
size=Integer.parseInt ( firstNumber);
int a[]=new int[size];
for(i=0;i<size;i++)
a[i]=Integer.parseInt
(JOptionPane.showInputDialog
( "Enter the values" ));
int min=a[0],max=a[0];
for(i=0 ;i<size;i++)
{
sum=sum+a[i];
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
average=(sum/size);
JOptionPane.showMessageDialog
(null,"result: sum"+sum
+" min:"+min+" max:"+max
+" average:"+average);
}
}
Explanation / Answer
import java.io.*;
import javax.swing.JOptionPane;
class MyClass
{
String firstNumber;
int i=0,size;
float average=0,sum=0;
MyClass() //constructor is used
{
firstNumber =
JOptionPane.showInputDialog ( "Enter
the number of integers" );
size=Integer.parseInt ( firstNumber);
}
public int[] read()
{
int a[]=new int[size];
for(i=0;i<size;i++)
a[i]=Integer.parseInt
(JOptionPane.showInputDialog
( "Enter the values" ));
return a;
}
public int[] minMax(int[] a)
{
int min=a[0],max=a[0];
int[] b=new int[3];
for(i=0 ;i<size;i++)
{
sum=sum+a[i];
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
b[0]=min;
b[1]=max;
return b;
}
public void avg(int[] b,int[] a)
{
average=(sum/size);
JOptionPane.showMessageDialog
(null,"result: sum"+sum
+" min:"+b[0]+" max:"+b[1]
+" average:"+average);
}
}
class MyClassDemo
{
public static void main(String args[])
{
MyClass m=new MyClass();
int[] c=m.read();
int [] b=m.minMax(c);
m.avg(b,c);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.