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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:46:31+00:00 2026-06-11T17:46:31+00:00

I am taking my first semester of Java programming, and we’ve just covered the

  • 0

I am taking my first semester of Java programming, and we’ve just covered the conditional operator (? 🙂 conditions. I have two questions which seem to be wanting me to “nest” conditional operators within eachother, something that I could easily (yet tediously) do with if-else-if statements.

1) “Assume that month is an int variable whose value is 1 or 2 or 3 or 5 … or 11 or 12. Write an expression whose value is “jan” or “feb” or “mar” or “apr” or “may” or “jun” or “jul” or “aug” or “sep” or “oct” or “nov” or “dec” based on the value of month. (So, if the value of month were 4 then the value of the expression would be “apr”.).”

an idea I had looks something like this:

(month==1)?"jan":(month==2)?"feb": (month==3)?"mar": (month==4)?"apr": 
(month==5)?"may":(month==6)?"jun": (month==7)?"jul":(month==8)?"aug":
(month==9)?"sep": (month==10)?"oct": (month==11)?"nov": (month==12)?"dec":

(I know this isn’t a complete expression, but I’m not sure how to phrase the operator to handle so many conditions.)

2) Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is “freshman” or “sophomore” or “junior” or “senior” based on the value of credits. In particular: if the value of credits is less than 30 the expression’s value is “freshman”; 30-59 would be a “sophomore”, 60-89 would be “junior” and 90 or more would be a “senior”.

again, I’ve been toying around and the best I can come up with is something like(and I’m probs missing some necessary parentheses):

credits < 30 ? "freshman": credits >= 30 && <=59 ?
 "sophomore": credits >= 60 && <= 89 ? "junior": "senior"

I’ve Googled around and checked the database here, but I don’t THINK that there’s anything exactly like this question; forgive me if I’m wrong. The program (CodeLab) won’t take Switch-Case or the if-else-if solution, always suggesting I should be using the conditional ? : operator, but everywhere I’ve looked I haven’t figured out how to rig the operator to handle so many conditions. We aren’t far past this in the book, so if you guys could help me find a solution, it’d be great if it’s one that jives with the little bit I’ve learned so far.

  • 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-11T17:46:32+00:00Added an answer on June 11, 2026 at 5:46 pm

    For the first question, you can indeed use the ternary operator, but a simpler solution would be to use a String[] with the month descriptions, and then subscript this array:

    String[] months = { "jan", "feb", "mar", ... };
    int month = 1; // jan
    String monthDescription = months[month - 1]; // arrays are 0-indexed
    

    Now, for your second question, the ternary operator seems more appropriate since you have fewer conditions, although an if would be much easier to read, imho:

    String year = "senior";
    if (credits < 30) {
      year = "freshman";
    } else if (credits <= 59) {
      year = "sophomore";
    } else if (credits <= 89) {
      year = "junior";
    }
    

    Contrast this with the ternary operator:

    String year = credits < 30 ? "freshman" : credits <= 59 ? "sophomore" : credits <= 89 ? "junior" : "senior";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm taking my first steps in python programming, I have a class named node
I'm a new student to programming and taking my first introduction to Java college
I'm just taking my first steps with Azure and the first thing I see
I am taking my first steps in C++ having a good background in Java.
First, thanks in advance for taking the time to read through this. I have
I'm taking a first look at Monk and the Ohm/Redis APIs and I have
I'm taking my first steps in PhoneGap with Android (How come you have to
I am taking first steps in socket programming as an added complexity I am
I am taking my first steps in the world of parallel programming with Open
I can't figure out why my code just taking the first tag and not

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.