Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hello fellow developers.
I am suppose to write a code that stores patient details and calculates some details like ratio. HDL(High Density Lipoprotein) and LDL are cholesterol figures. Please correct where ever you can.
I have a default constructor that assigns default values. And an overload constructor that has three parameters.
public PatientCheckUp()
{
PatientNumber = "L123";
HDL = 60.00;
LDL = 300;
}
public PatientCheckUp(String PatientNumber, double HDL,double LDL)
{
this.PatientNumber = PatientNumber;
this.HDL = HDL;
this.LDL = LDL;
}
`
I already included the get and set methods, I understand those.
What i don't know how to do is
Create a method called getCholesterolStatus() that will invoke a method i created to compute cholesterol ratio ( computeRatio() )
This method must compare the patient's cholesterol ratio to that of the optimum cholesterol ratio and return a messaged based on the comparison. If the ratio is less than the ideal cholesterol the a "optimal cholesterol ratio" message should be returned(3.33)
The computeRatio() method
public double computeRatio()
{
ratio = LDL / HDL;
return ratio;
}
I realize i have asked for a lot. I need to understand how to do this for my upcoming examination.
Thank you in advance.
You call a method by typing its name and () at the end of it, possibly passing parameters and an instance variable's name before it with a dot or class name ahead of it with a dot if necessary. If you call a method correctly, then you can use it simply as a value, compare it to a final variable and you can also use the ternary operator to make this a one-liner.
public String getCholesterolStatus() {
return (IDEAL_CHOLESTEROL <= computeRatio()) ? "Optimal cholesterol ratio" : "Bad cholesterol ratio";
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
I am in my first semester of a Java Course. I am struggling to understand how I put exclusiveness on a statement. I am writing a Class for an Object Couch. I have tried to build a well formed class, but for the outcome from my main, in the console it must only have 4 or 8 legs on the couch. There is no user input as I am hard coding the variables, but I want to be sure that if I hard code for 5 legs it will stop me or an error message will pop up. Any suggestions?
public void setNbrLegs(int nbrLegs){
if ((nbrLegs == 2) || (nbrLegs == 4)){
this.nbrLegs = nbrLegs;
}
}
I tried putting an "else" with a message that that number is bad, but what is did was bypass my error message and just insert the incorrect number ofLegs as 5.
Consider looking for the opposite: a condition where you must fail. From there, you can use runtime exceptions to ensure a few things:
The invalid state is not applied
A developer passing this invalid state will get an exception, and have a clear reason to fix their code
You no longer have to worry about invalid state further on in the method (i.e. legs will only be 2 or 4 further on).
In doing so, your method may end up looking like this:
public void setNbrLegs(int legs) {
if (legs != 4 && legs != 2) {
throw new IllegalArgumentException("Can only have 2 or 4 legs");
}
this.nbrLegs = legs;
}
This preconditional checking is also good to do early in your methods (fast-fail), as it will prevent excess work being done for a method that will only "fail".
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am currently practicing with my coding skills and one of the challenges I ran into was finding the first duplicate value produced in an array. I had solve the challenge, but not in a timely manner. I looked at the solution for the problem and ran into this solution and wanted help in understanding how it exactly works. I have the solution commented in the areas that I understand and what exactly is the purpose of that block of code, but I would like help in understanding the if-statement why it works the way it does.
int firstDuplicate(int[] a) {
for(int i=0;i<a.length;i++)
//Checks ???????????
if(a[Math.abs(a[i])-1]<0)
return Math.abs(a[i]);
//Make the checked value negative
else{
a[Math.abs(a[i])-1]=-a[Math.abs(a[i])-1];
}
//If there are no duplicates it returns -1
return -1;
}
Constraints:
1 ≤ a.length ≤ 105,
1 ≤ a[i] ≤ a.length.
Welcome to SO. I will not give you the exact answer but instead provide you with tools for you to figure it out.
In order for you to figure out this code, you need to debug it. Here are some ways you could go about it.
Set a breakpoint just prior to calling the function (look up how to do this for your IDE), thereafter step into your code line-by-line.
You could use a combination of temporary variables and System.out.println() statements. Looking into your code, break it down into modular bits that you can track. For instance you could re-write the expression inside the if statement as
int currentElementAbsolute = Math.abs(a[i]);
System.out.println(currentElementAbsolute);
int difference = currentElementAbsolute - 1;
System.out.println(difference);
int element = a[difference]
System.out.println(element);
if (element < 0)
{
return Math.abs(a[i]);
}
As you can see, for each operation/line, I print out the current value thus keeping track of events. Practice this and re-write the else part
PS: Upon completion you will realise this method fails to capture all duplicates when certain type of numbers are used. Happy Hunting :)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The normal code works below
ShipSize size = shipSize.startsWith("s") ? ShipSize.SMALL:
(shipSize.startsWith("m") ? ShipSize.MEDIUM: ShipSize.LARGE); // get ship size
Is this the right way to change the code so the string equals a word. I doesn't seem to be working for me.
ShipSize size = shipSize.equals("small") ? ShipSize.SMALL:
(shipSize.equals("medium") ? ShipSize.MEDIUM: ShipSize.LARGE); // get ship size
Any ideas why?
Equals is case sensitive, depending on your input, so you may need to use .equalsIgnoreCase() instead of .equals()
Other than that, you haven't done anything "wrong". You'll need to specify exactly what your input is for more detail.
ShipSize size = "small".equalsIgnoreCase(shipSize.name) ? ShipSize.SMALL: ("medium".equalsIgnoreCase(shipSize.name) ? ShipSize.MEDIUM: ShipSize.LARGE); // get ship size
Assuming ShipSize is an enum, then you can use the .name of the enum to get the String. Also, put the constants first when comparing to avoid possible null pointer exceptions.
EDIT: changed to equalsIgnoreCase.
Note that this assumes that your inputs are actually "small", "medium" or "large" and that your enum is:
public enum ShipSize
{
SMALL,
MEDIUM,
LARGE,
;
}
If this is the case, you can just do:
final ShipSize size = ShipSize.valueOf(shipSize.toUpperCase());
.valueOf() will throw an IllegalArgumentException if the constant does not exist, so if the risk exists you should catch that and recover appropriately.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I try solve some problem in the arena, but now I can not find it there. I just have a text of solution:
DEFINITION
Class Name: MatchMaker
Method Name: getBestMatches
Paramaters: String[], String, int
Returns: String[]
Method signature (be sure your method is public):
String[] getBestMatches(String[] members, String currentUser, int sf);
PROBLEM STATEMENT
A new online match making company needs some software to help find the “perfect
couples”. People who sign up answer a series of multiple-choice questions.
Then, when a member makes a “Get Best Mates” request, the software returns a
list of users whose gender matches the requested gender and whose answers to
the questions were equal to or greater than a similarity factor when compared
to the user’s answers.
Implement a class MatchMaker, which contains a method getBestMatches. The
method takes as parameters a String[] members, String currentUser, and an
int sf. Here members contains information about all the members. Elements of members are of the form
NAME G D X X X X X X X X X X
NAME represents the member’s name
G represents the gender of the current user.
D represents the requested gender of the potential mate.
Each X indicates the member’s answer to one of the multiple-choice
questions. The first X is the answer to the first question, the second is the
answer to the second question, et cetera.
currentUser is the name of the user who made the “Get Best Mates” request.
sf is an integer representing the similarity factor.
Can you help me, and tell how to find a solution in the TopCoder arena?
use the problem archive to find out which SRM or tournament the class was used in.
http://community.topcoder.com/tc?module=ProblemArchive
this will show you the top submissions from each of the acceptable programming languages. You can also use the summary button if you know which SRM the class is from but this does not guarantee that the solution you view is correct if the problem was an old one.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
ive hit a road block and im stuck with a question on my work.
Here is the question:
Provide an implementation of the getLoad method that adds up the individual weights of
the items in the items list and returns the total.
The items list is:
ArrayList<Item> items;
Ive done what i thought was right but for some reason its not working.
Any help on what is wrong, or if what im doing is wrong? thanks
#Override
public int getLoad() {
int load = 0; //declare the variable
for (Item i : items) { // for each item in the list of items
load = load + i.getWeight() ; // load equals the weight of the item and adds on
}
return load; //returns it
}
The one thing I could see going wrong is if getWeight() returns a double. In that case, you should make int load: double load instead
With the limited information you've provided here, I can only take a wild guess that the problem is either:
The list items is empty even though it shouldn't be. Make sure that Items actually get added to the list! Use a debugger or a printed message to find out if the item list is empty on getLoad().
Weights for each Item are not assigned correctly, so getWeight() returns zero for each of them. Make sure that each Item added to the list actually gets assigned its proper weight.
Posting more code would help us give a better answer.