use ternary operator to solve multiple conditions [closed] - ternary-operator

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can anyone please help me to solve this question. it doesn't work.

Take a look here:
class Question05
{
public static void main(String[] args)
{
double gpa = Double.parseDouble(args[0]);
String res = gpa >= 3.6?"First class Hons":(gpa<3.6 && gpa>=3.4?"Upper Second Class Hons": (gpa<3.4 && gpa>=3.0?"Lower Second Class Hons": (gpa<3.0 && gpa>=2.0?"Pass":"you have failed")));
System.out.println(res);
}
}
Edit: #veena, you were trying to assign a string to gpa, which was declared as a double!!!

I had this question before, and there are a few ways, depending on what you mean with the question...
As it does appear to be a class assignment, I will give my own examples rather than fix yours.
Scenario 1:
If you would like to have multiple passing conditions, consider the following...
Surround the conditions in parenthesis, just to be safe, and use logical separators.
eg:
(1==1 && 2==2)?"yay":"sadness"
https://jsfiddle.net/o4nu3ya5/
Scenario 2:
If you want to have conditions verified based on previous condintions verified, consider the following...
Just place a ternary within a ternary, possibly called a nested ternary??
eg:
1==1?2==2?"sweet":"almost":"not close"
https://jsfiddle.net/o4nu3ya5/1/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
Ternary are super fun. Get use to them, though understand, they will frustrate a lot of fellow developers as they are harder to read.

Related

Understanding void functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Write a program named program52.py that uses main and a void function named numbers that takes no arguments and does not return anything. The numbers function generates 5 random integers, each greater than 10 and less than 30 (duplicates are okay), and prints them all on one line separated by spaces. A loop is required for this, and the loop should also total up the integers so the sum can be displayed when the loop ends. The main function should call the numbers function.
This is the requirements for them, I'm very new and I've had a great deal of trouble using void functions so as you might imagine this has been difficult. I was wondering if anyone had insight on how to do this, I would not like the solution I want to understand how to do it properly.
In python, you don't need to define something to return for your function. It is by default void unless you add a return statement.
Refer to these for how to make functions:
https://www.w3schools.com/python/python_functions.asp, https://realpython.com/python-main-function/
Additionnal sources to help in your work:
https://www.w3schools.com/python/python_for_loops.asp,
https://www.techbeamers.com/python-random-number-tutorial/
You can quickly find answers to this type of question by looking it up on many websites like: W3Schools, GeeksForGeeks, RealPython and many others.

Searching for simple problems naturally solved using stacks [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to know about simple problems that can be naturally solved using stacks with the usual interface (emptyS, isEmptyS, push, pop, top).
The complexity asociated to the context of the problem should be null. I can't touch topics like parsing, compiling or search algorithms at this moment. This discards many classical examples.
The most beautiful example I found so far is checking for balanced parenthesis in strings. In very few lines, without any other background, the exercise shows the utility of the data structure:
Another good example is procesing a string where the asterisk means to pop an item from the stack and a letter means to push it into the stack. The function must return the stack after the operations described in the string are applied to an empty stack.
If you can share some others problems, I will apreciate it very much.
Thank you in advance.
Though this question is too broad, I am going to give some other applications. Some of other common applications are -
Parsing
Recursive Function
Calling Function
Expression Evaluation
Expression Conversion
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
Towers of Hanoi
Some details can be found here.

Simple String error, String breaking from ' in var [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
the parsing of user name has a ' inside of the user name
and i think that is causing the code to break
when i set it with this
tempUsername=Request.Form("UserName")
if (Request.Form("Action") = "Login") then
tempUsername=Request.Form("UserName")
tempPassword=Request.Form("UserPassword")
is that assumption right?
if so what is a solution to this?
Jumping onto the comment by James, as well as answering this question:
Input sanitization is an issue in every language. Even if there weren't ' characters in usernames, this code is danger++
At the very least, run all the data you get from Request.form through a function that escapes/sanitizes dangerous characters in the context of what the data is getting passed on to (such as data stores or dir-resolving code).
As for the code using <%, that's a sign this is an ASP script, and the syntax looks like it's VB. The (Request.Form("Action") = "Login") in particular is a dead give-away, because no sane programming language since the 80s uses "=" as an equality testing operator =)

Propose how to name a "get/create & get" function [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am working on a function that returns an object from a container (e.g an Array) by value (e.g by name). If the object doesn't already exist, then a default copy of the object (default constructor) is created and returned.
That way, this function ALWAYS returns an object, regardless if the object existed prior to calling the function.
Now my question is this: how to name this function? GetOrCreateThenGet sounds stupid. Any ideas?
Edit : I feel something like this would be closer to the function's essence: GetForSure...
The best suitable name for the function would be "Provide" like if you provide goods you can ship it from a warehouse or make/buy a new one and then deliver.
Why do you need anything other than getXXX(). Any method that has and/or means that it is doing to much and is mixing concerns and seriously breaking the Single Responsibility Principle.
Should the caller actually care if the object is being created on demand if all they need from the contract is to get the object?
getOrCreate implies you will get the object or it will be created but not returned.
Again why would I care about the create part?
If I really am supposed to care it would be getOrCreateAndGet to be semantically correct, but highly unorthodox and prone to discussions with peers about methods doing to many things and mixing concerns and breaking the Single Responsiblity Principle? Just because other people name things whacky and non-sensical isn't a good excuse to do it wrong also.
GetGuaranteed seems to cover all the caller needs to know...

how to write mockito test for action classes(struts2) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
`
Blockquote
How to write test class to test action classes using mockito?
Are you using JUnit? Think about having one test method for each requirement that your class has to meet, or each particular scenario of a requirement. Once you've planned out what each test method will be, work out
what you need to set up in each test method,
what the actual call to your class will look like within the test method,
what you need to verify at the end of the test method.
Use Mockito when any of your test methods are likely to use functionality outside of the class that you're actually trying to test. Mocking is all about removing the behaviour of other classes (collaborator classes) from what happens during your test. Once you've identified what these other classes are, and what behaviour you want from them, then you can start using Mockito.
But you really need to plan out your tests first. Mockito is not a silver bullet that will test things for you.
I'm sorry, but I really can't give a more detailed answer than this, unless you would care to add some detail to your question. Right now, your question is remarkably vague.

Resources