I am creating a BMI Ideal Weight calculator where the user is prompted to input data or there information into the textboxes, such as their name, height, weight (the weight can be in metric aka kgs or in imperial aka pounds) and the height can either be in metres or in feet. I also am using a radiobutton, two different ones to distinguish between genders (male or female) because males and females have different BMIs. The information and formulas I'm supposed to refer to is in the photo below, but my problem is so far when I am coding it and I'm just testing out for the male radio button because I completed it, nothing is outputting into my jLabel aka lblOutput...
Here is a quick rundown of what I want:
1)User is prompted to click on gender (radiobutton)
2)User is then needed to fill in the information
3)Supposed to output BMI and telling the person if it's normal, underweight, etc.
Here is the code (So far what I have):
String name = txt1.getText();
double Metric = Double.parseDouble(txt2.getText());
double Imperial = Double.parseDouble(txt2.getText());
double metres = Double.parseDouble(txt3.getText());
double inches = Double.parseDouble(txt4.getText());
double kgs = Double.parseDouble(txt5.getText());
double pounds = Double.parseDouble(txt5.getText());
double weight;
double weight2;
int m = 1;
int i =2;
int me =3;
int in=4;
if (rdbMale.isSelected()) {
if (Metric==m) {
//inches = System.null(); //need to learn how to make it null or dissapear if user inputs for metric instead of imperial
weight = metres/Math.pow(kgs, 2);
lblOutput.setText("Your name is " + name + " and your ideal weight in kgs is " + String.format("%.2f", weight));
}
} else if (Imperial==i) {
weight2 = inches/Math.pow(pounds, 2)*703;
lblOutput.setText("Your name is " + name + " and your ideal weight in pounds is " + String.format("%.2f", weight2));
}
// some codes are unneccessary like the int me, or the int in i used them so i can distinguish between which one the user wants to chose from either 3 for metres or 4 for inches, pls ignore that
//I basically just want this to work and I don't know how to get it to work
Related
I’m building a shop app and i have many products, I want to put an offer that when the user put an order for 2 pieces of the same product he get the third one for free, so he put 3 in the cart but the total amount is for 2, the same if he ordered 3,6,9,.....
this var is for the total price, the counter is the quantity
double usedPrice = getCurrentProduct.isOnSale
? getCurrentProduct.salePrice
: double.parse(getCurrentProduct.price);
double totalPrice = usedPrice * int.parse(counter.toString());
After calculating totalPrice you can just update the quantity by
counter = counter + (counter / 2).floor() ;
I have a variable that is an Int (loan). I want to add 10% to it each time a button is clicked.
I've tried various methods, firstly:
loan = 50
loan = loan + loan / 100 * 10
when I print loan, it still shows 50 without the 10 percent added.
Next, I tried declaring a new Int variable (percent) to work out the percentage:
loan = 50
var percent = loan / 100 * 10
loan = loan + percent
Again, loan prints 50 and percent prints 0.
In each case, I've experimented putting the maths into brackets, but that gives the same result. I'm sure this must be a very simple fix but I can't work it out, and have had no luck googling.
Your problem is that loan is declared as an Int. Ints cannot have fractional components, so when you try to divide 50 by 100, the answer, 0.5, has its fractional component stripped off, making 0. Times that by 10, and it's still zero, so when you add it to loan, it doesn't change the result.
You can solve this by declaring loan as a Double instead of an Int. You can also just multiply by 1.1 to add 10%.
var loan: Double = 50
loan *= 1.1
I want to add 10% to it each time a button is clicked.
You can either add 10%, or a tenth, of the original value, or multiply by 1.1:
var loan = 50.0 // including the decimal point makes it a float
...any of these will work:
loan = loan + loan * 0.1
loan = loan * 1.1
loan += loan * 0.1
loan *= 1.1
Again, "loan" prints 50 and "percent" prints 0.
That's because loan was declared as an integer type, and the other values in your calculation were also integers, so the compiler inferred that percent should also be an integer. You could fix that by explicitly setting the type of either loan or percent to Float or Double, like this:
var percent : Float = loan / 10
Or you can do it implicitly, as I did up top, by setting loan to 50.0 instead of 50. The compiler will understand that 50.0 is a Float, so it'll infer that loan is a Float, which would then cause calculations done with loan to be of that type, and percent would also be a Float.
first, loan should be double (cause int cannot have fractions)
Then you can do something like this:
loan = 50
loan *= 1.1
I am creating an expense ios application. I am using a Progress View to display how much income the user has left. So for example, if the user puts $100 income, and $30 expense, I'd like the bar to be 70% full. How do I calculate this?
I had originally put
let fractionalProgress = Float(expenseFloat!)/Float(balanceFloat!)
but this doesn't return the right value needed.
The formula in your question is correct if you want to know the percentage spent so far.
The percent remaining formula is "remaining balance" / "original balance".
And of course "remaining balance" is "original balance" - "total of expenses".
let fractionalProgress = (balanceFloat - expenseFloat) / balanceFloat
where balanceFloat is the original balance and expenseFloat is the total expenses.
In your example with a balanceFloat of 100 and an expenseFloat of 30 this gives (100 - 30) / 100 which is 0.7.
Of course you would multiple this result by 100 if you wish to show a percentage. Better yet, use a NumberFormatter setup with a .percent style.
We can calculate percentage in swift as below:
//Calucate percentage based on given values
public func calculatePercentage(value:Double,percentageVal:Double)->Double{
let val = value * percentageVal
return val / 100.0
}
We can use this function like:
let value = calculatePercentage(value: 500,percentageVal: 20)
print(value)
Output will be:
100
I have install Sakai version 12, I encountered the following problem: The input score for the question was 0.525 but after clicking the Save button the score became 0.53. I tried to reconfigure the sakai.properties file at line gradebook.class.average.decimal.places and assignment.grading.decimals but failed.
I have attached the picture, expect anyone to help me.
Thanks!
Picture 1: http://prntscr.com/j36o75
Picture 2: http://prntscr.com/j36ogy Picture 3: http://prntscr.com/j36on1 Picture 4: http://prntscr.com/j36ork
From your pictures it looks like you're referring to Tests & Quizzes rather than assignment. It looks like T&Q (Samigo) is hardcoded to only be 2 decimal places. From what I can see Gradebook is also hardcoded for individual grade items to be 2 decimal places.
You'd have to submit a feature request on Sakai's Jira or a Pull Request to allow these values to be configured. There may be some loss in precision if too many decimal places are supported.
samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/evaluation/TotalScoresBean.java
359: String newmax= ContextUtil.getRoundedValue(maxScore, 2);
samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/evaluation/AgentResults.java
241: String newscore = ContextUtil.getRoundedValue(totalAutoScore.replace(',', '.'), 2);
270: String newscore = ContextUtil.getRoundedValue(
296: String newscore = ContextUtil.getRoundedValue(finalScore.replace(',', '.'), 2);
samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/delivery/DeliveryBean.java
1253: String newscore= ContextUtil.getRoundedValue(rawScore, 2);
1272: String newscore= ContextUtil.getRoundedValue(rawScore, 2);
samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/delivery/DeliveryBeanie.java
382: String newscore= ContextUtil.getRoundedValue(rawScore, 2);
samigo-app/src/java/org/sakaiproject/tool/assessment/ui/listener/util/ContextUtil.java
334: public static String getRoundedValue(String orig, int maxdigit) {
336: return getRoundedValue(origdouble, maxdigit);
338: public static String getRoundedValue(Double orig, int maxdigit) {
I am new in Swift.
I am trying to make a budget application. This app have a Calculator like keyboard. My idea is when users enter the money app will automatically add a decimal place for users.
For example, if you type 1230 it will give you 12.30 and type 123 it will display 1.23
I wrote a couple lines of code down below. The problem is it only can add decimal point after first digit it won't go backwards when you give more digits. It only can display as X.XXXXX
I tried solve this problem with String.index(maybe increase index?) and NSNumber/NSString format. But I don't know this is the right direction or not.
let number = sender.currentTitle!
let i: String = displayPayment.text!
if (displayPayment.text?.contains("."))!{
displayPayment.text = i == "0" ? number : displayPayment.text! + number
}
else {
displayPayment.text = i == "0" ? number : displayPayment.text! + "." + number
}
Indexing Strings in Swift is not as "straightforward" as many would like, simply due to how Strings are represented internally. If you just want to add a . at before the second to last position of the user input you could do it like this:
let amount = "1230"
var result = amount
if amount.characters.count >= 2 {
let index = amount.index(amount.endIndex, offsetBy: -2)
result = amount[amount.startIndex..<index] + "." + amount[index..<amount.endIndex]
} else {
result = "0.0\(amount)"
}
So for the input of 1230 result will be 12.30. Now You might want to adjust this depending on your specific needs. For example, if the user inputs 30 this code would result in .30 (this might or might not be what you want).