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.
Related
// Create a full program in eclipse that will ask the user how times would you like to loop for. It will ask in the for loop for a rate in double value and a time in integer value. It will calculate the distance for each of the times you enter in the rate and the time.//
my current output gives me the correct answer if i only want to put in once, as it only has to run the program once. Whenever i input more than 1, It starts acting weird and thats the best way i can explain it because i don't know whats wrong with it.
Here is one random output
How many times would you like to calculate the distance.
12
Enter rate
2
Enter time
13
The distance is 26.0
Enter rate
12
Enter time
12
The distance is 144.0
How many times would you like to calculate the distance.
1
Enter rate
12
Enter time
1
The distance is 12.0
// my actual code
import java.util.Scanner;
public class NTC {
public static void main(String[] args){
Scanner kb=new Scanner(System.in);
int loop = 10;
double rate=0;
int time=0;
int count;
double distance = rate*time;
for (count = 0; count <= loop; count++) {
System.out.println("How many times would you like to calculate the distance.");
loop = kb.nextInt();
for(rate=0; rate <loop;rate++)
{
System.out.println("Enter rate");
rate = kb.nextDouble();
for(time=0; time <loop;time++)
{
System.out.println("Enter time");
time = kb.nextInt();
System.out.println("The distance is "+rate*time);
}
You need to have only one for loop:
import java.util.Scanner;
public class NTC
{
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
int loop = 10;
double rate=0;
int time=0;
int count;
double distance = rate*time;
System.out.println("How many times would you like to calculate the distance.");
loop = kb.nextInt();
for (count = 0; count < loop; count++)
{
System.out.println("Enter rate");
rate = kb.nextDouble();
System.out.println("Enter time");
time = kb.nextInt();
System.out.println("The distance is "+(rate*time));
}
}
}
Here's the exercise:
35. Write a Java program to compute the area of a polygon. Go to the editor
Area of a polygon = (n*s^2)/(4*tan(π/n))
where n is n-sided polygon and s is the length of a side
Input Data:
Input the number of sides on the polygon: 7
Input the length of one of the sides: 6
Expected Output
The area is: 130.82084798405722
My code returns 127.30573435631248 (length = 7, sides = 6)
The code on the website returns 130.82084798405722 (length = 7, sides = 6)
I'm having trouble seeing why mine is different from theirs...
Any ideas?
Here's my code:
public static void exercise35(){
int number1 = integerInput(); //set to 7, length
int sides = integerInput(); //set to 6, sides
double area = (sides * (number1 * number1)) / (4.0 * Math.tan((Math.PI / sides)));
System.out.println("The area of a polygon with " + sides + " sides of length " + number1 + " = " + area);
}
Here's the solution from http://www.w3resource.com/java-exercises/basic/index.php (exercise #35)
import java.util.Scanner;
public class Exercise35 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input the number of sides on the polygon: ");
int ns = input.nextInt();
System.out.print("Input the length of one of the sides: ");
double side = input.nextDouble();
System.out.print("The area is: " + polygonArea(ns, side)+"\n");
}
public static double polygonArea(int ns, double side) {
return (ns * (side * side)) / (4.0 * Math.tan((Math.PI / ns)));
}
}
You mixed sides and number in the formula.
That teaches an interesting lesson of programming: "Give sensible names to variables".
Change 6 and 7 and you'll get the answer you want : ) And in the future, please try debugging your code first to see what's going on.
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 trying to write a Java program that takes in a potentially infinite number of values - and once the user enters a negative number, the program stops, computes the average of all of the entered numbers (excluding the negative one) and prints out how many numbers were entered (once again, not the negative one) as well as the average.
Below is the code I currently have. When I try to run the program, it does not computer the average correctly and you have to enter a couple consecutive negative numbers for it to finally stop the program.
To test the arithmetic and the rest of the program, I inserted a statement that would close the program if the word "negative" was entered rather than a negative number. When did this, the average and count and everything else worked just like it was made to. Essentially, the problems start to occur when I try to stop the program after a negative number.
I am a beginning programmer and this has been driving me crazy for a couple hours. Any help is greatly appreciated!
import java.util.*;
import java.lang.Math;
import java.io.IOException;
import java.io.InputStream;
public class Average
{
public static void main(String[] args)
{
Scanner numInput = new Scanner(System.in);
double avg = 0.0;
double count = 0.0;
double sum = 0.0;
System.out.println("Enter a series of numbers. Enter a negative number to quit.");
while (numInput.hasNextDouble())
{
double negNum = numInput.nextDouble();
if (negNum >= 0)
{
sum += numInput.nextDouble();
count++;
avg = sum/count;
}
else
{
System.out.println("You entered " + count + " numbers averaging " + avg + ".");
break;
}
}
}
}
You should use: sum+=negNum; instead of sum += numInput.nextDouble();
As it is now, your program, reads a number and if it is not negative it reads another number and adds it to the sum.
Also, you should compute the average only once in the else block.
You are reading a new number to compute the sum.
It should be
sum += negNum;
You have already received the number entered by the user in line:
double negNum = numInput.nextDouble();
You should add this number itself to sum rather than asking for another number from user by calling numInput.nextDouble() again. So the fixed code would be:
public static void main(String[] args){
Scanner numInput = new Scanner(System.in);
double avg = 0.0;
double count = 0.0;
double sum = 0.0;
System.out.println("Enter a series of numbers. Enter a negative number to quit.");
while (numInput.hasNextDouble())
{
double negNum = numInput.nextDouble();
if (negNum >= 0)
{
sum += negNum;
count++;
avg = sum/count;
}
else
{
System.out.println("You entered " + count + " numbers averaging " + avg + ".");
break;
}
}
}
Sample Run:
Enter a series of numbers. Enter a negative number to quit.
2
3
-1
You entered 2.0 numbers averaging 2.5.
Change
sum += numInput.nextDouble(); // reading next value again
to
sum += negNum;
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))