menu search
brightness_auto
Ask or Answer anything Anonymously! No sign-up is needed!
more_vert

Can someone Help me with this??

 image

4 Answers

more_vert
import java.util.ArrayList; 

import java.util.Stack; 

public class StackManipulation { 

     public static void main(String[] args) { 

           // Create an ArrayList of ten elements 

           ArrayList<Integer> list = new ArrayList<>(); 

           list.add(1); 

           list.add(2); 

           list.add(3); 

           list.add(4); 

           list.add(5); 

           list.add(6); 

           list.add(7); 

           list.add(8); 

           list.add(9); 

           list.add(10); 

           // Pass the elements of the ArrayList to an array 

           int[] array = new int[list.size()]; 

           for (int i = 0; i < list.size(); i++) { 

                  array[i] = list.get(i); 

           }

 

          // Pass the elements of the array to a Stack and manipulate the elements using three Stack methods

          Stack<Integer> stack = new Stack<>(); 

          for (int i : array) { 

              stack.push(i); 

          } 

          stack.pop(); 

          stack.push(11); 

          stack.peek(); 

         // Iteratively display the elements of the Stack 

         while (!stack.isEmpty()) { 

            System.out.println(stack.pop()); 

         } 

      } 

}
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
Create a package called "operations" by adding the following line at the top of your Java file:

Copy code

package operations;

Create a class called "Calculator" that contains methods for performing basic arithmetic operations. For example:

Copy code

public class Calculator {

  public static int add(int a, int b) {

    return a + b;

  }

  public static int subtract(int a, int b) {

    return a - b;

  }

  public static int multiply(int a, int b) {

    return a * b;

  }

  public static int divide(int a, int b) {

    return a / b;

  }

}

Create a class called "StringUtils" that contains methods for manipulating strings. For example:

Copy code

public class StringUtils {

  public static String reverse(String s) {

    return new StringBuilder(s).reverse().toString();

  }

  public static int countVowels(String s) {

    int count = 0;

    for (char c : s.toCharArray()) {

      if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {

        count++;

      }

    }

    return count;

  }

}

To use these classes in another Java file, you can import the package and call the methods like this:

Copy code

import operations.*;

public class Main {

  public static void main(String[] args) {

    int result = Calculator.add(5, 7);

    System.out.println(result); // prints 12

    String reversed = StringUtils.reverse("Hello World");

    System.out.println(reversed); // prints "dlroW olleH"

  }

}

This is just one way to structure a Java package with classes that perform various operations. You can add more classes and methods to the package as needed to meet the specific requirements of your project.
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
Here is an example Java package structure with classes that perform the following operations:

package com.example.mathoperations;

public class Addition {

    public static int add(int a, int b) {

        return a + b;

    }

}

public class Subtraction {

    public static int subtract(int a, int b) {

        return a - b;

    }

}

public class Multiplication {

    public static int multiply(int a, int b) {

        return a * b;

    }

}

public class Division {

    public static int divide(int a, int b) {

        return a / b;

    }

}
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
more_vert
Copy code

package operations;

Create a class called "Calculator" that contains methods for performing basic arithmetic operations. For example:

Copy code

public class Calculator {

  public static int add(int a, int b) {

    return a + b;

  }

  public static int subtract(int a, int b) {

    return a - b;

  }
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

Related questions

Welcome to Answeree, where you can ask questions and receive answers from other members of the community.
...