Splitting units base of a number and separating remainders - java

I am trying to split a number of a base then separating the two numbers to get different outputs. (Keep in mind I just edited, my answer is the solution). This is left here so people that have a similar problem can find a solution. Thank you all!
So this is the idea:
If number >= 10 && of base 10
Then give me discounted price on 10 units
if number <= 0 && not base 10
Then add the discount for the number which has 10 units in it and the remainder without the discount (let's say 100% for simplicity sake of the numbers)
So to make a practical example
If I order 25 units of x (at $1 each) and 15 units (at $1 each) of y the price will be:
x 20 units = $0
x 5 units = $5 total
y 10 units = $0
y 5 units = $5 total
This is a bit tricky and this is what I got so far:
double discountedmNI = (mNI - ((mNI/100)*10)) * mNIC;
double discountedmNIP = mNI - ((mNI/100)*10);
if(mNIC >= 10 && mNIC % 10 == 0){
System.out.println("mNI " + discountedmNIP + " " + mNIC);
System.out.println(discountedmNI);
}
else if (!mNIC % 10 == 0){
System.out.println("mNI " + mNI + mNIC);
System.out.println(mNI * mNIC);
}
I don't think I am defining separate the 10 units right
Thank you all!

I hope I understood you right. I get that you want to calculate a total price that consists of two elements: the price for non-discounted items and a price for discounted items.
// The following three values are just example assumptions.
float discountInPercent = 100.0f;
float itemsOrdered = 5004.0f;
float itemPriceNormal = 5.0f;
// Here the price for one discounted item gets calculated.
// Please remember that the discount is given in percentage.
float itemPriceDiscounted = itemPriceNormal * ((100.0f - discountInPercent) / 100.0f);
// Calculate the count of items that get discounted and those that
// will get priced normally.
float itemsDiscounted = Math.floor(itemsOrdered / 10.0f);
float itemsNotDiscounted = itemsOrdered % 10;
// Finally calculate the two elements of the total price and sum it up.
float priceDiscounted = (itemsDiscounted * itemPriceDiscounted);
float priceNormal = (itemsNotDiscounted * itemPriceNormal);
float totalPrice = priceDiscounted + priceNormal;
System.out.println("Price discounted: %.2f" + priceDiscounted);
System.out.println("Price non-discounted: %.2f" + priceNormal);
System.out.println("Price total: %.2f" + totalPrice);

EUREKA!
double discountedmNIP = mNI - ((mNI/100)*10);
int mNIC2 = (mNIC % 10);
double mNIC2disc = (mNI * mNIC2);
double discountedmNI = (mNI - ((mNI/100)*10)) * (mNIC - mNIC2);
if(mNIC >= 10){
System.out.println(discountedmNIP + " " + (mNIC - mNIC2) + " " + discountedmNI );
System.out.println(mNI + " " + mNIC2 + " " + mNIC2disc);
}
else{
System.out.print(mNI + " " + mNIC);
System.out.print(mNI * mNIC);
}
double sum = (mNI + discountedmNI + discountedRh + rH);
System.out.println('\t');
System.out.println("Total order cost " + sum);
All I need to do is to take the units % 10 which will divide the left side integer or double by the right side (left side input from user)
and will give me the remainder when I do that variable subtracted to the original variable!
Again, this small step took me a whole night to figure it out, and is simple indeed. This is for a class, and if you are in that class and you are reading (even though you might have to dig a little to find what assignment is this one), I would just like to tell you this is what's fun about programming! I am not being sarcastic I really love these type of problems!
Signed:
That foreign guy;
EUREKA again!
Enjoy!

Related

Using a average to get probability of a single occurance in a range of numbers

So this is a interesting statistic related question.
What I have
Average
Range of possible occurrences
What I'm after
The probability of a single occurrence in my range happening
A example would be a average of 10.3 . range of 1-20. Whats the chance 4 occurs? I need to use 10.3 as a sort of weight because if not each occurrence has a 1/20 chance of happening
Is there a statistical formula for something like this?
Coding
public void ReboundFormula(double TeamRebound, double OTeamRebound, double OffensiveRebound, double DefensiveRebound,
double ShotsTotalAverage, double OShotsTotalAverage, double TeamShotAveragePercent, double OTeamShotAveragePercent,
double PlayerORebound, double PlayerDRebound, double PlayerOPercentRebound, double PlayerDPercentRebound)
{
//Possible rebounds using amount of shots that "miss" which result in a rebound chance. Note fouls, ball going out of bounds
//, or other event that causes a rebound to not occur on a missed shot
predictedPossibleRebounds = (ShotsTotalAverage*(1 -(double) TeamShotAveragePercent/100)) + OShotsTotalAverage*(1 - (double) TeamShotAveragePercent/100);
Temp = (double) TeamRebound/(TeamRebound+OTeamRebound);
Temp2 = (double) OTeamRebound/(TeamRebound+OTeamRebound);
System.out.println("Percent of Team 1 Rebounds: " + Temp + " | Percent of Team 2 Rebound: " + Temp2);
System.out.println();
//Predicted rebounds a team will grab of the amount of rebounds they will likely get
predictedTeamRebound = (double) predictedPossibleRebounds*Temp;
predictedOTeamRebound = (double) predictedPossibleRebounds*Temp2;
System.out.println("Amount of Rebounds to Team: " + predictedTeamRebound + " | Amount of Rebounds to OTeam: " + predictedOTeamRebound);
System.out.println();
//Rebounds predicted to be grabbed by teams
Oratio = ((double) OffensiveRebound/TeamRebound)*predictedTeamRebound;
Dratio = ((double) DefensiveRebound/TeamRebound)*predictedTeamRebound;
System.out.println("Amount of Offensive Rebounds: " + Oratio + " | Amount of Defensive Rebounds: " + Dratio);
System.out.println();
System.out.println("Predicted Rebounds: " + predictedPossibleRebounds + " | Player Offensive Percent: " + PlayerOPercentRebound + " | Player Defensive Percent: " + PlayerDPercentRebound);
//Player data time
Temp = (Oratio*(PlayerOPercentRebound/100)) + (Dratio*(PlayerDPercentRebound/100));
System.out.println();
System.out.println("Player Predicted Rebound Total: " + Temp + " | Player Rebound Total Average: " + (PlayerORebound+PlayerDRebound) );
System.out.println();
System.out.println( " " + (int) Math.round((Temp) - (Temp*0.75)) + " " + (int) Math.round((Temp) - (Temp*0.5)) + " " + (int) Math.round((Temp) - (Temp*0.25)) + " " + (int) Math.round((Temp) - (Temp*0.1)) + " " + (Temp)
+ " " + (int) Math.round((Temp) + (Temp*0.1)) + " " + (int) Math.round((Temp) + (Temp*0.25)) + " " + (int) Math.round((Temp) + (Temp*0.5)) + " " + (int) Math.round((Temp) + (Temp*0.75)));
System.out.println("-75% -50% -25% -10% Predicted Average +10% +25% +50% +75%");
System.out.println();
Feeding data
run.ReboundFormula(54, 48.2, 13.662, 40.338, 89.86, 87.06, 45.2, 45.7, 3.7, 6.2, 12.9, 22.8);
You would need to specify the distribution of outcomes. Merely to specify the average and range does not suffice. For example, an unbalanced die with a 1/12 chance of reading 1 and a 3/12 chance of reading 5, other outcomes having equal probability, averages 3.83. However, other unbalanced dice are also possible with the same average, for example a die with a 1/12 chance of reading 2 and a 3/12 chance of reading 6.
I suspect that what you want is a binomial distribution. The binomial distribution results from a finite number of discrete trials (as your program's rebounds), each of which either succeeds or fails, each trial of equal weight.
If you follow the link, you will see many formulas, probably more than you wanted, but a principle underlies the formulas and the principle is what you mainly want to grasp. That won't be so easy, but once you have grasped the principle the chief formulas will naturally follow.

Variable not updating based on a change of another

In my program I have a method calculateCost(), which gets the cost of a truck based on the minimum temperature of said truck.
public double calculateCost() {
int minimumTemperature = this.getTemperature();
System.out.println("Temp is: " + minimumTemperature);
double costOfTruck = 900 + 200 * (Math.pow(0.7, (minimumTemperature / 5)));
System.out.println("Cost is: " + costOfTruck);
return costOfTruck;
}
When this method is executed, the minimumTemperature correctly changes as shown in the console, however, the costOfTruck doesn't change when the minimumTemperature is changed.
If minimumTemperature < 5 then minimumTemperature / 5 will equal zero due to integer division and Math.pow(0.7, (minimumTemperature / 5) will equal 1, so try using a double numeric type
double costOfTruck = 900 + 200 * (Math.pow(0.7, (minimumTemperature / 5.0)));

How to get a percentage from an array value

I am trying to get a percentage from an array value in a for-loop.
System.out.println(playerName[i+1] + " got " + playerScore[i] + " questions right out of " + question.length + "!\n");
double playerScorePercentage = (playerScore[i] / question.length) * 100;
System.out.println("Which is " + playerScorePercentage + "%!");
What am I doing wrong? The playerScoredisplays a value but when I am trying to do a calculation with it below, it displays 0.0 no matter what.
An example run:
John got 3 questions right out of 10!
Which is 0.0%!
Try casting the values before the division:
double playerScorePercentage = ((double)playerScore[i] / (double)question.length) * 100;
And search the web for "integer division" if you don't know the topic.

Dynamically calculating change to the nearest 10 dollars

I am new to Java and I'm trying to figure out how to dynamically calculate the change to the nearest 10 dollars. For instance, the user inputs a value (34.36), my code then calculates tip, tax, and total amount for the bill (total 44.24). Without user input, I need to calculate the change from $50.00. I've tried to round up to 50.00 from 44.24 with no luck, obviously I am doing something wrong. I've tried Math.round and tried to find the remainder using %. Any help on how to get the total change due to the nearest 10 dollar value would be great. Thank you in advance, below is my code:
Full dis-closer, this is a homework project.
import java.util.Scanner;
import java.text.NumberFormat;
import java.lang.Math.*;
public class test1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Get input from user
System.out.println("Enter Bill Value: ");
double x = sc.nextDouble();
//Calculate the total bill
double salesTax = .0875;
double tipPercent = .2;
double taxTotal = (x * salesTax);
double tipTotal = (x * tipPercent);
double totalWithTax = (x + taxTotal);
double totalWithTaxAndTip = (x + taxTotal + tipTotal);
//TODO: Test Case 34.36...returns amount due to lower 10 number
//This is where I am getting stuck
double totalChange = (totalWithTaxAndTip % 10);
//Format and display the results
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
//Build Message / screen output
String message =
"Bill Value: " + currency.format(x) + "\n" +
"Tax Total: " + currency.format(taxTotal) + "\n" +
"Total with Tax: " + currency.format(totalWithTax) + "\n" +
"20 Percent Tip: " + currency.format(tipTotal) + "\n" +
"Total with Tax and 20 Percent Tip: " + currency.format(totalWithTaxAndTip) + "\n" +
"Total Change: " + currency.format(totalChange) + "\n";
System.out.println(message);
}
}
you make
double totalChange = round((totalWithTaxAndTip / 10)) * 10;
Math.round rounds a number to the nearest whole number, so as others have shown, you need to divide by 10, then multiply by 10 after rounding:
double totalChange = tenderedAmount - totalWithTaxAndTip;
double totalChangeRounded = 10 * Math.round(totalChange / 10);
Math.ceil(double) will round up a number. So what you need is something like that:
double totalChange = (int) Math.ceil(totalWithTaxAndTip / 10) * 10;
For totalWithTaxAndTip = 44.24, totalChange = 50.00
For totalWithTaxAndTip = 40.00, totalChange = 40.00
Everyone, thank you very much for helping me. I tested out everyone's solution. This is my final working code.....
double totalAmountPaid = totalWithTaxAndTip - (totalWithTaxAndTip % 10) + 10;
I tested it out using many different values and it seems to be working the way I want it to.
Again, I appreciate everyone for taking the time to help me out.

Creating Mulitple If statment for Converting height

public String convertHeightToFeetInches()
{
int leftOver = heightInInches % IN_PER_FOOT;
if(heightInInches < (IN_PER_FOOT * 2)){
return "1 foot " + leftOver + " inches";
}
else{ return "";
}
if(heightInInches < (IN_PER_FOOT * 3) && heightInInches > (heightInInches * 2)){
return "2 foot " + leftOver + " inches";
}
else{
return "";
}
I want to make it return "1 foot 4 inches" or however tall they are..
I got the first if statment to work but what would i do to continue up to like 6 feet.. I tried just adding another one but im pretty sure thats not how to do it. How can i put this together?
Wouldn't it be simpler just to calculate the foot as well?
public String convertHeightToFeetInches()
{
int inches = heightInInches % IN_PER_FOOT;
int foot = heightInInches / IN_PER_FOOT;
return "" + foot + " foot and " + inches + " inches";
}
It's possible to use a division statement to come up with the number if feet in 'heightInInches'.
Can you try that?
For example, I'm confident that if I know someone is 50 inches tall, I can quickly calculate that the person is at least 4 feet tall.
Once you have that working, we'll work on the inches.
You have most of the logic correct as it is, but you do need a little push. Consider a height between 1 and 2 feet. In mathematical terms, we would describe this as 1 < x <= 2. Consider now what that would translate to in Java - what about x must be true? Hint: it's two conditions.
Next, an if-else if-else won't work if you have an else just sitting there. else will execute if nothing else matches with it (and that will occur often). Place an if after those elses.

Categories