Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:11:52+00:00 2026-06-14T20:11:52+00:00

I need the following code to have a default constructor of BalancedString that initializes

  • 0

I need the following code to have a default constructor of BalancedString that initializes str to the empty string and resets a counter to 0 The class’s one arguement constructor passes a string s to str and resets counter to zero. The BalancedString class also provides a boolean method which is boolean balanced() that returns true if a string contains a balanced amount of parenthesis

import java.util.*;
public class BalancedString {
    private static String str;


    public BalancedString()
    {
        str = "";

    }

    public BalancedString(String s)
    {
        s = "";
        str = s;

}



public boolean balanced(){

    return true;

}
public static void main(String[] args) {
    int n = 0;
    CounterFancy.setCounter(n);
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a string that has any number of Left and right Parenthesis");
    String s = input.next();


        if (s.indexOf('(') != -1)

            CounterFancy.incCounter();

        if (s.indexOf(')') != -1)

            CounterFancy.decCounter();


    int counterValue = CounterFancy.getCounter();
    if (counterValue == 0)
        System.out.println("The string is Balanced");
    else 
        System.out.println("The string is NOT Balanced");
    input.close();
}

public String getStr()
{
    return str;
}
public String setStr(String s)
{
    str = s;
    return str;
}

}

AND the following is the other project that i got the CounterFancy classes from, but the problem is above^^ why is this only outputing that it is balanced

//Joe D'Angelo
//CSC 131-03
//Chapter 10 Programming Assignment 5a.
//Takes the user's input of whether they want the counter to be negative or positive and outputs
//10 values of the user's selected input, then restarts the counter at 0
import java.util.*;
public class CounterFancy { //I messed up the first time and had to change FancyCounter to CounterFancy that is why this is changed

    private static int counter;

    public CounterFancy()
    {
        counter = 0;        
    }

    public  CounterFancy(int n){
        counter = n;
    }

    public static int incCounter() //inc stands for increment
    {
            counter++;
        return counter;
    }
    public static int decCounter() //dec stands for decrement
    {
        counter--;
        return counter;
    }

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Press 1 for Possitive or Press 2 for Negative");
        int reply = input.nextInt();

        if (reply == 1)
        {
        for (int i = 1; i <=10; i ++)
        System.out.println("counter: " + CounterFancy.incCounter());
        CounterFancy.setCounter(5);
        System.out.println("Counter: " + CounterFancy.getCounter());

        }

        if (reply == 2)
        {
            for (int i = 1; i <=10; i ++)
                System.out.println("counter: " + CounterFancy.decCounter());
            CounterFancy.setCounter(5);
            System.out.println("Counter: " + CounterFancy.getCounter());

        }
        input.close();
        }



    public static int getCounter()
    {
        return counter;
    }

    public static void setCounter(int n)
    {
        counter = 0;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:11:53+00:00Added an answer on June 14, 2026 at 8:11 pm

    You are making a couple of mistakes in your BalancedString class definition. First, the str field should not be static. By making it static, all instances share the same str field.

    Second, and perhaps more critical, you are not constructing your BalancedString properly. You are setting the argument back to the empty string every time!

    public BalancedString(String s) {
            s = ""; // THIS LINE SHOULD NOT BE HERE!
            str = s;
    }
    

    Finally, your balanced() method is simply returning true regardless of the string. You need to implement some logic here.

    Regarding the main program: you need to loop through all the characters, increment for each '(' and decrement for each ')' character. Instead of this:

    if (s.indexOf('(') != -1)
    
            CounterFancy.incCounter();
    
    if (s.indexOf(')') != -1)
    
        CounterFancy.decCounter();
    

    You should have a loop like this:

    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (c == '(')
            CounterFancy.incCounter();
        else if (c == ')')
            CounterFancy.decCounter();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to turn off a default hover state. I have the following code.
I have following code which works for radio buttons but need to be changed
I need syntax help with the following code logic: I have a code block
i have the following code ..i need to loop through end of the file
I have the following code. I need to create list separators before A elements,
I need to have a checkbox which ajax-submits a form. The following code throws
I need to sort nodes in xml. I have the following code which successfully
I have the following FORTRAN code which I need to convert to C or
I have been sharing database variables using the following code: Namespace DataAccessVariables Public Class
So I have the following code that I wanted to make a little more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.