In: Computer Science
Please convert this java program to a program with methods please.
import
java.io.*;
import java.util.*;
public class Number{
public static
void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement
int[] array = new int[20]; //array of size 20
for(int i=0;i<20;i++){
array[i] = scan.nextInt(); //user input
if(array[i]<-999 || array[i]>999){ //check if value is inside the range
System.out.println("Please enter a number between -999 to
999");
i--;
}
}
// Now we have to create 4 array for odd,even,positive,negative number
int odd = 0;
//initializing as zero
int even = 0;
int positive = 0;
int negative = 0;
for(int i=0;i<20;i++){
if(array[i]%2 == 0){ //check if even
even++;
//increment
}
if(array[i]%2 != 0){ //check if odd
odd++;
//increment
}
if(array[i]>0){ //check if positive
positive++;
//increment
}
if(array[i]<0){ //check if negative
negative++;
//increment
}
}
//Now initializing array for all
int[] oddA =
new int[odd]; //arrays of required size
int[] evenA = new int[even];
int[] positiveA = new int[positive];
int[] negativeA = new int[negative];
int countOdd =
0; //initializing as zero
int countEven = 0; //for array indexing
int countPositive = 0;
int countNegative = 0;
for(int i=0;i<20;i++){
if(array[i]%2 == 0){ //check if even
evenA[countEven] = array[i]; //Putting value
countEven++;
}
if(array[i]%2 != 0){ //check if odd
oddA[countOdd]
= array[i]; //Putting value
countOdd++;
}
if(array[i]>0){ //check if positive
positiveA[countPositive] = array[i]; //Putting value
countPositive++;
}
if(array[i]<0){ //check if negative
negativeA[countNegative] = array[i]; //Putting value
countNegative++;
}
}
//Difference between highest and lowest positive number
int highest =
-1000; //Initialize
int lowest = 1000;
for(int i=0;i<20;i++){
if(array[i]>0 && array[i]>highest){ //condition for highest positive number
highest =
array[i];
}
if(array[i]>0 && array[i]<lowest){ //condition for
lowest positve number
lowest =
array[i];
}
}
int diff = highest - lowest; //difference
//Sum of all negative numbers
int sumNegative = 0;
for(int i=0;i<negative;i++){
sumNegative =
sumNegative + negativeA[i]; //sum of negative number
}
int input = 0;
do{
System.out.println("Enter your choice : ");
System.out.println("1. The orginal array");
System.out.println("2. The array of all odd numbers");
System.out.println("3. The array of all even numbers");
System.out.println("4. The array of all positive numbers");
System.out.println("5. The array of all negative numbers");
System.out.println("6. The highest and lowest postive value and
difference between them");
System.out.println("7. The sum of all negative numbers");
System.out.println("8. Display all positive appearing in positive
array whose value is less than 50");
System.out.println("9. Display all even numbers in reverse
order");
System.out.println("10. Display all multiples of 5 (positive or
negative)");
int choice = scan.nextInt(); //user choice
switch(choice){
case 1 :
for(int i=0;i<20;i++){
System.out.print(array[i]+" ");
}
System.out.println("");
break;
case 2 :
for(int i=0;i<odd;i++){
System.out.print(oddA[i]+" ");
}
System.out.println("");
break;
case 3 :
for(int i=0;i<even;i++){
System.out.print(evenA[i]+" ");
}
System.out.println("");
break;
case 4 :
for(int i=0;i<positive;i++){
System.out.print(positiveA[i]+" ");
}
System.out.println("");
break;
case 5 :
for(int i=0;i<negative;i++){
System.out.print(negativeA[i]+" ");
}
System.out.println("");
break;
case 6 :
System.out.println("The highest positive value is : " +
highest);
System.out.println("The lowest positive value is : " +
lowest);
System.out.println("The difference between the highest and lowest
positve value is : " + diff);
break;
case 7 :
System.out.println("The sum of all negative number is : " +
sumNegative);
break;
case 8 :
for(int i=0;i<positive;i++){
if(positiveA[i]<50){
System.out.print(positiveA[i]+" ");
}
}
System.out.println("");
break;
case 9 :
for(int i=even-1;i>=0;i--){
System.out.print(evenA[i]+" ");
}
System.out.println("");
break;
case 10 :
for(int i=0;i<20;i++){
if(array[i]%5 == 0){
System.out.print(array[i]+" ");
}
}
System.out.println("");
break;
}
System.out.println("Do you wish to continue : (1 or 0) : Press 1 to continue : ");
input = scan.nextInt();
}while(input
== 1);
}
}
Solution:
import java.io.*;
import java.util.*;
public class Main{
public static int odd = 0, even=0, positive = 0, negative=0;
public static int oddA[], evenA[], positiveA[], negativeA[];
public static int countOdd = 0, countEven = 0, countPositive = 0,
countNegative = 0;
public static int highest = -1000, lowest = 1000, sumNegative = 0,
diff=0;
public static int[] array = new int[20];
static void input(){
System.out.println("Enter 20 integers ranging from -999 to 999 :
"); //print statement
Scanner scan=new Scanner(System.in);
for(int i=0;i<20;i++){
array[i] = scan.nextInt(); //user input
if(array[i]<-999 || array[i]>999){ //check if value is
inside the range
System.out.println("Please enter a number between -999 to
999");
i--;
}
}
}
// Now we have to create 4 array for odd,even,positive,negative
number
static void create(){
for(int i=0;i<20;i++){
if(array[i]%2 == 0)
even++;
if(array[i]%2 != 0)
odd++;
if(array[i]>0)
positive++;
if(array[i]<0)
negative++;
}
}
//Difference between highest and lowest positive number
static void diff(){
for(int i=0;i<20;i++){
if(array[i]>0 && array[i]>highest) //condition for
highest positive number
highest = array[i];
if(array[i]>0 && array[i]<lowest) //condition for
lowest positve number
lowest = array[i];
}
diff = highest - lowest; //difference
}
//Now initializing array for all
static void init(){
oddA = new int[odd];
evenA = new int[even];
positiveA = new int[positive];
negativeA = new int[negative];
for(int i=0;i<20;i++){
if(array[i]%2 == 0){ //check if even
evenA[countEven] = array[i]; //Putting value
countEven++;
}
if(array[i]%2 != 0){ //check if odd
oddA[countOdd] = array[i]; //Putting value
countOdd++;
}
if(array[i]>0){ //check if positive
positiveA[countPositive] = array[i]; //Putting value
countPositive++;
}
if(array[i]<0){ //check if negative
negativeA[countNegative] = array[i]; //Putting value
countNegative++;
}
}
for(int i=0;i<negative;i++)
sumNegative = sumNegative + negativeA[i]; //sum of negative
number
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Main obj= new Main();
obj.input();
obj.create();
obj.init();
obj.diff();
int input = 0;
do{
System.out.println("Enter your choice : ");
System.out.println("1. The orginal array");
System.out.println("2. The array of all odd numbers");
System.out.println("3. The array of all even numbers");
System.out.println("4. The array of all positive numbers");
System.out.println("5. The array of all negative numbers");
System.out.println("6. The highest and lowest postive value and
difference between them");
System.out.println("7. The sum of all negative numbers");
System.out.println("8. Display all positive appearing in positive
array whose value is less than 50");
System.out.println("9. Display all even numbers in reverse
order");
System.out.println("10. Display all multiples of 5 (positive or
negative)");
int choice = scan.nextInt(); //user choice
switch(choice){
case 1 :
for(int i=0;i<20;i++){
System.out.print(array[i]+" ");
}
System.out.println("");
break;
case 2 :
for(int i=0;i<odd;i++){
System.out.print(oddA[i]+" ");
}
System.out.println("");
break;
case 3 :
for(int i=0;i<even;i++){
System.out.print(evenA[i]+" ");
}
System.out.println("");
break;
case 4 :
for(int i=0;i<positive;i++){
System.out.print(positiveA[i]+" ");
}
System.out.println("");
break;
case 5 :
for(int i=0;i<negative;i++){
System.out.print(negativeA[i]+" ");
}
System.out.println("");
break;
case 6 :
System.out.println("The highest positive value is : " +
highest);
System.out.println("The lowest positive value is : " +
lowest);
System.out.println("The difference between the highest and lowest
positve value is : " + diff);
break;
case 7 :
System.out.println("The sum of all negative number is : " +
sumNegative);
break;
case 8 :
for(int i=0;i<positive;i++){
if(positiveA[i]<50){
System.out.print(positiveA[i]+" ");
}
}
System.out.println("");
break;
case 9 :
for(int i=even-1;i>=0;i--){
System.out.print(evenA[i]+" ");
}
System.out.println("");
break;
case 10 :
for(int i=0;i<20;i++){
if(array[i]%5 == 0)
System.out.print(array[i]+" ");
}
System.out.println("");
break;
}
System.out.println("Do you wish to continue : (1 or 0) : Press 1 to
continue : ");
input = scan.nextInt();
}while(input == 1);
}
}