Question

In: Computer Science

public class Main { public static void main(String [] args) { int [] array1 = {5,...

public class Main
{
public static void main(String [] args)
{
int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65};
int numElements = 10;

System.out.println("Part 1");
// Part 1
// Enter the statement to print the numbers in index 5 and index 8
// put a space in between the two numbers and a new line at the end


// Enter the statement to print the numbers 8 and 53 from the array above
// put a space in betwaeen the two numbers and a new line at the end.


// Enter the statement to change the number 2 in the array to be 12
// then write the statement to print out that number in the array
// and a new line at the end

System.out.println("Part 2");
// Part 2
// Write the loop to print out all of the numbers in the array
// Use the variables declared above (you might have to declare
// another variable) Put a space between each number


System.out.println("\nPart 3");
// Part 3
// Write the loop to print out all of the EVEN numbers in the array
// Use the variables declared above (you might have to declare
// another variable) Put a space between each number.   

System.out.println("\nPart 4");
// Part 4
// Write a method called computeTotal that has the array
// and the number of elements passed into it. It will return
// the total of all of the numbers in the array (it will be
// below the main method)
int total;
// Call the function you just wrote and store the answer
// in the variable total declared above

// This will print the total out
System.out.println(total);

}

}

Solutions

Expert Solution

Source code of the completed program is given below.Detailed comments are included for better understanding the code.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Source code

public class Main {
    public static void main(String[] args) {
        int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65};
        int numElements = 10;
        System.out.println("Part 1");
        //statement to print the numbers in index 5 and index 8
        System.out.println(array1[5]+" "+array1[8]);
        System.out.println();
        //statement to print the numbers 8 and 53
        System.out.println(array1[1]+" "+array1[6]);
        System.out.println();
        //statement to change the number 2 in the array to 12
        array1[4]=12;
        //printout the number
        System.out.println(array1[4]);
        System.out.println();
        System.out.println("Part 2");
        //loop to print all the elements of the array
        for(int i=0;i<numElements;i++)
        {
            System.out.print(array1[i]+" ");
        }
        System.out.println("\nPart 3");
        //loop to print out all the even numbers in the array
        for(int i=0;i<numElements;i++)
        {
            //if the remainder obtain when array element divided by 2 is 0
            //then the number is even.% gives remainder after division
            if(array1[i]%2==0)
                System.out.print(array1[i]+" ");
        }
        System.out.println("\nPart 4");
        //declaring integer variable total
        int total;
        //calling the computeTotal() method
        total=computeTotal(array1,numElements);
        //displaying total
        System.out.println(total);
    }
    static int computeTotal(int arr[],int n)
    {
        //declare an integer variable sum and initialize it with 0
        int sum=0;
        //loop iterate through all elements
        for(int i=0;i<n;i++)
            //add each element to sum
            sum=sum+arr[i];
        //return the result sum
        return sum;
    }
}

Screen shot of the code

Screen shot of the output


Related Solutions

---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT