4 Replies - 8360 Views - Last Post: 28 October 2009 - 04:55 PM Rate Topic: -----

#1 harjot00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 15-October 09

Falling Distance

Post icon  Posted 28 October 2009 - 04:18 PM

Question:

Quote

When an object is falling because of gravity, the following formula can be used to determine the distance of the object falls in a specific time period.

d = 1/2 gt2

The Variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the amount of time, in seconds, that the object has been falling.

Write a method named falling Distance that accepts an object's falling time (in seconds) as an argument. The method should return the distance, in meters, that the object has fallen during that time interval. Demonstrate the method by calling it in a loop that passes the value 1 through 10 as arguments, and displays the return value.

heres what I have so far, I don't really understand the point of the loop am I on the right track here?
import java.util.Scanner;
public class Chapter5Question5
{
  public static void main (String [] args)
  {
	double fallingTime;
	Scanner keyboard=new Scanner(System.in);
	System.out.println("Ener the falling time (in seconds): ");
	fallingTime=keyboard.nextDouble();
	fallingDistance(fallingTime);
  }
	public static void fallingDistance(double fallingTime)
	{
	  double a=0.5, g=9.8;//0.5=1/2
	  double d=(a*g)*(fallingTime*fallingTime);	
	  for(int x=1; x<11; x++)
	{
	   System.out.println("");  
	} 
	}
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: Falling Distance

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Falling Distance

Posted 28 October 2009 - 04:27 PM

as your task says, you should make your method return the value ofdistance in meters.
also it says that you should call your method 10 times.

so change your method to:
public static double fallingDistance(double fallingTime){
//make calculations into your double d variable.
//and return its value using "return"
return d;
}



then, inside the main method, from what i understand from the instructions, you should pass the xffrom 1 to 10 to the fallinDistance method.
so dont scan a value to fallingTime. instead use:
for(int x=1;x<11;x++){
System.out.println(fallingDistance(x));
}



this should do.
Was This Post Helpful? 1
  • +
  • -

#3 harjot00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 15-October 09

Re: Falling Distance

Posted 28 October 2009 - 04:33 PM

View Postjapanir, on 28 Oct, 2009 - 03:27 PM, said:

as your task says, you should make your method return the value ofdistance in meters.
also it says that you should call your method 10 times.

so change your method to:
public static double fallingDistance(double fallingTime){
//make calculations into your double d variable.
//and return its value using "return"
return d;
}



then, inside the main method, from what i understand from the instructions, you should pass the xffrom 1 to 10 to the fallinDistance method.
so dont scan a value to fallingTime. instead use:
for(int x=1;x<11;x++){
System.out.println(fallingDistance(x));
}


this should do.


Thank you for the reply so I tried what you are saying:
import java.util.Scanner;
public class Chapter5Question5
{
  public static void main (String [] args)
  {
	double fallingTime;
	Scanner keyboard=new Scanner(System.in);
	System.out.println("Ener the falling time (in seconds): ");
	fallingTime=keyboard.nextDouble();
	fallingDistance(fallingTime);
	
	for(int x=1;x<11;x++)
	{
	System.out.println(fallingDistance(x));
	}

  }
	public static void fallingDistance(double fallingTime)
	{
	  double a=0.5, g=9.8;//0.5=1/2
	  double d=(a*g)*(fallingTime*fallingTime);	
	  return (d);
	}
}


but I get an error for the system.out.println in the loop:
void type not allowed here why is this?
Was This Post Helpful? 0
  • +
  • -

#4 harjot00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 15-October 09

Re: Falling Distance

Posted 28 October 2009 - 04:53 PM

OK so I just figured out what I was doing wrog, and now it seems to work, The loop part was really confusing me, I still don't understand this question fully (mainly why the loop has to be there) but it seems to work fine :^: thank you for your help
import java.util.Scanner;
public class Chapter5Question5
{
  public static void main (String [] args)
  {
	double fallingTime;
	Scanner keyboard=new Scanner(System.in);
	System.out.println("Ener the falling time (in seconds): ");
	fallingTime=keyboard.nextDouble();

	for(int x=1;x<11;x++)
	{
	System.out.println(fallingDistance(x));
	}

  }
	public static double fallingDistance(double fallingTime)
	{
	  double a=0.5, g=9.8;//0.5=1/2
	  double d=(a*g)*(fallingTime*fallingTime);	
	  return (d);
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Falling Distance

Posted 28 October 2009 - 04:55 PM

you left it as void.
you should change your method to return double as i suggested.
public static double fallingDistance(double fallingTime){
//code
}



you see the double i wrote instead of the void?
that indicates that the method returns a double value.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1