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 8737025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:25:34+00:00 2026-06-13T10:25:34+00:00

Basically I want the program to take input A or L to instantiate different

  • 0

Basically I want the program to take input A or L to instantiate different classes. I have the construct in place for taking in those variables and such, I just need to know how to actually create a condition or switch that will initialize a class given the input. Code is as follows:

public constructor(char choice){
    if(choice == 'A'){
        classa<String> baga = new classa<String>();
        classa<String> bagb= new classa<String>();
    }
    else if(choice== 'L'){
        classb<String>  baga = new classb<String>();
        classb<String> bagb = new classb<String>();
    }
}
  • 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-13T10:25:35+00:00Added an answer on June 13, 2026 at 10:25 am

    If you want these variables to have different types, and you still expect to get some use out of them, then they must have some common operations. Those operations can be factored into a Bag interface:

    public ConsonantCounter(char bagImplementationClass){
        Bag vowels;
        Bag consonants;
        if (bagImplementationClass == 'A'){
            vowels = new ResizableArrayBag<String>();
            consonants = new ResizableArrayBag<String>();
        } else if (bagImplementationClass == 'L'){
            vowels = new LinkedListBag<String>();
            consonants = new LinkedListBag<String>();
        }
        // ...
    }
    

    Incidentally, a char is not really type-safe here; I would use an enumeration. Further, I don’t like the use of mutation, so perhaps I’d go for something more along these lines:

    final Bag vowels = makeLetterBag(type);
    final Bag consonants = makeLetterBag(type);
    
    private static IBag makeLetterBag(BagType type) {
        switch (type) {
        case RESIZABLE:
            return new ResizableArrayBag<String>();
        case LINKED_LIST:
            return new LinkedListBag<String>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I want to have my program retrieve various variables from the hard drive
As the title implies basically: Say we have a complex program and we want
Basically I want to make simple toggle program (that will be mapped to some
I want to redirect the output of some Windows program directly to gvim basically
I basically want the same functionality as facebook, twitter and all those other infinite
I have a third party program which basically allows users to send email and
So I basically want to check some information for my project. I have GTX
In my program I want to draw a simple score line graph. I have
I have to make a program that basically reads x lines from a file
I have a program which I want to store information in a string. I

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.