I'm a beginner programmer and new to stackoverflow.
So i have been making a Math Formula Solver and I have 4 formulas up and functioning. I am working on my Sine formula solver and it won't work. I use a scanner and switch to select the formula and input the variables. Here is what i have.
Scanner input = new Scanner(System.in);
System.out.println("Enter '0' for list of formula call numbers");
System.out.print("Enter the formula request number: " );
int mFormula = input.nextInt();
switch(mFormula)
{
case 5 :
System.out.println("Sine Problem Solver, please enter your variables below: ");
System.out.println();
System.out.print("Value for known side: ");
int x = input.nextInt();
System.out.println();
System.out.print("Value for degrees: ");
int x3 = input.nextInt();
System.out.println();
double Sine = (x * Math.sin(x3));
System.out.print("The side length is: " + Sine);break;
}
im just really confused on why it isn't working.
It seems that your main problem is that Math.sin doesn't take degrees but radians. Try
Math.sin(Math.toRadians(x3))
Related
I am writing a program that finds the hypotenuse of a triangle, I need to let the program run an arbitrary amount of times until the user enters 2. I cannot figure out how to end the program when the user enters 2.
package assignment5a;
import java.util.Scanner;//import Scanner
public class Assignment5A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//new Scanner variable
int answer;
double side1, side2, result;
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
while(answer < 0 || answer > 2){
System.err.println("Please enter a valid answer.");
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
}
System.out.println("Enter side 1 of the triangle :");//input for side 1
side1 = sc.nextDouble();
System.out.println("Enter side 2 of the triangle :");//input for side 2
side2 = sc.nextDouble();
result = hypotenuse(side1, side2);//declares result as the result of the method hypotenuse
System.out.printf("Hypotenuse of your triangle is: %.2f%n", result);//prints results
}
public static double hypotenuse(double s1, double s2){//method for calculating hypotenuse
double hypot;
hypot = Math.sqrt((Math.pow(s1, 2) + Math.pow(s2, 2)));
return hypot;
}
}
Wilmol's answer and Elliot Frisch's answer / comment are half the solution.
The other half is that you need an outer loop around most of the logic so it'll repeat. Put most of main() inside a loop that uses while (true) { to start so that it'll loop forever.
Then use the logic of if (answer == 2) { ... to actually break out when the user inputs 2.
Few options:
if (answer == 2)
{
break;
}
if (answer == 2)
{
return;
}
if (answer == 2)
{
System.exit(0);
}
So I figured it out. Your answers helped a lot but i ended up putting two while loops. The code is below:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//new Scanner variable
int answer;
double side1, side2, result;
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
while(answer < 0 || answer > 2){
System.err.println("Please enter a valid answer.");
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
}
while(answer == 1){
System.out.println("Enter side 1 of the triangle :");//input for side 1
side1 = sc.nextDouble();
System.out.println("Enter side 2 of the triangle :");//input for side 2
side2 = sc.nextDouble();
result = hypotenuse(side1, side2);//declares result as the result of the method hypotenuse
System.out.printf("Hypotenuse of your triangle is: %.2f%n", result);//prints results
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
}
}
public static double hypotenuse(double s1, double s2){//method for calculating hypotenuse
double hypot;
hypot = Math.sqrt((Math.pow(s1, 2) + Math.pow(s2, 2)));
return hypot;
}
}
Exercise 2.12
(Compute arithmetic progression) An arithmetic progression(AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance the sequence 5,7,9,11,13,15... is an AP with a common difference of 2. If the initial term of an AP is A1 and the common difference of successive members is d, then the nth term of the sequence an is given by:
a n = a 1 + (n-1)*d
Write a program that prompts the user to enter a 1 and d and computes a 46.
Enter speed and acceleration: 60 3.5
The minimum runway length for this airplane is 514.286
This is my code:
import java.util.Scanner;
public class Chapter2Exercise12 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter speed: ");
double speed = input.nextDouble();
System.out.print("Enter acceleration: ");
double acceleration = input.nextDouble();
double length = speed + (46 - 1)* acceleration;
System.out.println("The minimum runway length for this airplane is " +
length + " meters");
}
}
This is the answer from the book:
import java.util.Scanner;
public class Exercise02_12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter speed v: ");
double v = input.nextDouble();
System.out.print("Enter acceleration a: ");
double a = input.nextDouble();
double length = v * v / (2 * a);
System.out.println("The minimum runway length for this airplane is " +
length + " meters");
}
}
Does anyone understand how they get the formula from length?
It can be relevant to other people who don't understand this and need to apply this in a project.
It appears that your code is computing A46 term correctly.
But, the question was they were actually asking is missing something like that:
"An airplane starts its liftoff in velocity 0.
Ask the user to type in the airplane's acceleration and liftoff speed (minimal speed needed for liftoff).
Compute the minimal length needed for the runway."
The answer, of course, is by using the formulas previously mentioned in the comments.
Today i tried to make a simple program in Java that gives me the average of the numbers i entered.
The problem is that if i use int added (as shown below), i don't get the same answer as when i use double added.
Code:
public class Main {
public static void main(String[] args) {
int amount;
int number;
int added = 0;
double average;
Scanner input = new Scanner(System.in);
System.out.println("This program calculates the average of the numbers entered by you.");
System.out.println("How many numbers do you want to enter?");
amount = input.nextInt();
for(int i=1; i<=amount; i++){
System.out.println("Enter a number:");
number = input.nextInt();
added = added + number;
}
average = added/amount;
System.out.println("The average of the numbers entered is: " + average);
}
}
Result:
This program calculates the average of the numbers entered by you.
How many numbers do you want to enter?
6
Enter a number:
3
Enter a number:
2
Enter a number:
4
Enter a number:
1
Enter a number:
6
Enter a number:
5
The average of the numbers entered is: 3.0
When i use double, i get the right answer:
public class Main {
public static void main(String[] args) {
int amount;
int number;
double added = 0;
double average;
Scanner input = new Scanner(System.in);
System.out.println("This program calculates the average of the numbers entered by you.");
System.out.println("How many numbers do you want to enter?");
amount = input.nextInt();
for(int i=1; i<=amount; i++){
System.out.println("Enter a number:");
number = input.nextInt();
added = added + number;
}
average = added/amount;
System.out.println("The average of the numbers entered is: " + average);
}
}
Result when i use double added instead of int added:
This program calculates the average of the numbers entered by you.
How many numbers do you want to enter?
6
Enter a number:
3
Enter a number:
2
Enter a number:
4
Enter a number:
1
Enter a number:
6
Enter a number:
5
The average of the numbers entered is: 3.5
Why does this happen and what should i do to avoid it?
To me it seems like added could be an int because i don't add up decimals but i add up whole numbers.
Thank you in advance.
In order for added/amount to return a non integer, either added or amount must be float or double or cast to either of them.
When both are int, an integer division takes place and the result is only converted to double after the division in order to be stored in your double average variable.
The result of a division in which both elements are integer, will be an integer. At least one of them needs to be a float or a double for the result of the division to be a floating point number.
I am writing a program for class which will allow the user to calculate the area of an isosceles trapezoid. Here is my code:
import java.util.Scanner;
import java.lang.Math;
public class CSCD210Lab2
{
public static void main (String [] args)
{
Scanner mathInput = new Scanner(System.in);
//declare variables
int topLength, bottomLength, height;
//Get user input
System.out.print("Please enter length of the top of isosceles trapezoid: ") ;
topLength = mathInput.nextInt() ;
mathInput.nextLine() ;
System.out.print("Please enter length of the bottom of isosceles trapezoid: ") ;
bottomLength = mathInput.nextInt() ;
mathInput.nextLine() ;
System.out.print("Please enter height of Isosceles trapezoid: ") ;
height = mathInput.nextInt() ;
mathInput.nextLine() ;
double trapArea = ((topLength + bottomLength)/2*(height));
System.out.println();
System.out.printf("The area of the isosceles trapezoid is: "+trapArea);
}
}
If I enter say, 2 for topLength, 7 for bottomLength, and 3 for height, I will get an answer of 12.0, when it should result in an answer of 13.5. Does anyone know why my code is printing out wrong answers and not printing the .5?
The base of the issue can be known as "Integer Division". In Java, dividing 2 integers will yield a non-rounded integer.
Below are multiple ways to fix the issue you are having. I prefer the first method as it allows you to use your formula with non-integer values. Not all the lengths of a triangle are integers :)
Using the Scanner#getDouble and placing topLength, bottomLength, and height in doubles will give you the desired output.
Your code will then look like this :
public static void main(String[] args) {
Scanner mathInput = new Scanner(System.in);
// declare variables
double topLength, bottomLength, height;
// Get user input
System.out.print("Please enter length of the top of isosceles trapezoid: ");
topLength = mathInput.nextDouble();
mathInput.nextLine();
System.out.print("Please enter length of the bottom of isosceles trapezoid: ");
bottomLength = mathInput.nextDouble();
mathInput.nextLine();
System.out.print("Please enter height of Isosceles trapezoid: ");
height = mathInput.nextDouble();
mathInput.nextLine();
double trapArea = ((topLength + bottomLength) / 2 * (height));
System.out.println();
System.out.printf("The area of the isosceles trapezoid is: " + trapArea);
}
You could also cast your ints to doubles and calculate your trapArea as so :
double trapArea = (((double)topLength + (double)bottomLength) / 2 * ((double)height));
OR even simple, if you want, convert the 2 you are deviding to a double :
double trapArea = ((topLength + bottomLength) / 2.0 * (height));
All of these options will yield :
The area of the isosceles trapezoid is: 13.5
I need to create a program that reads 2 integers from the keyboard and prints a message saying whether the first number divides the second evenly.
I know I have to use either / or %, but it's not working.
int y, x,z;
System.out.print("Enter first number: ");
x= keyboard.nextInt();
System.out.print("Enter second number: ");
y= keyboard.nextInt();
z=y/x;
if (z%2==0){
System.out.println("Divides evenly");
} else {
System.out.println("Does not divide evenly");
}
I have to divide y by x. So, for example, x= 25 y=100, it should come out even. Then x=35 y=100 not even.
I've tried
z==0
z%y==0
Not working.
You're close, you don't need the extra integer division.
Integer division ( % ) gives you the remainder. So when you do z=y%x;, all you need to do in your if statement is check that z is equal to 0 (meaning that the first number evenly decided the second number).
int y, x,z;
System.out.print("Enter first number: ");
x= keyboard.nextInt();
System.out.print("Enter second number: ");
y= keyboard.nextInt();
z=y%x;
if (z==0){ //Changed this liine
System.out.println("Divides evenly");
} else {
System.out.println("Does not divide evenly");
}
int x,y;
System.out.print("Enter first number: ");
x= keyboard.nextInt();
System.out.print("Enter second number: ");
y= keyboard.nextInt();
if (x%y==0){
System.out.println("Divides evenly");
} else {
System.out.println("Does not divide evenly");
}