using JAVA Loop for math calculation [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I had tried to do some calculations and something doesn't quite add up. I am trying to achieve the screenshot below
but this is what i'm getting
Please I need some help, this is what I have done so far
public class VelocityFall
{
public static void main (String [] a)
{
Scanner s = new Scanner (System.in);
System.out.print("This program prints a table that shows each \nsecond,"
+
"height from the ground (meters), and the velocity (m/s)\n of a free-falling" +
"object from an initial height (metres).\nPlease input the Initial Height H: ");
// input/get the value of H from the keyboard
double H = s.nextDouble ();
// we need to design/output the table by using println with lines and tabs (\t)
System.out.println ("------------------------------------------");
System.out.println (" t(s)\t\tHeight(m)\t\tVelocity(m/s)");
System.out.println ("------------------------------------------");
//we now require a for loop
for (int t = 0; t<=15; t++)
{
// we are now going to calculate and output the velocity and decreasing
height
double velocity = 9.8*t;
H = H-(0.5*9.8*Math.pow(t,2));
System.out.println(t + "\t\t" + H + "\t\t" + velocity);
}
}
}

Your problem is that you are reassigning the H variable in the line below.
H = H-(0.5*9.8*Math.pow(t,2));
Replace that line with the one below to get the right output.
double H_new = H-(0.5*9.8*Math.pow(t,2));
Don't forget to change the variable in your println call too:
System.out.println(t + "\t\t" + H_new + "\t\t" + velocity);
This way, the H variable stays equal to the user's input and your calculation isn't affected by the results of the previous calculation.
Output:
t(s) Height(m) Velocity(m/s)
------------------------------------------
0 1234.56 0.0
1 1229.6599999999999 9.8
2 1214.96 19.6
3 1190.46 29.400000000000002
4 1156.1599999999999 39.2
5 1112.06 49.0
6 1058.1599999999999 58.800000000000004
7 994.4599999999999 68.60000000000001
8 920.9599999999999 78.4
9 837.6599999999999 88.2
10 744.56 98.0
11 641.6599999999999 107.80000000000001
12 528.9599999999999 117.60000000000001
13 406.4599999999999 127.4
14 274.15999999999985 137.20000000000002
15 132.05999999999995 147.0
As for the issue of repeating digits, try using the DecimalFormat class.

Related

Java indexOf("") returns 0 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can someone help and explain whether indexOf("") returns 0 is predefined same as -1 by returning a negative result?
Thanks and Happy Easter to all!
ps. the following link was helpful, but it does not contain the exact answer to my question
"Hello".indexOf("") returns 0
public class ExIndexOf {
public static void main(String[] args) {
String s = "We learn Java.";
System.out.println(s.indexOf("ava")); // -> 10
System.out.println(s.indexOf("java")); // -1
System.out.println(s.indexOf(" ")); // -> 2
System.out.println(s.indexOf("")); // -> 0
}
}
Because indexOf returns the first position (index) of its argument in the string. Strings in Java, like arrays and collections are zero-indexed, meaning that the index 0 describes the first item. Index 1 is the second item and index n describes the n+1th item. Many functions return the (invalid) index -1 (a "magic" value) to denote "not found" or "error".
The empty string is contained in every string multiple times. The first position where it can be found is at position 0. Think of it as: String s = "" + "We learn Java." (or even more verbose: s = "" + "W" + "" + "e" + "" + " " + "" + "l" + …).
String s = "We learn Java.";
System.out.println(s.indexOf("")); // -> 0
System.out.println(s.indexOf("W")); // 0
System.out.println(s.indexOf("e")); // -> 1
System.out.println(s.indexOf(" ")); // -> 2
System.out.println(s.indexOf("not found")); // -> -1

Input radius for diameter, surface and circumference output is not giving me the output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm a beginner with java and have a problem. I have an assignment to do with the java.util.scanner class where you can put a value for radius in the console and get as output the diameter, surface etc.
My problem is, that I can enter the radius value, but it's not giving me the desired value for the diameter etc, only the text.
import java.util.Scanner;
public class Lab1_Aufg1
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter Radius: ");
double Radius = s.nextDouble();
double durchmesser;
double umfang;
double flaeche;
durchmesser = Radius * 2;
umfang = 2 * Math.PI * Radius;
flaeche = Math.PI * Radius*Radius;
System.out.println("Durchmesser = " durchmesser);
System.out.println("Umfang = " umfang);
System.out.println("Fläche = " flaeche);
s.close();
}
}
You need to change s.Next() to s.nextDouble() to get a double input. also, you need to put a + between the variables and the strings instead of putting a space:
System.out.println("Durchmesser = " + durchmesser);
You are missing a } at the bottom of your file.
Always make sure to count your curly braces.
Edit:
Your code was edited by another user, if you pop it into jdoodle you should see that it works now.
All problem fixed use this code, it'll work:
import java.util.Scanner;
public class stackCircle
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter Radius: ");
double Radius = s.nextDouble();
System.out.printf("Durchmesser = %.2f\n" , Radius * 2);
System.out.printf("Umfang = %.2f\n" , 2 * Math.PI * Radius);
System.out.printf("Fläche = %.2f\n" , Math.PI * (Radius*Radius));
s.close();
}
}
Note: I have added %.2f, this will make your output to display results up to 2 decimal positions. This is easy to read then using default 8 decimal places. Hope it helps.
You need to change Next() to nextDouble() to get an double-type input.
Also, String objects can be appended using the + operator in Java. You need to fix how you append strings within your System.out.println() statements:
System.out.println("Durchmesser = " + durchmesser); // Need to add the '+' symbol
System.out.println("Umfang = " + umfang);
System.out.println("Fläche = " + flaeche);

Programming average with arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public static void main (String args[])
{
//10 name arrays
String players[]=new String[10];
players[0]="Kevin Love";
players[1]="Kyrie Irving";
players[2]="Lebron James";
players[3]="Dion Waiters";
players[4]="Shawn Marion";
players[5]="Tristan Thompson";
players[6]="Anderson Varejo";
players[7]="Joe Harris";
players[8]= "Mike Miller";
players[9]="Brendan Haywood";
//10 height arrays in centimeter
double heights[]= new double [10];
heights[0]=208;
heights[1]=191;
heights[2]=203;
heights[3]=193;
heights[4]=201;
heights[5]=206;
heights[6]=208;
heights[7]=198;
heights[8]=203;
heights[9]=213;
System.out.println("The average of the players height is: " + calcAverage(heights)+ " cm");
}
// for calculating average for the players height
public static double calcAverage(double[] heights) {
double sum = 0;
for (int i=0; i < heights.length; i++) {
sum = sum + heights[i];
}
double average = sum / (double)heights.length;
return average;
}
// comparing height with the average
public static void heightvsAverage(String [] players, double [] heights, double average)
{
for(int i=0;i<10;i++)
{
while (heights[i]>average)
{
System.out.println("Players with above average are:");
System.out.println();
System.out.println("Players|Heights");
System.out.println("---------+---------");
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(String.format("%-11s|%8s", players[i],df.format(heights[i])));
}
}
}
}
I am having a problem with the third method i am trying to compare the heights with the average, and i am trying to make a table in the third method that outputs players with height above average. Main method and method 2 are excellent it's just that i am having difficulty making the chart in method 3
Hints:
1) What is going on here?
while (heights[i]>average)
Think about it!
2) How many headers does a table need? How many times is your code printing the table header? Why?
Use if in place of while in third method
First of all you're never calling the method, so I've added the function call after you display the average
heights[9]=213;
System.out.println("The average of the players height is: " + calcAverage(heights)+ " cm");
//Add this line
heightvsAverage(players, heights, calcAverage(heights));
Make sure you remove the while back to the if, otherwise you'll have an infinite loop.
Finally, check what the other answers told you, you should only be displaying your header once, then the player's names.
EDIT
I changed the function to this:
// comparing height with the average
public static void heightvsAverage(String [] players, double [] heights, double average)
{
System.out.println("Players with above average are:\n");
System.out.println("Players|Heights");
for(int i=0;i<10;i++)
{
if (heights[i]>average)
{
System.out.println("---------+---------");
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(String.format("%-11s|%8s", players[i],df.format(heights[i])));
}
}
}
that and adding the function call that I mentioned before will get you the print that I'm guessing you need.
FYI, this is simple analysis, and I would think many would agree you gotta try a bit harder yourself before asking others to solve the problem for you, otherwise you won't learn much.
Good luck!

Replacing Clavier.lireDouble [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hello to all and best wishes for 2014.
My question is ... In the following program is it possible to replace the ht = Clavier.lireDouble () to simplify the package because every time I implement this command I have to create a class "Clavier.java "this increased the source code ?
Thank you for your answers
best regards
NOTE FROM EDITOR
Clavier is the French translation of keyboard. The Clavier class seems
to be used for educational purpose. The question asked here could be
"How can my java program read a value from the keyboard input?"
Billing program for lectuire a price exclusive of tax and VAT calculation and VAT and price discounts based on cost
r0%
1000 <= r1%
2000 <= r3%
r5%> = $ 5000 */
/Program (In french)/
public class Tva
{
public static void main (String[]args)
{
double ht;
double ttc, net, tauxr, remise;
double taux_tva = 21.6;
System.out.print ("donnez le prix hors taxe :");
ht = Clavier.lireDouble(); /*Replace this command*/
ttc = ht * (1. + taux_tva/100);
if (ttc < 1000.) tauxr = 0;
else if (ttc < 2000) tauxr = 1.;
else if (ttc < 5000) tauxr = 3.;
else tauxr = 5.;
remise = ttc * tauxr / 1000;
net = ttc - remise;
System.out.println ("Prix Ttc : "+ ttc);
System.out.println ("Remise : "+ remise);
System.out.println ("Net à payer : "+ net);
}
}
You can use the Scanner class for that purpose.
You code to replace can become
Scanner scan = new Scanner(System.in);
ht = scan.nextDouble();
One remark : my mother tongue is also French but IMHO I think it is better to code in English because it is always possible that you will have to share/submit... your code to people who don't understand your mother tongue so it will make things more difficult.
Ah I was about to forget it : because Clavier is a French word, you can be sure it never existed in Java or standard JDK.

Java program involving [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to write the following program sequence.
The first three numbers in the sequence are 1, 1, 2. Every other number in the sequence is the sum of the three previous numbers. The program should prompt the user to enter a limit; the program will stop when the current number in the sequence is greater than or equal to this limit.
For example if I write the limit being 123 I should get: 1 1 2 4 7 13 24 44 81
I tried the following:
import jpb.*;
public class XiaolinSequence {
public static void main(String[] args) {
SimpleIO.prompt("Enter a limit on the largest number to be displayed:");
String userInput = SimpleIO.readLine();
int counter = Integer.parseInt(userInput);
int older = 1;
int old = 1;
int current = 2;
while (current < counter) {
int nextNumber = older + old + current;
older = old;
old = current;
current = nextNumber;
System.out.println(nextNumber);
}
}
}
But I am having trouble getting the sequence to print out.
You need to change how you print things.
The missing 1 1 2 are never printed because they are never stored in nextnumber, the only variable you ever print.
You will get an additional 149 because you print nextnumber without checking it its value is greater than the limit.
For me the output of the following code is 1 1 2 4 7 13 24 44 81 all on new lines.
int counter=123; // replaced IO code so I did not have to download the jar.
int older=1;
int old =1;
int current=2;
System.out.println(older); // prints the first 1
System.out.println(old); // prints the second 1
System.out.println(current); // prints the 2
while(current<counter){
int nextnumber=older+old+current;
older=old;
old=current;
current=nextnumber;
if(nextnumber <= counter)
{
System.out.println(nextnumber);
}
}
Ok since people bashed me for your SimpleIO, use whatever you want to read the input. Instead, I'm going to point out a logic flaw in your code.
For the program to function correctly, you need to print out older instead of current, like so:
while (older < counter)
{
System.out.println(older);
final int nextnumber = older + old + current;
older = old;
old = current;
current = nextnumber;
}
It works just fine.
There is no such thing as SimpleIO in java.lang.
You may want to replace String userInput = SimpleIO.readLine() with
System.out.print("Enter limit: ");
Scanner in = new Scanner(System.in);
String userInput = in.next();
then the code will work.
Oh and by the way, don't forget to print out 1 1 2 before you start the loop.

Categories

Resources