Simple Coding Problems - java

In the country of Rahmania, the cost of mailing a letter is 40 sinas for letters up to 30 grams. Between 30 g and 50 g, it is 55 sinas. Between 50 g and 100 g, it is 70 sinas. Over 100 g, it costs 70 sinas plus an additional 25 sinas for each additional 50 g (or part thereof). For example, 101 grams would cost 70 + 25 = 95 sinas. 149 g would also cost 95 sinas, because both of these packages only use the first 50 g over 100 g. Write a program that prompts the user for a mass and then gives the cost of mailing a letter having that mass.
This is the question and the only part I don't know how to do is the last part with the additional 25 sinas for each additional 50g.
Can someone show me how to write this part of the code?
I use netbeans btw.
Scanner input = new Scanner(System.in);
double l;
l = input.nextInt();
x = l >= 100 / 50 * 25 + 70;
if (l <= 30) {
System.out.println("40 Sinas");
}
if (l > 30 && l < 50) {
System.out.println("55 Sinas");
}
if (l > 50 && l < 100) {
System.out.println("70 Sinas");
}
if (l >= 100) {
System.out.println("");
}
Here's the important part of the code. As you can see I need help on the last part.

A solution:
Scanner input = new Scanner(System.in);
int l; // it should be int if read int
l = input.nextInt();
if (l <= 30) {
System.out.println("40 Sinas");
}
if (l > 30 && l < 50) {
System.out.println("55 Sinas");
}
if (l > 50 && l < 100) {
System.out.println("70 Sinas");
}
if (l >= 100) {
System.out.println(((((l - 100) / 50) + 1) * 25) + 70);
}

I just want to point out, there is one major problem with Krayo's solution. If the weight value is exactly 100, using that calculation, you will in fact charge the customer an extra 25 sinas. This is caused by the + 1 portion of the calculation.
If the weight is 100, then the number of 50 gram intervals above 100 is 0 (weight - 100 = 0). Therefore, the customer shouldn't be charge anything, since the wording of the question says Over 100 g.... However, because you're adding 1, before performing the additional cost calculation, the customer gets dinged an extra 25 sinas needlessly.
Instead, in order to insure the correct amount for this, and all other cases, I would use the Math.ceil(double) method instead. This method will round the value up to the nearest whole number. For example, 5.2 becomes 6.0, and 5.0 remains 5.0.
So your calculation would look like this instead:
int cost = (int) (Math.ceil((weight - 100) / 50.0f) * 25) + 70;
Note that we need use a floating point value to ensure we end up with a decimal value. And since Math.ceil returns a double, we simply cast it back to an integer.
This is a sample program to demonstrate this:
public class CalculationTesting {
public static void main(String[] args){
int weight = 100;
int good_cost = (int) (Math.ceil((weight - 100) / 50.0f) * 25) + 70;
int bad_cost = ((((weight - 100) / 50) + 1) * 25) + 70;
System.out.println("Weight = " + weight + "g");
System.out.println("Good cost calculation = $" + good_cost);
System.out.println("Bad cost calculation = $" + bad_cost);
}
}
You can change the weight value to experiment.
Which outputs:
Weight = 100g
Good cost calculation = $70
Bad cost calculation = $95

Related

Split Number into equal periodic values

I have been given min and max number,
e.g: min = 10 and max = 1000 now i want to split this number into equal parts and in result i should get 10 numbers.
like: 100, 200, 300, 400, 500, 600, 700 , 800 , 900, 1000
this we can do by dividing max with number we want e.g: 1000/10 and add 100 every time.
but, there is one scenario in which we have been given :
min = 14 and max = 1113 in this case if we divide 1113/10 then we will get 111
and the range will come like 0 , 113, 226, 339 ...
but, i don't want to show them like this . It would be better if i can display 110 or 115 or 120 even numbers (round off) instead of 113.
Can anyone help me.
In your example, when min=14 and max=1113 you divide 1113/10 to get 111. If you want to round 111 down to 110, you can perform 111 - (111 % 10) to get 110. Then simply iterate 10 times and multiply the beginning number by the current iteration.
There is a similar post to this here: How to round *down* integers in Java?
Depending on what numbers you want to display (either 110 or 115 or 120) you can approach this in a similar way.
That is an algorithm for scaling a diagram; distributing ticks on an axis.
However 10 ticks is then an over-estimate.
private int[] ticks(int min, int max) {
int gap = (max - min) / 10;
// Round gap up to a nice value - best calculatory:
if (gap < 1) {
gap = 1;
} else if (gap < 5) {
gap = 5;
} else if (gap < 10) {
gap = 10;
} else if (gap < 50) {
gap = 50;
} else if (gap < 100) {
gap = 100;
} else if (gap < 250) {
gap = 250;
...
}
int minTick = (min/gap) * gap;
if (minTick > min) { // For negative numbers.
minTick -= gap;
}
int maxTick = (max/gap) * gap;
if (maxTick > min) { // Negative, and ceiling.
maxTick += gap;
}
int ngap = (maxTick - minTick) / gap;
int[] ticks = new int[ngaps + 1];
for (int i = 0; i <= ngaps; ++i) {
ticks[i] = minTick + i * gap;
}
return ticks;
}

get dynamic ration for distrubution prize java

I have this algorithm that works, but I want to change it to have a dynamic ratio in order to have the distance of values for the first positions higher than those ones for the lastest positions.
So for instance if prize money = 1000, total_prizes = 5, last_prize = 50 I got:
1) 350.00
2) 275.00
3) 200.00
4) 125.00
5) 50.00
TOTAL SUM: 1000.00
What I would like to see should be:
1) 475.00
2) 250.00
3) 150.00
4) 75.00
5) 50.00
TOTAL SUM: 1000.00
Instead of having between positions always a fixed value of 75. Have an increase distance approaching the first positions, in this case
from 5) to 4) 25
from 4) to 3) 75
from 3) to 2) 100
from 2) to 1) 225
Here the current code:
public static void main(String[] aa){
float ratio;
float first_prize;
float s=0;
float money = 1000;
int total_prizes = 5;
float last_prize = 50;
float prizes[] = new float[total_prizes+1];
first_prize=2*(money/total_prizes)-last_prize; //last member of the progresion
ratio=(first_prize-last_prize)/(total_prizes-1);
prizes[total_prizes]=last_prize;
for (int j = total_prizes-1; j >=1; j--) {
prizes[j]=prizes[j+1]+ratio;
}
for(int k=1;k<=total_prizes;k++){
System.out.printf("%d) %.2f\n",k,prizes[k]);
s+=prizes[k];
}
System.out.printf("TOTAL SUM: %.2f\n",s);
}
Inside the loop:
for (int j = total_prizes-1; j >=1; j--) {
//ratio here should be calculated dynamically based on position...
prizes[j]=prizes[j+1]+ratio;
}
Thanks! :)
If I understand you, what you want is to have the following conditions:
Fixed total sum. In this case 1000.
Approximately a geometric ratio r with n terms. In this case 1.75.
All "round" numbers. In this case to the nearest 25.
I would suggest that you solve it by first creating an exact geometric sequence, and then tweak it to be round numbers.
The first step is easy because all geometric series with n terms look like this:
x + r * x + r^2 * x + ... + r^(n-1) * x
= x * (1 + r + r^2 + ... + r^(n-1))
= x * (1 + r + r^2 + ... + r^(n-1)) * (r - 1) / (r - 1)
= x * ((r - 1) + (r^2 - r) + (r^3 - r^2) + ... + (r^(n) - r^(n-1)) / (r - 1)
= x * (-1 + (r - r) + (r^2 - r^2) + ... + (r^(n-1) - r^(n-1)) + r^n) / (r - 1)
= x * (r^n - 1) / (r - 1)
So in we want
1000 = x * (r^n - 1) / (r - 1)
= x * (1.75^5 - 1) / (1.75 - 1)
= x * (16.4130859375 - 1) / 0.75
= 20.55078125 x
That gives us the following starting solution.
48.6599505797377
85.154913514541
149.021098650447
260.786922638282
456.377114616993
After that it is a question of tweaking those numbers up and down to get the answer that you want. There are a lot of ways you can do that. One is simply to start with the smallest and move it to a rounded number, and redistribute among the others in proportion to their weights, and continue. That gives you the following sequence of partial answers.
48.6599505797377
85.154913514541
149.021098650447
260.786922638282
456.377114616993
50
85.034965034965
148.811188811189
260.41958041958
455.734265734266
50
75
150.537634408602
263.440860215054
461.021505376344
50
75
150
263.636363636364
461.363636363636
50
75
150
275
450
This is not exactly your desired answer. But it is pretty darned close and hopefully acceptable as a strategy.
UPDATE I had tried 1.75 because it was close to (475/50)^(1/4). When I tried 1.8 instead I got your exact desired answer.
Furthermore note that we do not need to produce all of those intermediate answers. Each time we only need the smallest term. Which is given by target * (r-1) / (r^n - 1). Every time you find the smallest term, you subtract that from the target, reduce n by one, and repeat.

math need a number between -180 and 180

I have a math problem.
In minecraft the yaw can be between -180 and 180 now I need to add 90 to the player his yaw. but if the yaw is 150 + 90 = 240 that is more than 180, how can I add 90 but if it passes 180 count further on -180.
Examples:
-150 + 90 = -60, No problem
0 + 90 = 90, No problem
150 + 90 = 240, Problem it needs to be -120
This is a classic example of modular arithmetic. You can use the remainder operator %.
int current = 150;
int input = 90;
int newCurrent = ((180 + current + input) % 360) - 180;
System.out.println("NEW: " + newCurrent);
The output is:
NEW: -120
You could solve it like this:
private static final int MIN = -180;
private static final int MAX = 180;
public static void main(String[] args) {
System.out.println(keepInRange(-150 + 90));
System.out.println(keepInRange(0 + 90));
System.out.println(keepInRange(150 + 90));
System.out.println(keepInRange(-150 - 90));
}
private static int keepInRange(final int value) {
if (value < MIN) {
/*
* subtract the negative number "MIN" from "value" and
* add the negative result to "MAX" (which is like subtracting the absolute value
* of "value" from "MAX)
*/
return MAX + (value - MIN);
}
if (value > MAX) {
/*
* subtract the number "MAX" from "value" and to get exceeding number
* add it to "MIN"
*/
return MIN + (value - MAX);
}
return value;
}
The output is:
-60
90
-120
120
According to your explanation, the last to values should be -120 and 120 and not -60 and 60 like you said in your example. The exceeding amount should be added to the "other" bound, and not from 0. 150 + 90 is 240, so it exceeds the MAX bound by 60. This then should be added to the MIN bound. The result is -120.
If this is not correct, then please update your question.
You can use the modulo operator for positive numbers, this looks like a division but gives you the rest instead of the quotient:
10/6 = 1,66666...
10%6 = 4
For you: ( 150 + 90 ) % 180 = 60

Trying to round to nearest $5, $10, $50, and $100 when computing the next minimum bid in an auction

I'm working on a big program that utilizes a bunch of smaller methods to create a functioning auction system. I'm having trouble with this particular method where I have to raise the minimum bid for an item given the current or "now highest bid"
The instructions are as follows:
If a bid is accepted, then you need to raise the minBid on that item by 5% above the current bid,rounded up to the nearest $5 below $100.00,
rounded up to the nearest $10 between $100.01 and
$1000.00, rounded up to the nearest $50.00 between
$1000.01 and $10000.00, and rounded up to
the nearest $100 above $10000.00
My main issue is figuring out exactly I can round up by just 5 or just 10, depending on the amount of the bid. Here's my code so far, any help would be greatly appreciated :)
public class computeMinNextBid
{
public static double computeMinNextBid(double currentBid)
{
double minNextBid = (currentBid * 0.05) + currentBid;
if(minNextBid <= 100.00)
{
minNextBid = Math.round(minNextBid);
}
else if(minNextBid >= 100.01 && minNextBid <= 1000.00)
{
minNextBid = Math.round(minNextBid);
}
else if(minNext Bid >=1000.01 && <= 10000.00)
{
minNextBid = Math.round(minNextBid);
}
else(minNextBid >= 10000.01)
{
minNextBid = Math.round(minNextBid);
}
return minNextBid;
}
public static void main(String [] args)
{
double currentBid = Double.parseDouble(args[0]);
double minNextBid = computeMinNextBid(currentBid);
System.out.println("The next minimum bid is: " + minNextBid);
}
}
The general expression for rounding to N (using integer arithmetic) is:
x = (x + N/2)/N * N;
This rounds to the nearest multiple of N. For example, for x = 45 an N = 100, this yields 0 (i.e., it rounds 45 down to 0). For x = 55, this yields 100 (i.e., it rounds 55 up to 100).
To round down (also known as truncation), remove the "+ N/2" part:
x = x/N * N;
To round up to the nearest N, use:
x = (x + N-1)/N * N;
For example, for x = 35 and N = 100, this yields 100 (i.e., it rounds any x between 0 and 99 up to 100).

Strange: Cant get '1' from Math.random

I am using the following code, it generates numbers randomly but the problem is, I am not able to figure out why does not it generate the number 1
int ran, g, d, col, ran2;
double y = 1000 * (Double.parseDouble(t2.getText()));
int x = (int) y;
d = Integer.parseInt(anum.getText());
double c = 10;
int prevrandom = Integer.parseInt(lnum.getText());
lnum.setText("");
lnum.setVisible(true);
for (g = 0; g==0;) {
d = Integer.parseInt(anum.getText());
ran = (int) (Math.random() * (c)); // Random Number Creation Starts
if (ran > (c / 10)) {
g = 1;
ran2 = ((int) (Math.random() * 10)) % 2;
if (ran2 == 1) {
ran = ran * (-1);
}
d = d + ran;
if (d < 0) {
ran = ran * (-1);
d = d + (2 * ran);
}
int a = d - ran;
if(prevrandom==ran){
g=0;
}
if(g==1){
lnum.setText("" + ran);
}
}
}
I call this function(button) from somewhere. The problem comes when the sum ('a') becomes 4, according to my conditions it shouldn't allow any number other than 'one' and thus it goes into infinite loop.
I am talking about ran variable. Which I get after multiplying Math.random with 10^x where x is a positive integer.
Here ran2 is a number with value 1 or 0. As I multiply Math.Random with 10 which gives a 1 digit number and I mod it with 2.
THis is a 14 year old boy new to java. it would be greatful of people out here to help rather than discourage.
Look at the Javadoc:
Returns a double value with a positive sign, greater than or equal to
0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
If you need integer random numbers, you might be better off with java.util.Random. To generate a random integer in the range a..b (inclusively), you can use
Random random=new Random();
int rnd=a+random.nextInt(b-a+1);
The problem lies in the code
if (ran > (c / 10)) {
The random number gets created which is even equal to one; but here due to the sign '>' it gets rejected.
Use '>=' instead.
ran = (int) (Math.random() * (c)); where c is from 10 to 10^x
This can be 1 as follows.
int c = 1000;
for (int i = 0; i < 1000; i++) {
int count = 0;
int ran;
do {
ran = (int) (Math.random() * (c)); // where c is from 10 to 10^x
count++;
} while (ran != 1);
System.out.println("count: " + count);
}
prints sometime like
count: 1756
count: 86
count: 839
count: 542
count: 365
....
count: 37
count: 2100
count: 825
count: 728
count: 1444
count: 1943
It returns 1 a thousand time in less than a second.

Categories

Resources