Finding Angle in SSS Triangle JAVA - java

I need help calculating the angle between the 5 and the 3. I have absolutely no idea how to even model the question, let alone convert it to java. This is entirely specific, does not need to be abstracted.
int a=3; int b=4; int c=5;
is honestly as far as I can get. This would be cos(b), right? I just need a quick pointer/solution

Is this a right-triangle? So the 5 is the hypotenuse (the side opposite the right angle)?
If that's the case, you need to find any one of the following (which should all be equal to each other): arctan(4/3), ArcSin(4/5), or arccos(3/5).
For example, using the first:
Math.toDegrees(Math.atan(4.0/3.0))

Related

how to calculate inverse erfc while unknown value and constants are involved

I have this equation in java,
double BER = (Erf.erfc(Math.sqrt(3 * CodedEb_No) * Math.sin(Math.PI/8)))/3;
Erf.erfc is from org.apache.commons.math3.special.Erf
I don't know what is CodedEb_No, but BER is 1E-7. How can I calculate CodedEb_No?
I have a hint that I need to use erfcInv() from org.apache.commons.math3.special.Erf. But, since the rest of the values are also part of erfc function, I am bit confused on how to approach this. Can someone help me with this?
I think this is more of a maths than programming question after a very quick look and not testing, I guess you want to rearrange this function to make CodedEb_No the subject. I think that would be
CodedEb_No = Math.pow(Erf.erfcInv(BER * 3) / Math.sin(Math.PI/8), 2) / 3
You need to test this carefully!

Is there a better method for randomizing functions besides Random?

I have 2 strings in an array. I want there to be a 10% chance of one and 90% chance to select the other. Right now I am using:
Random random = new Random();
int x = random.nextInt(100 - 1) + 1;
if (x < 10) {
string = stringArray(0);
} else {
string = stringArray(1);
}
Is this the best way of accomplishing this or is there a better method?
I know it's typically a bad idea to submit a stack overflow response without submitting code, but I really challenge this question of " the best way." People ask this all the time and, while there are established design patterns in software worth knowing, this question almost always can be answered by "it depends."
For example, your pattern looks fine (I might add some comments). You might get a minuscule performance increase by using 1 - 10 instead of 1 - 100, but the things you need to ask yourself are as follows :
If I get hit by a bus, is the person who is going to be working on the application going to know what I was trying to do?
If it isn't intuitive, I should write a comment. Then I should ask myself, "Can I change this code so that a comment isn't necessary?"
Is there an existing library that solves this problem? If so, is it FOSS approved (if applicable) / can I use it?
What is the size of this codebase eventually going to be? Am I making a full program with microservices, a DAO, DTO, Controller, View, and different layers for validation?
Is there an existing convention to solve my problem (either at my company or in general), or is it unique enough that I can take my own spin on it?
Does this follow the DRY principle?
I'm in (apparently) a very small camp on stack overflow that doesn't always believe in universal "bests" for solving code problems. Just remember, programming is only as hard as the problem you're trying to solve.
EDIT
Since people asked, I'd do it like this:
/*
* #author DaveCat
* #version 1.0
* #since 2019-03-9
* Convenience method that calculates 90% odds of A and 10% odds of B.
*
*/
public static String[] calculatesNinetyPercent()
{
Random random = new Random();
int x = random.nextInt(10 - 1 ) + 1
//Option A
if(x <= 9) {
return stringArray(0);
}
else
{
//Option B
return stringArray(1);
}
}
As an aside, one of the common mistakes junior devs make in enterprise level development is excessive comments.This has a javadoc, which is probably overkill, but I'm assuming this is a convenience method you're using in a greater program.
Edit (again)
You guys keep confusing me. This is how you randomly generate between 2 given numbers in Java
One alternative is to use a random float value between 0..1 and comparing it to the probability of the event. If the random value is less than the probability, then the event occurs.
In this specific example, set x to a random float and compare it to 0.1
I like this method because it can be used for probabilities other than percent integers.

java.math.BigDecimal.scale() equivalent for double

I've got a matrix of values like the one below that I need to scale. I've been looking around for an inbuilt function if there is one that could do this for me. I haven't found one & so have ended up writing code to do the scaling using the below formula
scaledMatrix = (Matrix - MeanMatrix)/Standard Deviation
This code is a bit buggy & I'm working on correcting it. While I do that, I happened to bump on java.math.BigDecimal.scale() & did look up an equivalent for double as the matrix I have is double type numbers
If someone could please help me with details on
1) If there is an inbuilt function that accepts matrix of values & returns me the scaled matrix
2) `java.math.BigDecimal.scale()` equivalent for `double` type data
Any help would be much appreciated please.
The BigDecimal.scale() method does not do what you seem to think it is doing. A BigDecimal value is stored as a * 10^b (where ^ denotes exponentiation). The BigDecimal.scale() method basically returns the b part of that.
I do not know of a similar method for double values, nor do I know of a method which performs the function you need. Since you put apache-commons in the tags, I suggest you look into Apache Commons's extensive statistical library.

How do I use a point class, rational number class, and line class to Solve a system of 2 linear equations?

I am fairly new to computer science and have been trying, unsuccessfully, to use a point class, a line class, and a Rational number class in order to solve a System of linear equations. The code for the System is old and I'm adapting it to work for the new one I'm making but I can't, for the life of me, debug/work-through one section of the code(the relates to the rest so if I can't do one I can't do all and the whole code doesn't work) at oneChosenFirst(). I'm not seeking the complete code but a very simplified explanation as to how to properly do this, thank you. Here is what i have in systems so far. the systems is based on user inputs too.
My Systems of Linear equations for slope and a y intercept:
public static void oneChosenFirst() {
System.out.println("Enter values for Y intercept (0,#). ");
double A = scn.nextDouble();
double B = scn.nextDouble();
Point Yint1 = new Point();
System.out.println("Enter values for slope (Rise/Run). ");
Line slope1 = new Line();
}
Once again, many thanks, anything you think may help me on my journey through java is much appreciated.
To solve a system of linear equations geometrically (as with your point and line classes), represent each equation as a line, and then the solution to the system is the point at which these lines intersect. If you are having difficulty with a particular step of this process, please say so and show what you've tried so far to accomplish that step.

How do I effectively use the Modulus Operator in Java

I am doing a college assignment in Java that deals with currency. For that I am advised to use ints instead of doubles and then later convert it to a dollar value when I print out the statement.
Everything works fine until I do calculations on the number 4005 (as in $40.05 represented as an int). I am pasting the part of code I am having problems with, I would appreciate if someone could tell me what I am doing wrong.
import java.io.*;
class modumess {
public static void main(String[] args) {
int money = 4005; //Amount in cents, so $40.05;
// Represent as normal currency
System.out.printf("$%d.%d", money/100, money%100);
}
}
The above code, when run, shows $40.5, instead of $40.05. What gives?
Kindly note that this is for my homework and I want to learn, so I would really appreciate an explanation about the root of the problem here rather than just a simple solution.
EDIT: Following Finbarr's answer, I have added the following to the code which seems to have fixed the problem:
if (money%100 < 10) {
format = "$%d.0%d";
}
Is this a good way to do it or am I over-complicating things here?
EDIT: I just want to make it clear that it was both Finbarr and Wes's answer that helped me, I accepted Wes's answer because it made it clearer for me on how to proceed.
A better way would be something like this for a general case:
format = "%d.%02d";
%02d gives you 0 padding for 2 digits. That way you don't need the extra if statement.
See this for more explanation of things you can do in format: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
The modulus operator returns the remainder after division without fractional calculation. In this case, 4005%100 returns 5 as the remainder of 4005/100 is 5.

Categories

Resources