Question

In: Computer Science

Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...

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);
}
}

Solutions

Expert Solution

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);
}
}


Related Solutions

Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
import java.io.*; import java.util.*; /** * * * * * Lab Project 9: Rock, Paper, Scissors...
import java.io.*; import java.util.*; /** * * * * * Lab Project 9: Rock, Paper, Scissors * * @authors *** Replace with your names here *** */ public class lab { // global named constant for random number generator static Random gen = new Random(); // global named constants for game choices static final int ROCK = 1; static final int PAPER = 2; static final int SCISSORS = 3; // global names constants for game outcomes static final int...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for Scanner class game{    public static void main(String args[])    {        // generating a random number        Random rand = new Random();        int code = rand.nextInt(99999) + 1, chances = 1, help, turn, i,match, sum;               // for input        Scanner sc = new Scanner(System.in);               // running for 10 times   ...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT