Question

In: Computer Science

(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have...

(JAVA)

Balanced

Class diagram:

Balanced

+ swing(int[] a) : boolean


For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us if an array is balanced or not.

We say that an array is balanced if the sum of the elements belonging to the first half of the array (not including the element in the middle), and the sum of the elements belonging to the second half of the array (not including the element in the middle) They are equal.

Example 1: the balance method receives the array [1,2,3,4,2]

The method returns false

Example 2: the balance method receives the array [1,2,78,3,0]

The method returns true

TIP: for this exercise, ONLY odd arrangements will be made.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================


public class Balanced {


    public boolean swing(int[] a) {

        if (a == null) return false;
        if (a.length <= 1) return true;
        
        int front =0;
        int rear = a.length-1;
        int firstHalfSum = 0, secondHalfSum = 0;

        while ((rear-front)!=1 &&(rear-front)!=0){
            firstHalfSum+= a[front++];
            secondHalfSum+=a[rear--];
        }

        return firstHalfSum==secondHalfSum;

    }

    public static void main(String[] args) {

        Balanced balanced = new Balanced();

        System.out.println("balanced.swing(new int[]{1,2,3,4,2}) = " + balanced.swing(new int[]{1, 2, 3, 4, 2}));
        System.out.println("balanced.swing(new int[]{1,2,7,8,3,0}) = " + balanced.swing(new int[]{1, 2, 7, 8, 3, 0}));
        System.out.println("balanced.swing(new int[]{1,2,3,4,5,1,1112,5,4,3,2,1}) = " + balanced.swing(new int[]{1,2,3,4,5,1,1112,5,4,3,2,1}));
    }


}

====================================================================


Related Solutions

(JAVA) Repeated Class diagram: Repeated + repeatedDetector(ArrayList<Integer> a) : int For this exercise, you will have...
(JAVA) Repeated Class diagram: Repeated + repeatedDetector(ArrayList<Integer> a) : int For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us the amount of "repeated" elements that are in the dynamic array. When we speak of "repeated", we mean counting the number of numbers of the same value that are found in succession. Example 1: The...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
In Java The Order class should have:  Seven instance variables: the order number (an int),...
In Java The Order class should have:  Seven instance variables: the order number (an int), the Customer who made the order, the Restaurant that receives the order, the FoodApp through which the order was placed, a list of food items ordered (stored as an array of FoodItem), the total number of items ordered (an int), and the total price of the order (a double).  A class constant to set the maximum number of items that can be ordered...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the...
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrays are equals(same values in the same indices), false otherwise if(arr1.length != arr2.length){ return false; } for(int i = 0; i < arr1.length; i++){ if(arr1[i] != arr2[i]){ } } return true; } public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
You have a class named AirConditioner that has a private boolean field named on. It has...
You have a class named AirConditioner that has a private boolean field named on. It has method named turnOn that sets the field to true and one named turnOff that set it to false. It also has a method named isOn that returns the value of the on field. Fill in the blank to complete the turnOff method. public void turnOff() { ______________________ }
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS FILE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT